2013-01-14 74 views
1

我有一个主窗体将弹出一个新窗体。我想锁定弹出窗体的位置,以便窗口不能移动,并且它将与主窗体同时移动。 (所以,如果用户拖拽的主要形式有它弹出移动)锁定winform位置

做的网站上搜索和一些做的是这样的:

this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None

和我有锁定的属性设置为是的,但这不起作用。

但我想保留边界。什么是锁定表单的正确方法?

+2

这违反了一些UI可用性规则。当用户最小化/最大化主窗口时爆炸非常严重。简单而直观的方法是让您的主窗口更大。将用户控件停靠或定位到右侧。 –

+0

是的,这是我想要做的。这就像一个小日志应该弹出到主窗口的一侧。什么是最好的方式来做到这一点? –

回答

1
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; 
    } 
} 

我与Hans同意这一个,我认为一旦你看到它是如何狡猾看起来你可能会同意过。

1

你可以做这样的事情(从here拍摄):

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); 
} 
+0

这个位会导致一个错误:'0×0112;'它说';预计“是C#的正确语法? –

+0

@sd_dracula出于某种原因,当我复制并粘贴时,x是一个乘号(×)。它应该编译,如果你用x代替它,如上面的编辑。 – Daniel