我正在使用无边框表单并使用 HTCAPTION 实现该表单,以便可以拖动它。HTCAPTION 的另一个好处是,当双击时,它可以最小化和最大化。唯一的问题是我想捕获一个事件,以便当窗口最大化时,我可以将按钮的图标从最大化按钮更改为正常模式按钮。关于如何实现这一点的任何好主意?
protected override void WndProc(ref Message m) { base.WndProc(ref m);
switch(m.Msg)
{
case Global.WM_NCHITTEST:
Point mouseCursor = PointToClient(Cursor.Position);
if (mouseCursor.X < borderSize && mouseCursor.Y < borderSize)
m.Result = (IntPtr)Global.HTTOPLEFT;
else if (mouseCursor.X < borderSize && mouseCursor.Y > Height - borderSize)
m.Result = (IntPtr)Global.HTBOTTOMLEFT;
else if (mouseCursor.X < borderSize)
m.Result = (IntPtr)Global.HTLEFT;
else if (mouseCursor.X > Width - borderSize && mouseCursor.Y < borderSize)
m.Result = (IntPtr)Global.HTTOPRIGHT;
else if (mouseCursor.X > Width - borderSize && mouseCursor.Y > Height - borderSize)
m.Result = (IntPtr)Global.HTBOTTOMRIGHT;
else if (mouseCursor.X > Width - borderSize)
m.Result = (IntPtr)Global.HTRIGHT;
else if (mouseCursor.Y < borderSize)
m.Result = (IntPtr)Global.HTTOP;
else if (mouseCursor.Y > Height - borderSize)
m.Result = (IntPtr)Global.HTBOTTOM;
else
m.Result = (IntPtr)Global.HTCAPTION;
break;
}
}
这是我想在捕获的事件中添加的代码
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
pb_max.Image = GomeeSoft.Properties.Resources.buttonmax;
}
else
{
Screen screen = Screen.FromRectangle(new Rectangle(Left, Top, Width, Height));
this.MaximumSize = screen.WorkingArea.Size;
WindowState = FormWindowState.Maximized;
pb_max.Image = GomeeSoft.Properties.Resources.buttonreturn;
}