protected override void WndProc(ref Message m) { switch (m.Msg) { case Win32.WM_SYSCOMMAND: { maximized = (int)m.WParam == Win32.SC_MAXIMIZE; break; } case Win32.WM_SIZE: { maximized = (int)m.WParam == Win32.SIZE_MAXIMIZED; break; } case Win32.WM_NCPAINT: { //break; if (Dwm.CompositionEnabled) { //var v = 2; //Dwm.SetWindowAttribute(Handle, 2, ref v, 4); MARGINS margins = new MARGINS(1); Dwm.ExtendFrameIntoClientArea(Handle, ref margins); } break; } case Win32.WM_NCCALCSIZE: { var r = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); var max = WindowState == FormWindowState.Maximized; if (max) { var x = Win32.GetSystemMetrics(Win32.SM_CXSIZEFRAME); var y = Win32.GetSystemMetrics(Win32.SM_CYSIZEFRAME); var p = Win32.GetSystemMetrics(Win32.SM_CXPADDEDBORDER); var w = x + p; var h = y + p; r.left += w; r.top += h; r.right -= w; r.bottom -= h; var appBarData = new APPBARDATA(); appBarData.cbSize = Marshal.SizeOf(typeof(APPBARDATA)); var autohide = (Win32.SHAppBarMessage(Win32.ABM_GETSTATE, ref appBarData) & Win32.ABS_AUTOHIDE) != 0; if (autohide) { r.bottom -= 1; } Marshal.StructureToPtr(r, m.LParam, true); } m.Result = IntPtr.Zero; return; } case Win32.WM_NCHITTEST: { int x = m.LParam.ToInt32() & 0xFFFF; int y = (m.LParam.ToInt32() >> 16) & 0xFFFF; Point c = PointToClient(new Point(x, y)); x = c.X; y = c.Y; int result; if (WindowState == FormWindowState.Maximized) { result = Win32.HTCAPTION; } else { if (x < BorderWidth) { if (y < BorderWidth) { result = Win32.HTTOPLEFT; } else if (y >= Height - BorderWidth) { result = Win32.HTBOTTOMLEFT; } else { result = Win32.HTLEFT; } } else if (x >= Width - BorderWidth) { if (y < BorderWidth) { result = Win32.HTTOPRIGHT; } else if (y >= Height - BorderWidth) { result = Win32.HTBOTTOMRIGHT; } else { result = Win32.HTRIGHT; } } else { if (y < BorderWidth) { result = Win32.HTTOP; } else if (y >= Height - BorderWidth) { result = Win32.HTBOTTOM; } else { result = Win32.HTCAPTION; } } } m.Result = new IntPtr(result); return; } default: break; } base.WndProc(ref m); }
[DllImport("shell32.dll")] public static extern int SHAppBarMessage(uint dwMessage, [In] ref APPBARDATA pData);