I'm creating a custom drawn window by specifying border style NONE and custom processing of WM_NCHITTEST. I've defined some area as 'my window caption' and returning HTCAPTION result for WM_NCHITTEST in this area. When window is in normal state the behavior is expected by me. The window can be moved by dragging 'my window caption' and can be maximized by double clicking on it.
The problem is with the behavior of my window in maximized state. I still returning HTCAPTION result for WM_NCHITTEST in area of 'my window caption' and window can be restored to original size by double clicking on it again, but it's also still can be moved and this isn't what I want. What should I do to fix such behavior?
Fix:
protected override void WndProc(ref Message m)
{
if(m.Msg == WM_NCHITTEST)
{
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
pos = this.PointToClient(pos);
if(HitTestForNC(ref m, pos))
{
if(WindowState != FormWindowState.Maximized || m.Result != (IntPtr)HitTestValues.HTCAPTION)
{
return;
}
}
}
else if(m.Msg == WM_GETMINMAXINFO)
{
base.WndProc(ref m);
MinMaxInfo mmi = (MinMaxInfo)Marshal.PtrToStructure(m.LParam, typeof(MinMaxInfo));
mmi.ptMaxPosition = Screen.FromControl(this).WorkingArea.Location;
mmi.ptMaxSize = Screen.FromControl(this).WorkingArea.Size;
Marshal.StructureToPtr(mmi, m.LParam, false);
return;
}
base.WndProc(ref m);
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Message m = new Message();
if(HitTestForNC(ref m, e.Location))
{
if(m.Result == (IntPtr)HitTestValues.HTCAPTION && WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
return;
}
}
}
base.OnMouseDoubleClick(e);
}
HitTestForNC method is responsible for evaluation of hit test result on my custom drawn form. The implementation may look ugly, but it's pretty simple.