允许用户移动无边框窗口

Jos*_* M. 3 vb.net

我有一个没有边框的表单,我希望用户能够移动.我找不到任何可以让我这样做的东西.

是否可以移动边框设置为None的窗口?

小智 10

引入一个布尔变量,如果当前拖动了表单,则保存状态,以及保存拖动起始点的变量.然后OnMove相应地移动表单.由于这已在其他地方得到解答,我只需将其复制并粘贴到此处.

Class Form1
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

http://www.dreamincode.net/forums/topic/59643-moving-form-with-formborderstyle-none/偷来


小智 5

所有“简单”的 VB 答案都使我的表单在多个屏幕上到处跳跃。所以我从 C# 中的相同答案中得出了这个,它就像一个魅力:

Public Const WM_NCLBUTTONDOWN As Integer = 161
Public Const HT_CAPTION As Integer = 2
Run Code Online (Sandbox Code Playgroud)

然后

 <DllImport("User32")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer

End Function

<DllImport("User32")> Private Shared Function ReleaseCapture() As Boolean

End Function
Run Code Online (Sandbox Code Playgroud)

最后

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If (e.Button = MouseButtons.Left) Then
        ReleaseCapture()
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 这需要更多的投票,除非有人有理由说它不好。我一直在尝试其他方法,包括我自己的方法,但它们都很滞后,而且在复杂的表格或缓慢的 PC 上往往会留下表格。这是干净和快速的。做得好。值得补充的是,您需要修改负责拖动的控件的句柄。 (2认同)