允许用户移动无边框窗口

时间:2013-06-30 15:45:18

标签: vb.net

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

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

4 个答案:

答案 0 :(得分:9)

引入一个布尔变量,如果当前拖动了表单,则保存状态,以及保存拖动起始点的变量。然后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

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

被盗

答案 1 :(得分:3)

所有'简单'的VB答案使我的表格在多个屏幕的地方跳跃。所以我用C#中的相同答案得出了它,它就像一个魅力:

Public Const WM_NCLBUTTONDOWN As Integer = 161
Public Const HT_CAPTION As Integer = 2

然后

 <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

最后

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

答案 2 :(得分:1)

Dim offSetX As Integer
Dim offSetY As Integer

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Me.Location = New Point(Cursor.Position.X - offSetX, Cursor.Position.Y - offSetY)
End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    offSetX = PointToClient(Cursor.Position).X
    offSetY = PointToClient(Cursor.Position).Y
    Timer1.Enabled = True
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    Timer1.Enabled = False
End Sub

这是一种略显邋x的方式xD

希望它虽然有帮助=]

答案 3 :(得分:1)

另一种方法是处理WM_NCHITTEST message。这允许您让表单的某些部分响应鼠标事件,因为标题栏,边框等将用于带边框的窗口。例如,如果表单上有标签并且在HTCAPTION处理程序中返回WM_NCHITTEST,则可以通过拖动此标签来移动表单,就像通过拖动移动常规窗口一样在它的标题栏上。有关示例代码,请参阅此Stack Overflow question