Best way to customize winforms frame?

628 Views Asked by At

I would like to draw some simple rectangles and text over the window frame so it looks like Visual Studio. Can it be done or should i make a borderless window and handle moving andresizing myself?

EDIT: I wrote this. But how do I draw now?

protected override void WndProc(ref Message m)
{
    if (m.Msg == WndProcMsg.WM_PAINT) //0x000f
    {

    }
    base.WndProc(ref m);
}
2

There are 2 best solutions below

0
On BEST ANSWER

https://learn.microsoft.com/en-us/windows/desktop/dwm/customframe works great. It was traslated to C# by @Chris Taylor. You can download the project from his OneDrive

Just fix case Win32Messages.WM_NCHITTEST with:

int ht = NCHitText(m);
if (callDWP) 
{ 
   callDWP = (ht == Win32Constants.HTNOWHERE);
    result = new IntPtr(ht);
}
0
On

You should handle the WM_NCPAINT instead:

if (m.Msg == WM_NCPAINT) 
            { 

                IntPtr hdc = GetWindowDC(m.HWnd); 
                if ((int)hdc != 0) 
                { 
                    Graphics g = Graphics.FromHdc(hdc); 
                   .... work with graphics
                    ReleaseDC(m.HWnd, hdc); 
                } 

            } 

with the Graphics object you can do all the drawing operation you need. As far as I remember, when owner drawing the window you can possibly experience some flickering, in such a case you should consider handling WM_ERASEBACKGROUND and return false.