2009-03-02 37 views
4

默認的MDI父控件有一個大的「桌面」區域,可以顯示多個子窗體。用戶可以將表單拖動到此桌面區域的邊緣,以便大多數子表單不在屏幕上。 (一個滾動條出現在MDI父項中)我不喜歡這個特性。有沒有辦法鎖定桌面區域的邊緣,以便子窗體保持完全可見?Winforms MDI「桌面」區域邊界

回答

3
  1. 禁用MDI窗口滾動條
  2. 掛鉤所有子窗口的OnMove事件。如果窗口移動到邊界之外,沿着x和y「回彈」它,直到它回到父窗口的內部。
+1

看我下面的代碼包含的代碼做到這一點。 – Jeff 2009-03-02 21:19:08

3

爲了澄清,你說的是MDI客戶端的「桌面」區域是客戶區。

您可以處理子窗體的調整大小/移動事件處理程序,然後在超出MDI客戶區的邊界時調整/限制子的移動。

5

我用來實現我上面選擇的答案代碼:

Public alreadyMoved As Boolean = False 
Public Const HEIGHT_OF_MENU_STATUS_BARS As Integer = 50 
Public Const WIDTH_OF_MENU_STATUS_BARS As Integer = 141 
Private Sub Form_Move(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles MyBase.Move 
    If Not alreadyMoved Then 
     alreadyMoved = True 

     'If I'm over the right boundry, drop back to right against that edge 
     If Me.Location.X + Me.Width > _ 
      MdiParent.ClientRectangle.Width - WIDTH_OF_MENU_STATUS_BARS Then 
      MyBase.Location = New System.Drawing.Point(_ 
       (MdiParent.ClientRectangle.Width - Me.Width - _ 
       WIDTH_OF_MENU_STATUS_BARS), MyBase.Location.Y) 
     End If 

     'If I'm over the bottom boundry, drop back to right against that edge 
     If Me.Location.Y + Me.Height > _ 
      MdiParent.ClientRectangle.Height - HEIGHT_OF_MENU_STATUS_BARS Then 
      MyBase.Location = New System.Drawing.Point(_ 
       MyBase.Location.X, (MdiParent.ClientRectangle.Height - _ 
       Me.Height - HEIGHT_OF_MENU_STATUS_BARS)) 
     End If 

     'If I'm over the top boundry, drop back to the edge 
     If Me.Location.Y < 0 Then 
      MyBase.Location = New System.Drawing.Point(MyBase.Location.X, 0) 
     End If 

     'If I'm over the left boundry, drop back to the edge 
     If Me.Location.X < 0 Then 
      MyBase.Location = New System.Drawing.Point(0, MyBase.Location.Y) 
     End If 
    End If 
    alreadyMoved = False 
End Sub