Custom window frame with DWM: how to handle WM_NCCALCSIZE correctly

I am trying to create my own window frame for my form using DWM. The platform is C # WinForms, Pinwoking DWM.

Following the MSDN article on creating a custom window frame with DWM, the following basic steps:

  • Delete the standard frame (an area without a client), returning 0 in response to the WM_NCCALCSIZE message
  • Extending the scope in the client area using the DwmExtendFrameIntoClientArea function

I process the WM_NCCALCSIZE message as follows:

protected override void WndProc(ref Message m)
{
   switch (m.Msg)
   {
       case WM_NCCALCSIZE:
            if (isDwmWindowFramePaintEnabled() && m.WParam != IntPtr.Zero)
            {
                m.Result = IntPtr.Zero;
            }
            else
            {
                base.WndProc(ref m);
            }
            return;
   }
}

According to the MSDN documentation for WM_NCCALCSIZE ,

wParam , 0 NCCALCSIZE_PARAMS , . , .

, . / , , . , :

  • ,
  • Windows
  • WM_NCCALCSIZE , .

, , / . , DWM. , DWM .

, , , .

+4
1

Windows Forms, . Form.SizeFromClientSize(int, int) AdjustWindowRectEx . :

  • RestoreWindowBoundsIfNecessary WM_WINDOWPOSCHANGED
  • SetClientSizeCore

:

  • CreateParams :

    private bool createParamsHack;
    
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            // Remove styles that affect the border size
            if (createParamsHack)
                cp.Style &= ~(int)(WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_THICKFRAME);
            return cp;
        }
    }
    
  • WndProc WM_WINDOWPOSCHANGED:

        if (m.Msg == WM_WINDOWPOSCHANGED)
        {
            createParamsHack = true;
            base.WndProc(ref m);
            createParamsHack = false;
        }
    
  • SetClientSizeCore:

    protected override void SetClientSizeCore(int x, int y)
    {
        createParamsHack = true;
        base.SetClientSizeCore(x, y);
        createParamsHack = false;
    }
    

SizeFromClientSize(Size), , .

+3

All Articles