我正在使用Windows窗体处理应用程序的UI。在咨询this question和this one之后,我制作了可调整大小的无边框格式。
但是现在,我在最大化状态下遇到了一个问题,也就是说,如果我将鼠标移动到边缘,我仍然可以调整窗体的大小。如何在最大化状态下使表单冻结以防止此大小调整?
使表格无边框的代码
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;
}
}
退出,最大化/恢复,最小化的代码
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;
}
答案 0 :(得分:0)
WndProc方法中的代码拦截WM_NCHITTEST(客户端窗口命中测试)消息,并根据鼠标的位置设置结果(例如鼠标正在击中边缘)。然后,这些结果将用于触发其他功能。
如果您不希望在窗口处于最大化状态时发生此功能,请绕过它。
将代码(在WndProc中)包装成类似......
if (this.WindowState != FormWindowState.Maximized)
{
// existing code
}