我正在创建一个自定义表单(C#/Windows Forms/Vista/Windows7),并覆盖 WndProc 以捕获 WM_NCPAINT、WM_NCCALCSIZE 和 WM_NCHITTEST 以绘制自定义框架。我几乎完成了它,但有一个我无法解决的问题。
问题是 NC_CALCSIZE 使我的表单在最大化后恢复时缩小。我用谷歌搜索并找到了 Bob Powell 的答案,他说当 WPARAM 为 TRUE 时我不需要处理 NC_CALCSIZE。在我这样做之后,WM_NCPAINT 不再起作用(它确实处理了 WM_NCPAINT,但它不再绘制非客户区,只有在我使其无效之后)。
因此,恢复,当我处理 WM_NCCALCSIZE(WPARAM == TRUE) 时,它会缩小我的表单,当我不处理时,它不会再绘制了。
以前有人遇到过这个问题吗?如果需要更多代码,我可以提供。谢了。
这是我的 WN_CALCSIZE 代码:
private void WndProcNonClientCalcSize(ref Message m)
{
if (m.WParam == WinAPI.FALSE)
{
this.Log(MethodInfo.GetCurrentMethod(), "FALSE");
WinAPI.NCCALCSIZE_PARAMS csp;
csp = (WinAPI.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(WinAPI.NCCALCSIZE_PARAMS));
csp.rectProposed.Top += this._nonClientHeight;
csp.rectProposed.Bottom -= this._nonClientBorderThickness;
csp.rectProposed.Left += this._nonClientBorderThickness;
csp.rectProposed.Right -= this._nonClientBorderThickness;
Marshal.StructureToPtr(csp, m.LParam, false);
}
else if (m.WParam == WinAPI.TRUE)
{
this.Log(MethodInfo.GetCurrentMethod(), "TRUE");
WinAPI.NCCALCSIZE_PARAMS csp;
csp = (WinAPI.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(WinAPI.NCCALCSIZE_PARAMS));
csp.rectProposed.Top += this._nonClientHeight;
csp.rectProposed.Bottom -= this._nonClientBorderThickness;
csp.rectProposed.Left += this._nonClientBorderThickness;
csp.rectProposed.Right -= this._nonClientBorderThickness;
Marshal.StructureToPtr(csp, m.LParam, false);
}
m.Result = WinAPI.TRUE;
}
这是我的 WM_NCPAINT 代码:
private bool WndProcNonClientPaint(ref Message m)
{
this.Log(MethodInfo.GetCurrentMethod(), string.Empty);
this.PaintNonClient(m.HWnd, (IntPtr)m.WParam);
m.Result = WinAPI.TRUE;
return true;
}
private void PaintNonClient(IntPtr hWnd, IntPtr hRgn)
{
WinAPI.RECT windowRect = new WinAPI.RECT();
WinAPI.GetWindowRect(hWnd, ref windowRect);
Rectangle bounds = new Rectangle(0, 0,
windowRect.Right - windowRect.Left,
windowRect.Bottom - windowRect.Top);
if (bounds.Width == 0 || bounds.Height == 0)
return;
Region clipRegion = new Region(bounds);
if (hRgn != (IntPtr)1)
clipRegion = Region.FromHrgn(hRgn);
WinAPI.DCV dcv =
WinAPI.DCV.WINDOW |
WinAPI.DCV.INTERSECTRGN |
WinAPI.DCV.CACHE |
WinAPI.DCV.CLIPSIBLINGS;
IntPtr hDC =
WinAPI.GetDCEx(
hWnd,
hRgn,
dcv);
if (hDC == IntPtr.Zero)
hDC = WinAPI.GetWindowDC(hWnd);
IntPtr compatiblehDC = WinAPI.CreateCompatibleDC(hDC);
IntPtr compatibleBitmap = WinAPI.CreateCompatibleBitmap(hDC, bounds.Width, bounds.Height);
try
{
WinAPI.SelectObject(compatiblehDC, compatibleBitmap);
WinAPI.BitBlt(compatiblehDC, 0, 0, bounds.Width, bounds.Height, hDC, 0, 0, WinAPI.SRCCOPY);
using (Graphics g = Graphics.FromHdc(compatiblehDC))
{
Rectangle outterEdge = new Rectangle(0, 0, this.Width, this.Height);
int x = this._nonClientBorderThickness;
int y = this._nonClientHeight;
int width = this.Width - (this._nonClientBorderThickness * 2);
int height = this.Height - this._nonClientBorderThickness - this._nonClientHeight;
Rectangle innerEdge = new Rectangle(x, y, width, height);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(outterEdge);
path.AddRectangle(innerEdge);
using (SolidBrush brush = new SolidBrush(Color.FromArgb(45, 45, 48)))
g.FillPath(brush, path);
path.Dispose();
}
WinAPI.BitBlt(hDC, 0, 0, bounds.Width, bounds.Height, compatiblehDC, 0, 0, WinAPI.SRCCOPY);
}
finally
{
WinAPI.DeleteObject(compatibleBitmap);
WinAPI.DeleteDC(compatiblehDC);
}
}