简体   繁体   中英

Should I receive WM_NCPAINT when there is no client area?

There is something I would like to understand in my custom control. I handle WM_NCCALCSIZE to set the client area to the entire window, in other terms there is no nonclient area. I was expecting to not receive WM_NCPAINT but I still receive it each time the window size changes. Here is my WndProc code:

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);
    }
}

So, when a resize occurs, I checked and WM_NCCALCSIZE is correctly received, _drawManager.NcCalcSize does not modify "rect", then WM_NCPAINT is received and I'm obliged to compare the bounds and client rect to check if any non client painting should occur. Is this normal?

My guess would be that it's

1) Easier for Windows to do it this way(no special case for missing border), and the benefit of not sending it is only a marginal performance gain.
2) It can't be changed now since some programs require the message to be sent since they do too much in the handler. And if you read Raymond Chen's blog you'll know how important that is to the windows api team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM