FullScreenManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows;
  4. using System.Windows.Interop;
  5. namespace Muchinfo.PC.Common.Helpers
  6. {
  7. /// <summary>
  8. /// 避免窗口最大化时遮盖任务栏
  9. /// </summary>
  10. public static class FullScreenManager
  11. {
  12. private static Window _window;
  13. public static void RepairWpfWindowFullScreenBehavior(Window wpfWindow)
  14. {
  15. if (wpfWindow == null)
  16. {
  17. return;
  18. }
  19. _window = wpfWindow;
  20. if (wpfWindow.WindowState == WindowState.Maximized)
  21. {
  22. wpfWindow.WindowState = WindowState.Normal;
  23. wpfWindow.Loaded += delegate { wpfWindow.WindowState = WindowState.Maximized; };
  24. }
  25. wpfWindow.SourceInitialized += delegate
  26. {
  27. IntPtr handle = (new WindowInteropHelper(wpfWindow)).Handle;
  28. HwndSource source = HwndSource.FromHwnd(handle);
  29. if (source != null)
  30. {
  31. source.AddHook(WindowProc);
  32. }
  33. };
  34. }
  35. private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  36. {
  37. switch (msg)
  38. {
  39. case 0x0024:
  40. WmGetMinMaxInfo(hwnd, lParam);
  41. handled = true;
  42. break;
  43. }
  44. return (IntPtr)0;
  45. }
  46. private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
  47. {
  48. var mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  49. // Adjust the maximized size and position to fit the work area of the correct monitor
  50. int MONITOR_DEFAULTTONEAREST = 0x00000002;
  51. IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  52. if (monitor != IntPtr.Zero)
  53. {
  54. var monitorInfo = new MONITORINFO();
  55. GetMonitorInfo(monitor, monitorInfo);
  56. RECT rcWorkArea = monitorInfo.rcWork;
  57. RECT rcMonitorArea = monitorInfo.rcMonitor;
  58. mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  59. mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  60. mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  61. mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  62. ////添加窗口最小Size控制,否则即使窗口设置了最小高宽也不启作用
  63. if (_window != null)
  64. {
  65. if (_window.MinWidth > 0) mmi.ptMinTrackSize.x = (int)_window.MinWidth;
  66. if (_window.MinHeight > 0) mmi.ptMinTrackSize.y = (int)_window.MinHeight;
  67. }
  68. }
  69. Marshal.StructureToPtr(mmi, lParam, true);
  70. }
  71. [DllImport("user32")]
  72. internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. [DllImport("User32")]
  77. internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
  78. #region Nested type: MINMAXINFO
  79. [StructLayout(LayoutKind.Sequential)]
  80. internal struct MINMAXINFO
  81. {
  82. public POINT ptReserved;
  83. public POINT ptMaxSize;
  84. public POINT ptMaxPosition;
  85. public POINT ptMinTrackSize;
  86. public POINT ptMaxTrackSize;
  87. } ;
  88. #endregion
  89. #region Nested type: MONITORINFO
  90. /// <summary>
  91. /// </summary>
  92. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  93. internal class MONITORINFO
  94. {
  95. /// <summary>
  96. /// </summary>
  97. public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  98. /// <summary>
  99. /// </summary>
  100. public RECT rcMonitor;
  101. /// <summary>
  102. /// </summary>
  103. public RECT rcWork;
  104. /// <summary>
  105. /// </summary>
  106. public int dwFlags;
  107. }
  108. #endregion
  109. #region Nested type: POINT
  110. /// <summary>
  111. /// POINT aka POINTAPI
  112. /// </summary>
  113. [StructLayout(LayoutKind.Sequential)]
  114. internal struct POINT
  115. {
  116. /// <summary>
  117. /// x coordinate of point.
  118. /// </summary>
  119. public int x;
  120. /// <summary>
  121. /// y coordinate of point.
  122. /// </summary>
  123. public int y;
  124. /// <summary>
  125. /// Construct a point of coordinates (x,y).
  126. /// </summary>
  127. public POINT(int x, int y)
  128. {
  129. this.x = x;
  130. this.y = y;
  131. }
  132. }
  133. #endregion
  134. #region Nested type: RECT
  135. /// <summary> Win32 </summary>
  136. [StructLayout(LayoutKind.Sequential, Pack = 0)]
  137. internal struct RECT
  138. {
  139. /// <summary> Win32 </summary>
  140. public int left;
  141. /// <summary> Win32 </summary>
  142. public int top;
  143. /// <summary> Win32 </summary>
  144. public int right;
  145. /// <summary> Win32 </summary>
  146. public int bottom;
  147. /// <summary> Win32 </summary>
  148. public static readonly RECT Empty;
  149. /// <summary> Win32 </summary>
  150. public int Width
  151. {
  152. get { return Math.Abs(right - left); } // Abs needed for BIDI OS
  153. }
  154. /// <summary> Win32 </summary>
  155. public int Height
  156. {
  157. get { return bottom - top; }
  158. }
  159. /// <summary> Win32 </summary>
  160. public RECT(int left, int top, int right, int bottom)
  161. {
  162. this.left = left;
  163. this.top = top;
  164. this.right = right;
  165. this.bottom = bottom;
  166. }
  167. /// <summary> Win32 </summary>
  168. public RECT(RECT rcSrc)
  169. {
  170. left = rcSrc.left;
  171. top = rcSrc.top;
  172. right = rcSrc.right;
  173. bottom = rcSrc.bottom;
  174. }
  175. /// <summary> Win32 </summary>
  176. public bool IsEmpty
  177. {
  178. get
  179. {
  180. // BUGBUG : On Bidi OS (hebrew arabic) left > right
  181. return left >= right || top >= bottom;
  182. }
  183. }
  184. /// <summary> Return a user friendly representation of this struct </summary>
  185. public override string ToString()
  186. {
  187. if (this == Empty)
  188. {
  189. return "RECT {Empty}";
  190. }
  191. return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom +
  192. " }";
  193. }
  194. /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
  195. public override bool Equals(object obj)
  196. {
  197. if (!(obj is Rect))
  198. {
  199. return false;
  200. }
  201. return (this == (RECT)obj);
  202. }
  203. /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
  204. public override int GetHashCode()
  205. {
  206. return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
  207. }
  208. /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
  209. public static bool operator ==(RECT rect1, RECT rect2)
  210. {
  211. return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right &&
  212. rect1.bottom == rect2.bottom);
  213. }
  214. /// <summary> Determine if 2 RECT are different(deep compare)</summary>
  215. public static bool operator !=(RECT rect1, RECT rect2)
  216. {
  217. return !(rect1 == rect2);
  218. }
  219. }
  220. #endregion
  221. }
  222. }