我正在为使用 Windows 窗体的应用程序开发 UI。在咨询了这个问题和这个问题之后,我制作了一个可调整大小的无边界表格。
我有一台多屏电脑。当我在主屏幕上最大化时,表单通常会最大化。如果我将表单完全移动到我的第二个屏幕然后使用最大化,表单就会消失。经过调查,我发现即使第二个屏幕的属性“this.MaximizedBounds”是正确确定的,即当我使用该语句时 {X=-1440,Y=0,Width=1440,Height=900}
this.WindowState = FormWindowState.Maximized;
表格在 {X=-2880,Y=0,Width=1440,Height=900} 处绘制,好像由于某种原因绘制方向相反?鉴于此,我知道我可以以编程方式确定绘制表单,但我想知道我是否缺少设置一些附加属性以确保绘制方向正确?
以编程方式最大化表单的问题是表单的状态仍然是 FormWindowState.Normal 并且我无法将其更改为 FormWindowState.Maximized 而不会导致 from 在错误的位置重绘
使表单无边界的代码
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;
}