2014-01-24 38 views
1

我有一个没有边框和标题栏的自定义窗体。我使用面板(宽度= 1px)来模拟边框。所有的工作都很好,除了左边界和上边界。当我尝试减小窗体的大小(通过拖动它到右侧),它工作正常,但当窗体的大小== this.MinimumSize。它开始移动到右侧。我只想改变尺寸,但不要移动... 这里是我的左边的代码。我如何修改它只更改大小?通过边框自定义窗体大小调整

private void borderW_MouseDown(object sender, MouseEventArgs e) 
    { 
     Active = true; 

    } 


    private void borderW_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (Active) 
     { 
      if (e.X < 0) 
      { 
       this.Location = new Point(this.Left + e.X, this.Top); 
       this.Size = new Size(this.Width - e.X, this.Height); 
      } 
      else 
      { 
       this.Size = new Size(this.Width - e.X, this.Height); 
       this.Location = new Point(this.Left + e.X, this.Top); 
      } 
     } 
    } 

    private void borderW_MouseUp(object sender, MouseEventArgs e) 
    { 
     Active = false; 
    } 

回答

2

将此功能粘贴到您的表单中。这是一种可以防止表单移动的覆盖。

但是,您必须使用使得它只有活动的情况适合它,只要你的表格的左边是一样的form.left + form.width(从我从你的问题理解。

protected override void WndProc(ref Message m) 
{ 
const int WM_NCLBUTTONDOWN = 161; 
const int WM_SYSCOMMAND = 274; 
const int HTCAPTION = 2; 
const int SC_MOVE = 61456; 
if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE)) 
return; 
if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION) 
) 
return; 
base.WndProc(ref m); 
} 
+0

不,FormStartPostion如何影响表单位置,何时更改位置(this.Location = new Point(this.Left + eX,this.Top);) – Udjen

+1

你说得对,开始位置只在表单加载时才有影响。这个答案:http://bytes.com/topic/c-sharp/answers/279387-how-do-i-prevent-form-moving指向一个解决方案。如果你的form.left是eqal form.left +表单.width,你可以踢这个winproc来防止移动窗口。 –

+0

我该如何使用这个?我在哪里必须粘贴它? – Udjen

相关问题