I have derived a class from System.Windows.Forms.Form
class to create a custom form.
I have override the method WndProc(ref m)
and process WM_NCPAINT
window message to draw the caption bar with my own customization.
I have provided the basic version of my implementation below.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WindowMessages.WM_NCPAINT: // TO draw the title bar text and buttons
On_Wm_NcPaint(ref m);
break;
case WindowMessages.WM_NCCALCSIZE: //To define the client area size of the form.
On_Wm_NcCalcSize(ref m);
break;
case WindowMessages.WM_WINDOWPOSCHANGED:
On_Wm_WindowPosChanged(ref m);
break;
case WindowMessages.WM_NCLBUTTONUP: //For handling close,minimize and maximize operations.
On_Wm_NcLButtonUp(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
private void On_Wm_WindowPosChanged(ref Message m)
{
NativeMethods.WINDOWPOS wpos = (NativeMethods.WINDOWPOS)m.GetLParam(typeof(NativeMethods.WINDOWPOS));
if ((wpos.flags & WindowMessages.SWP_NOSIZE) == 0)
{
var rect = new NativeMethods.RECT();
NativeMethods.GetWindowRect(this.Handle, ref rect);
var region = NativeMethods.CreateRectRgn(0, 0, rect.Width, rect.Height);
if (region != IntPtr.Zero)
NativeMethods.SetWindowRgn(this.Handle, region, true);
Invalidate();
return;
}
base.WndProc(ref m);
}
This works properly for me.
The problem with this is, the form flickers when resizing from the top and left side of the form.
It seems that the below things happens when I increase the form size by resizing from the left side.
- First the form is repositioning to the left side.
- Then the width of the form is increasing in the right.
This happens little slowly, so that it looks like flickering.
Note : Flickering occurs in the form. Not in the controls added to the form.
I have tried the below ways to resolve flickering.
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
this.DoubleBuffered = true;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
These approaches doesn't give any result.
This flickering doesn't occurs in the base form (System.Windows.Forms.Form)
This may occurs due to the incorrect handling of the WindowMessage WM_WINDOWPOSCHANGED.
Please share your ideas to get rid of this flickering and to get a smooth resizing.
Thanks in advance