WindowUtil.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Forms;
  11. using System.Windows.Interop;
  12. namespace GeekDesk.Util
  13. {
  14. public class WindowUtil
  15. {
  16. public enum GetWindowCmd : uint
  17. {
  18. GW_HWNDFIRST = 0,
  19. GW_HWNDLAST = 1,
  20. GW_HWNDNEXT = 2,
  21. GW_HWNDPREV = 3,
  22. GW_OWNER = 4,
  23. GW_CHILD = 5,
  24. GW_ENABLEDPOPUP = 6
  25. }
  26. [Flags]
  27. public enum SetWindowPosFlags
  28. {
  29. SWP_NOSIZE = 0x0001,
  30. SWP_NOMOVE = 0x0002,
  31. SWP_NOZORDER = 0x0004,
  32. SWP_NOREDRAW = 0x0008,
  33. SWP_NOACTIVATE = 0x0010,
  34. SWP_FRAMECHANGED = 0x0020,
  35. SWP_SHOWWINDOW = 0x0040,
  36. SWP_HIDEWINDOW = 0x0080,
  37. SWP_NOCOPYBITS = 0x0100,
  38. SWP_NOOWNERZORDER = 0x0200,
  39. SWP_NOSENDCHANGING = 0x0400
  40. }
  41. //取得前台窗口句柄函数
  42. [DllImport("user32.dll")]
  43. private static extern IntPtr GetForegroundWindow();
  44. //取得桌面窗口句柄函数
  45. [DllImport("user32.dll")]
  46. private static extern IntPtr GetDesktopWindow();
  47. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  48. private static extern IntPtr FindWindow(string className, string windowName);
  49. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  50. private static extern IntPtr GetWindow(HandleRef hWnd, int nCmd);
  51. [DllImport("user32.dll")]
  52. private static extern IntPtr SetParent(IntPtr child, IntPtr parent);
  53. [DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
  54. private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
  55. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  56. private static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy, int flags);
  57. [DllImport("user32.dll")]
  58. private static extern int ReleaseDC(IntPtr window, IntPtr handle);
  59. public static void SetOwner(Window source, IntPtr parentHandle)
  60. {
  61. WindowInteropHelper helper = new WindowInteropHelper(source);
  62. helper.Owner = parentHandle;
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. /// <param name="window"></param>
  68. /// <returns></returns>
  69. public static bool WindowIsTop(Window window)
  70. {
  71. IntPtr handle = new WindowInteropHelper(window).Handle;
  72. IntPtr deskHandle = GetDesktopHandle(window, DesktopLayer.Progman);
  73. IntPtr deskHandle2 = GetDesktopHandle(window, DesktopLayer.FolderView);
  74. IntPtr deskHandle3 = GetDesktopHandle(window, DesktopLayer.SHELLDLL);
  75. IntPtr topHandle = GetForegroundWindow();
  76. return (topHandle.Equals(handle) || topHandle.Equals(deskHandle));
  77. }
  78. public const int GW_CHILD = 5;
  79. public static IntPtr GetDesktopHandle(Window window, DesktopLayer layer)
  80. {
  81. HandleRef hWnd;
  82. IntPtr hDesktop = new IntPtr();
  83. switch (layer)
  84. {
  85. case DesktopLayer.Progman:
  86. hDesktop = FindWindow("Progman", null);//第一层桌面
  87. break;
  88. case DesktopLayer.SHELLDLL:
  89. hDesktop = FindWindow("Progman", null);//第一层桌面
  90. hWnd = new HandleRef(window, hDesktop);
  91. hDesktop = GetWindow(hWnd, GW_CHILD);//第2层桌面
  92. break;
  93. case DesktopLayer.FolderView:
  94. hDesktop = FindWindow("Progman", null);//第一层桌面
  95. hWnd = new HandleRef(window, hDesktop);
  96. hDesktop = GetWindow(hWnd, GW_CHILD);//第2层桌面
  97. hWnd = new HandleRef(window, hDesktop);
  98. hDesktop = GetWindow(hWnd, GW_CHILD);//第3层桌面
  99. break;
  100. }
  101. return hDesktop;
  102. }
  103. public void EmbedDesktop(Object embeddedWindow, IntPtr childWindow, IntPtr parentWindow)
  104. {
  105. Form window = (Form)embeddedWindow;
  106. HandleRef HWND_BOTTOM = new HandleRef(embeddedWindow, new IntPtr(1));
  107. const int SWP_FRAMECHANGED = 0x0020;//发送窗口大小改变消息
  108. SetParent(childWindow, parentWindow);
  109. SetWindowPos(new HandleRef(window, childWindow), HWND_BOTTOM, 300, 300, window.Width, window.Height, SWP_FRAMECHANGED);
  110. }
  111. }
  112. public enum DesktopLayer
  113. {
  114. Progman = 0,
  115. SHELLDLL = 1,
  116. FolderView = 2
  117. }
  118. }