I'm now overriding wndproc and handling the wm_ncpaint by letting base
do it first and then drawing over the top... This works the first time
the program draws the close button but after that it doesn't work...
Note: I'm just drawing a big white box right now to get the
functionality in - why is the close button drawing outside of
wm_ncpaint?... Here's the code:
protected override void WndProc(ref Message m)
{
switch ( m.Msg )
{
case (int) MSG.WM_NCPAINT:
base.WndProc( ref m );
Main_OnNcPaint( ref m );
return;
}
}
private void Main_OnNcPaint ( ref Message m )
{
Debug.WriteLine("drawing");
// If I use GetDCEx, the program crashes on the Graphics
// .FromHdc Line - GetDCEx returns 0?
//IntPtr hdc = User32.GetDCEx( m.HWnd, m.WParam,
((uint)DCX_FLAGS.DCX_WINDOW)|((uint)DCX_FLAGS.DCX_INTERSECTRGN) );
IntPtr hdc = User32.GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hdc);
int CaptionHeight = Bounds.Height - ClientRectangle.Height; //Titlebar
Size CloseButtonSize = SystemInformation.CaptionButtonSize;
int X = Bounds.Width - CloseButtonSize.Width;
int Y = 6;
// ControlPaint causes the program to not use XP styles anymore
//ControlPaint.DrawButton(g, X, Y, 15, 15, ButtonState.Normal);
// Fill a rectangle instead
g.FillRectangle(SystemBrushes.Window, X, Y, CloseButtonSize.Width,
CloseButtonSize.Height);
//g.Dispose();
User32.ReleaseDC(m.HWnd, hdc);
m.Result = IntPtr.Zero;
}