正如我毫不懷疑,尼爾答案將工作花一點時間來實現它,我能夠做我想要多一點優雅與此代碼:
Private Sub myWindow_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
Dim primaryBounds As System.Drawing.Rectangle = Windows.Forms.Screen.PrimaryScreen.Bounds
Dim windowBounds As System.Drawing.Rectangle = New System.Drawing.Rectangle(CInt(Me.Left), CInt(Me.Top), CInt(Me.Width), CInt(Me.Height))
If (windowBounds.Left < 0) Then
windowBounds = New System.Drawing.Rectangle(0, windowBounds.Top, windowBounds.Width, windowBounds.Height)
ElseIf (windowBounds.Right > primaryBounds.Right) Then
windowBounds = New System.Drawing.Rectangle(primaryBounds.Right - windowBounds.Width, windowBounds.Top, windowBounds.Width, windowBounds.Height)
End If
If (windowBounds.Top < 0) Then
windowBounds = New System.Drawing.Rectangle(windowBounds.Left, 0, windowBounds.Width, windowBounds.Height)
ElseIf (windowBounds.Bottom > primaryBounds.Bottom) Then
windowBounds = New System.Drawing.Rectangle(windowBounds.Left, primaryBounds.Bottom - windowBounds.Height, windowBounds.Width, windowBounds.Height)
End If
Me.Left = windowBounds.Left
Me.Top = windowBounds.Top
End Sub
本作被拖動的窗口會停留在主窗口(整個窗口)內,但您可以輕鬆地將邊界更改爲所需的任何值。
我在說拖動窗口周圍的窗口,而不是重新調整它的大小。想想在屏幕上設置一個區域,即使屏幕較大,也只允許窗口四處移動。像拖曳容器是我能想到的最好的。 – ScottN 2009-02-20 07:13:37
我坐在這裏再讀一遍,想知道我是否問了這個問題,以及我怎麼會認爲我是在談論窗口大小而不是拖動,我甚至沒有提到「大小」或「重新大小」這個詞我的問題... – ScottN 2009-02-20 07:18:15
拖動邊界?我認爲它默認是可拖動的。你想拖動像Windows窗體中的那些句柄嗎? – 2009-02-20 15:11:37