在我的自定义控件中有一些我想了解的东西。我处理 WM_NCCALCSIZE 将客户区设置为整个窗口,换句话说,没有非客户区。我原以为不会收到 WM_NCPAINT 但每次窗口大小更改时我仍然会收到它。这是我的 WndProc 代码:
if (m.Msg == Win32Calls.WM_NCPAINT)
{
// I don't know why WM_NCPAINT is sent when WM_NCCALCSIZE has stated that there is no client area, so here is my workaround to stop processing here
if (Bounds.Size == ClientSize)
return;
// Draw borders if any
if (handled)
return;
}
else if (m.Msg == Win32Calls.WM_NCCALCSIZE)
{
if (m.WParam != IntPtr.Zero)
{
Win32Calls.NCCALCSIZE_PARAMS csp;
csp = (Win32Calls.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(Win32Calls.NCCALCSIZE_PARAMS));
Rectangle rect = new Rectangle(csp.rgrc0.Left, csp.rgrc0.Top,
csp.rgrc0.Right - csp.rgrc0.Left, csp.rgrc0.Bottom - csp.rgrc0.Top);
_drawManager.NcCalcSize(ref rect);
csp.rgrc0.Left = rect.X;
csp.rgrc0.Right = rect.X + rect.Width;
csp.rgrc0.Top = rect.Y;
csp.rgrc0.Bottom = rect.Y + rect.Height;
Marshal.StructureToPtr(csp, m.LParam, false);
}
}
因此,当发生调整大小时,我检查并正确接收 WM_NCCALCSIZE,_drawManager.NcCalcSize 不修改“rect”,然后接收 WM_NCPAINT,我有义务比较边界和客户端矩形以检查是否应该发生任何非客户端绘画. 这是正常的吗?