我有一个主窗体窗口,它将弹出一个新的窗体窗口。我想锁定弹出窗体的位置,使窗口不能移动,它会与主窗体同时移动。(因此,如果用户拖动主窗体,弹出窗口也会随之移动)
在网站上进行了搜索,有些是这样的:
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None
我将 Locked 属性设置为 True 但这不起作用。
但我想保留边界。锁定表格的正确方法是什么?
你可以做这样的事情(取自这里):
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
base.WndProc(ref message);
}
public class Form1
{
private Form2 Form2 = new Form2();
private Point form2Location;
private Point form1Location;
private void Button1_Click(System.Object sender, System.EventArgs e)
{
form1Location = this.Location;
Form2.Show();
form2Location = Form2.Location;
}
private void Form1_Move(System.Object sender, System.EventArgs e)
{
Form2.IsMoving = true;
Point form2OffSetLocation = new Point(this.Location.X - form2Location.X, this.Location.Y - form2Location.Y);
Form2.Location = form2OffSetLocation;
Form2.IsMoving = false;
}
}
public class Form2
{
public bool IsMoving;
private void Form2_Move(System.Object sender, System.EventArgs e)
{
if (IsMoving) return;
if (staticLocation.X != 0 & staticLocation.Y != 0) this.Location = staticLocation;
}
private Point staticLocation;
private void Form2_Load(System.Object sender, System.EventArgs e)
{
staticLocation = this.Location;
}
}
我同意汉斯的观点,我想一旦你看到它看起来有多狡猾,你可能也会同意。