2012-07-06 68 views
1

enter image description here如何停止將MDI子窗體拖出父窗口的邊緣?

「人」是一個MDI父窗體 「新」是MDI子窗體

我怎樣才能阻止「新」被拖到外面「人」的邊緣?

我找到工作好

protected override void OnMove(EventArgs e) 
    { 
     // 
     // Get the MDI Client window reference 
     // 
     MdiClient mdiClient = null; 
     foreach (Control ctl in MdiParent.Controls) 
     { 
      mdiClient = ctl as MdiClient; 
      if (mdiClient != null) 
       break; 
     } 
     // 
     // Don't allow moving form outside of MDI client bounds 
     // 
     if (Left < mdiClient.ClientRectangle.Left) 
      Left = mdiClient.ClientRectangle.Left; 
     if (Top < mdiClient.ClientRectangle.Top) 
      Top = mdiClient.ClientRectangle.Top; 
     if (Top + Height > mdiClient.ClientRectangle.Height) 
      Top = mdiClient.ClientRectangle.Height - Height; 
     if (Left + Width > mdiClient.ClientRectangle.Width) 
      Left = mdiClient.ClientRectangle.Width - Width; 
     base.OnMove(e); 
    } 
+2

防止它出現在那裏,或從那裏被拖到那裏? – cHao 2012-07-06 12:40:31

+0

我不希望它被拖到邊緣 – ThElitEyeS 2012-07-06 12:59:09

+0

考慮用這些類型的信息來更新你的問題。 =) – 2012-07-06 13:05:19

回答

0

MDI父窗體的代碼重新部署其新的形式有點像顯示多種形式級聯。要重新定位窗口,您必須在Form_Loading事件處理程序中設置位置。

0

設置子形成中StartPosition到CenterParent

0

隱藏MDI容器的滾動條,示例代碼:

internal sealed class NonScrollableWindow : NativeWindow 
{ 
    private readonly MdiClient _mdiClient; 

    public NonScrollableWindow(MdiClient parent) 
    { 
     _mdiClient = parent; 
     ReleaseHandle(); 
     AssignHandle(_mdiClient.Handle); 
    } 
    internal void OnHandleDestroyed(object sender, EventArgs e) 
    { 
     ReleaseHandle(); 
    } 
    private const int SB_BOTH = 3; 
    [DllImport("user32.dll")] 
    private static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow); 
    protected override void WndProc(ref Message m) 
    { 
     ShowScrollBar(m.HWnd, SB_BOTH, 0); 
     base.WndProc(ref m); 
    } 
} 

使用(在MDI父負載事件),

foreach (MdiClient control in Controls.OfType<MdiClient>()) 
     { 
      if (control != null) 
      { 
       new NonScrollableWindow(control); 
       break; 
      } 
     } 
0

在移動您可以使用代碼來檢測當前位置是否超出您想要的位置。您需要先建立父窗口的頂部,左側,底部和右側邊緣。

這樣的代碼:

BufferWidth = 10; // 10 pixel buffer to edge 
// ParentTop is the top Y co-ordinate of the parent window 

if (this.location.Y > (ParentTop-BufferWidth)) 
{ 
    int LocX = this.Location.X; 
    this.location = new Point(LocX, (ParentTop-BufferWidth)); 
} 

您將需要重複這一過程,每個邊。通過提前計算帶有緩衝區的邊,代碼可以更簡化,因爲它們只會在父窗口移動時纔會更改。

相關問題