title: How do I draw to the window's title bar in C++.NET?
body: <p>I'm trying to draw a custom title bar, and I've read that in order to paint outside a window's client area, I need to override WndProc and handle the WM_NCPAINT message. Currently, I'm doing that like this:</p>
  
  <pre><code>//WndProc override
  virtual void WndProc(Message% m) override
  {
      switch(m.Msg)
      {
          case 0x85:  //WM_NCPAINT
          case 0x0A:  //WM_PAINT
              //Call original
              System::Windows::Forms::Form::WndProc(m);
  
              //Now we'll do our painting
              DrawTitleBar(m.HWnd);
  
              break;
          default:
              System::Windows::Forms::Form::WndProc(m);
              break;
      }
  }
  </code></pre>
  
  <p>Which works, because I can put a breakpoint in and it gets hit. If I remove the call to the original, the window's frame isn't drawn. <code>DrawTitleBar</code> looks like this:</p>
  
  <pre><code>void DrawTitleBar(IntPtr hWnd)
  {
      IntPtr hDC;
      Graphics^ g;
  
      //Get the device context (DC)
      hDC = GetWindowDC(hWnd);
      //Get the graphics
      g = Graphics::FromHdc(hDC);
  
      //Draw
      g-&gt;FillRectangle(Brushes::Blue, 0, 0, 100, 10);
  
      //Cleanup
      g-&gt;Flush();
      ReleaseDC(hWnd, hDC);
  }
  </code></pre>
  
  <p>I first get the DC from the window handle. Then I get the <code>Graphics</code> object by using <code>Graphics::FromHdc</code>. I release the DC with <code>ReleaseDC</code>. Incase there's an issue here, this is how I import the native Win32 functions:</p>
  
  <pre><code>[DllImport("user32.dll")]
  extern IntPtr GetWindowDC(IntPtr hWnd);
  [DllImport("user32.dll")]
  extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  </code></pre>
  
  <p>Also: I've tried a bunch of different methods, all with the same results. I can find a bunch of C# and VB examples on the web, but no C++ examples. I've also read about Windows Vista compatibility being an issue with this sort of thing. Currently, I don't care about that, as I will add it later.</p>