title: Custom window frame with DWM: how to handle WM_NCCALCSIZE correctly
body: <p>I'm trying to make a custom window frame for my form using DWM.
  The platform is C# WinForms, Pinvoking DWM.</p>
  <p>Following the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb688195%28v=vs.85%29.aspx" rel="noreferrer">MSDN article on making custom window frame with DWM</a>, the main steps are next:</p>
  <ol>
  <li>Remove standard frame (non-client area), returning 0 in answer to WM_NCCALCSIZE message</li>
  <li>Extend the frame into client area using DwmExtendFrameIntoClientArea function</li>
  </ol>
  <p>I handle WM_NCCALCSIZE message in the next way:</p>
  <pre><code>protected override void WndProc(ref Message m)
  {
     switch (m.Msg)
     {
         case WM_NCCALCSIZE:
              if (isDwmWindowFramePaintEnabled() &amp;&amp; m.WParam != IntPtr.Zero)
              {
                  m.Result = IntPtr.Zero;
              }
              else
              {
                  base.WndProc(ref m);
              }
              return;
     }
  }
  </code></pre>
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms632634%28v=vs.85%29.aspx" rel="noreferrer">MSDN documentation on WM_NCCALCSIZE</a>,</p>
  <blockquote>
  <p>When wParam is TRUE, simply returning 0 without processing the
  NCCALCSIZE_PARAMS rectangles will cause the client area to resize to
  the size of the window, including the window frame. This will remove
  the window frame and caption items from your window, leaving only the
  client area displayed.</p>
  </blockquote>
  <p>Everything is fine and works for me except one issue.
  When I maximize/restore window, it's always growing a little bit when it gets restored.
  I think, the issue is something like this:</p>
  <ol>
  <li>When window gets restored, it contains client area only</li>
  <li>Windows tries to give some non-client area to the window</li>
  <li>In WM_NCCALCSIZE client area grows to contain non-client area</li>
  </ol>
  <p>So, like this window grows a little bit each time I maximize/restore it.
  I need to remove non-client area to paint custom form frame with DWM.
  I can't simply set window border style to none as then DWM will not paint the window caption and borders.</p>
  <p>Please, help to solve the issue and happily have a custom window frame.</p>