Reputation: 488
I am working on the UI for an application using Windows Form. I made a resizable borderless form after consulting this question and this one as well.
But now, I have an issue in the maximized state, that is, if I move the mouse to the edge, I can still resize the form. How do I make the form Freeze in the maximized state to prevent this resizing?
Code to make form Borderless
protected override void WndProc(ref Message m)
{
const int RESIZE_HANDLE_SIZE = 10;
switch (m.Msg)
{
case 0x0084/*NCHITTEST*/ :
base.WndProc(ref m);
if ((int)m.Result == 0x01/*HTCLIENT*/)
{
Point screenPoint = new Point(m.LParam.ToInt32());
Point clientPoint = this.PointToClient(screenPoint);
if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
{
if (clientPoint.X <= RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)13/*HTTOPLEFT*/ ;
else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
m.Result = (IntPtr)12/*HTTOP*/ ;
else
m.Result = (IntPtr)14/*HTTOPRIGHT*/ ;
}
else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
{
if (clientPoint.X <= RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)10/*HTLEFT*/ ;
else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
m.Result = (IntPtr)2/*HTCAPTION*/ ;
else
m.Result = (IntPtr)11/*HTRIGHT*/ ;
}
else
{
if (clientPoint.X <= RESIZE_HANDLE_SIZE)
m.Result = (IntPtr)16/*HTBOTTOMLEFT*/ ;
else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
m.Result = (IntPtr)15/*HTBOTTOM*/ ;
else
m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
}
}
return;
}
base.WndProc(ref m);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x20000; // <--- use 0x20000
cp.ClassStyle |= 0x08;
return cp;
}
}
Code to quit,maximize/restore,minimize
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
if(this.WindowState==FormWindowState.Normal)
{
this.button2.Image = ((System.Drawing.Image)(Properties.Resources.Restore_Down));
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;
}
else if(this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.button2.Image = ((System.Drawing.Image)(Properties.Resources.Maximize));
}
}
private void button3_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
Upvotes: 0
Views: 404
Reputation: 2391
The code in your WndProc method is intercepting the WM_NCHITTEST (client window hit test) message and is setting the result (e.g. mouse is hitting an edge) based on the position of the mouse. These results are then used to trigger other functionality.
If you don't want this functionality to happen when your window is in a maximised state, just bypass it.
Wrap the code (inside WndProc) in something like...
if (this.WindowState != FormWindowState.Maximized)
{
// existing code
}
Upvotes: 0