public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO info);
/// <summary> /// Retrieves information about a display monitor. /// </summary> /// <param name="hMonitor">A handle to the display monitor of interest.</param> /// <param name="info">A MONITORINFO or MONITORINFOEX structure that receives information about the specified display monitor.</param> /// <returns></returns> public static bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO info) { return NativeMethods.GetMonitorInfo(hMonitor, ref info); }
private 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); } } } } } }