ModernWPF.Internal.LegacyBorderManager.HandleNcCalcSize C# (CSharp) Method

HandleNcCalcSize() static private method

static private HandleNcCalcSize ( IntPtr hwnd, IntPtr wParam, IntPtr lParam ) : void
hwnd System.IntPtr
wParam System.IntPtr
lParam System.IntPtr
return void
        static void HandleNcCalcSize(IntPtr hwnd, IntPtr wParam, IntPtr lParam)
        {
            if (wParam == BasicValues.TRUE)
            {
                var wpl = default(WINDOWPLACEMENT);
                wpl.length = (uint)Marshal.SizeOf(typeof(WINDOWPLACEMENT));

                if (User32.GetWindowPlacement(hwnd, ref wpl))
                {
                    // detect if maximizd and set to workspace remove padding
                    if (wpl.showCmd == ShowWindowOption.SW_MAXIMIZE)
                    {
                        // in multi-monitor case where app is minimized to a monitor on the right/bottom
                        // the MonitorFromWindow will incorrectly return the leftmost monitor due to the minimized
                        // window being set to the far left, so this routine now uses the proposed rect to correctly
                        // identify the real nearest monitor to calc the nc size.

                        NCCALCSIZE_PARAMS para = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(lParam, typeof(NCCALCSIZE_PARAMS));

                        var windowRect = para.rectProposed;
                        IntPtr hMonitor = User32.MonitorFromRect(ref windowRect, MonitorOption.MONITOR_DEFAULTTONEAREST);// MonitorFromWindow(hWnd, 2);
                        if (hMonitor != IntPtr.Zero)
                        {
                            MONITORINFO lpmi = new MONITORINFO();
                            lpmi.cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO));
                            if (User32.GetMonitorInfo(hMonitor, ref lpmi))
                            {
                                var workArea = lpmi.rcWork;
                                User32Ex.AdjustForAutoHideTaskbar(hMonitor, ref workArea);
                                Debug.WriteLine("NCCalc original = {0}x{1} @ {2}x{3}, new ={4}x{5} @ {6}x{7}",
                                    para.rectProposed.Width, para.rectProposed.Height,
                                    para.rectProposed.left, para.rectProposed.top,
                                    workArea.Width, workArea.Height,
                                    workArea.left, workArea.top);
                                para.rectProposed = workArea;
                                Marshal.StructureToPtr(para, lParam, true);

                            }
                        }
                    }
                }
            }
        }