HwndRenderTarget.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Avalonia.Win32.Interop;
  7. using SharpDX;
  8. using SharpDX.DXGI;
  9. namespace Avalonia.Direct2D1
  10. {
  11. class HwndRenderTarget : SwapChainRenderTarget
  12. {
  13. private readonly IntPtr _hwnd;
  14. public HwndRenderTarget(IntPtr hwnd)
  15. {
  16. _hwnd = hwnd;
  17. }
  18. protected override SwapChain1 CreateSwapChain(Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc)
  19. {
  20. return new SwapChain1(dxgiFactory, Device, _hwnd, ref swapChainDesc);
  21. }
  22. protected override Size2F GetWindowDpi()
  23. {
  24. if (UnmanagedMethods.ShCoreAvailable)
  25. {
  26. uint dpix, dpiy;
  27. var monitor = UnmanagedMethods.MonitorFromWindow(
  28. _hwnd,
  29. UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST);
  30. if (UnmanagedMethods.GetDpiForMonitor(
  31. monitor,
  32. UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI,
  33. out dpix,
  34. out dpiy) == 0)
  35. {
  36. return new Size2F(dpix, dpiy);
  37. }
  38. }
  39. return new Size2F(96, 96);
  40. }
  41. protected override Size2 GetWindowSize()
  42. {
  43. UnmanagedMethods.RECT rc;
  44. UnmanagedMethods.GetClientRect(_hwnd, out rc);
  45. return new Size2(rc.right - rc.left, rc.bottom - rc.top);
  46. }
  47. }
  48. }