C# 设置窗体最大化,以及窗体最大化时的坐标
reference:
https://www.cnblogs.com/adandelion/archive/2008/04/03/1136198.html
protected override void WndProc(ref Message m)
{
if (m.Msg == (int)WM.WM_GETMINMAXINFO)
{
MINMAXINFO mmi = (MINMAXINFO)m.GetLParam(typeof(MINMAXINFO));
//mmi.ptMinTrackSize.x = this.MinimumSize.Width;
//mmi.ptMinTrackSize.y = this.MinimumSize.Height;
//if (this.MaximumSize.Width != 0 || this.MaximumSize.Height != 0)
//{
// mmi.ptMaxTrackSize.x = this.MaximumSize.Width;
// mmi.ptMaxTrackSize.y = this.MaximumSize.Height;
//}
mmi.ptMaxPosition.x = _location.X;
mmi.ptMaxPosition.y = _location.Y;
System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, true);
}
base.WndProc(ref m);
}
private void _UpdateMaxSizeInfo()
{
try
{
// 获取当前窗体所在的屏幕
Screen screen = Screen.FromControl(this);
// 获取屏幕的工作区大小(排除了任务栏和停靠窗口的空间)
Rectangle workingArea = screen.WorkingArea;
//MessageBox.Show($"工作区大小: 宽度={workingArea.Width}, 高度={workingArea.Height}");
// 获取整个屏幕的大小
//Rectangle bounds = screen.Bounds;
//MessageBox.Show($"屏幕总大小: 宽度={bounds.Width}, 高度={bounds.Height}");
var x = workingArea.Location.X + index * workingArea.Width / count;
x = index * workingArea.Width / count;
_location = new Point(x, 0);
this.MaximumSize = new Size(workingArea.Width/count, workingArea.Height);
}
catch (Exception ex)
{
ex.ToErrorObject().Show();
}
}
private void BarControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(this.WindowState==FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
_UpdateMaxSizeInfo();
this.WindowState = FormWindowState.Maximized;
}
}
注意WndProc中在处理WM_GETMINMAXINFO后,可以继续运行 base.WndProc(ref m)
不会影响,可能系统默认不修改其中的内容