A bad thing about maximizing a WPF window is that if the window's WindowStyle is set to none directly or indirectly (for example, you will override the default form style in many cases, that is, not using the Windows default border and maximizing the button to create a personalized form), When the window is maximized, the window fills the screen and covers the taskbar. This often does not meet the actual requirements.
Here's a nice solution to solve the problem by Wm_getminmaxinfo (msdn:the wm_getminmaxinfo message being sent to a windows when the size or position O f The window is about to change. An application can use this message to override the window ' s default maximized size and position, or its default minimum O R maximum tracking size.) Messages are hooked up to a hook to handle. The message code is: 0x0024 (more message code can refer to the appendix of this article)
There's nothing to say, just go to the code (call the Fullscreenmanager.repairwpfwindowfullscreenbehavior method for the window you want to process):
public static class Fullscreenmanager {public static void Repairwpfwindowfullscreenbehavior (Window wpfwindow)
{if (Wpfwindow = = null) {return;
} if (wpfwindow.windowstate = = windowstate.maximized) {wpfwindow.windowstate = Windowstate.normal;
wpfwindow.loaded + = delegate {wpfwindow.windowstate = windowstate.maximized;}; } wpfwindow.sourceinitialized + = delegate {INTPTR handle = (new Windowinterophelper (Wpfwindow)).
Handle;
HwndSource Source = Hwndsource.fromhwnd (handle); if (source!= null) {source.
Addhook (WINDOWPROC);
} }; private static INTPTR WindowProc (INTPTR hwnd, int msg, IntPtr wParam, IntPtr LParam, ref BOOL handled) {s
Witch (msg) {case 0x0024:wmgetminmaxinfo (hwnd, LParam);
handled = TRUE;
Break
Return (INTPTR) 0; } private static void WMGETMINMAxinfo (IntPtr hwnd, IntPtr LParam) {var mmi = (minmaxinfo) marshal.ptrtostructure (LParam, typeof (Minmaxinfo)); Adjust the maximized size and position to fit the work area of the correct monitor int monitor_defaulttonear
EST = 0x00000002;
INTPTR monitor = Monitorfromwindow (hwnd, monitor_defaulttonearest);
if (monitor!= IntPtr.Zero) {var monitorinfo = new Monitorinfo ();
Getmonitorinfo (monitor, monitorinfo);
RECT Rcworkarea = monitorinfo.rcwork;
RECT Rcmonitorarea = Monitorinfo.rcmonitor;
mmi.ptmaxposition.x = Math.Abs (rcworkarea.left-rcmonitorarea.left);
MMI.PTMAXPOSITION.Y = Math.Abs (rcworkarea.top-rcmonitorarea.top);
mmi.ptmaxsize.x = Math.Abs (rcworkarea.right-rcworkarea.left);
Mmi.ptmaxsize.y = Math.Abs (rcworkarea.bottom-rcworkarea.top);
Marshal.structuretoptr (MMI, LParam, True); }