移动表单,单击Label vb.net

时间:2013-08-03 13:09:32

标签: vb.net label click-through

我有简单的形式没有边框,没有标题栏。它上面只有一个标签显示秒表。我需要通过在窗体上的任意位置单击鼠标来移动窗体,然后拖动。

我解决了这个问题,但问题是当我点击标签所占据的表格时,表格不会移动。换句话说,我只需要看标签,没有任何其他功能。如何点击标签?

3 个答案:

答案 0 :(得分:1)

在这个网站上有already an answer,但这是在C#中,所以我在这里重复这个答案,但在VB.NET中翻译。如果您认为这是有用的,请不要提出回答的问题....

这里需要注意的重要事项是还要为Label1处理mousedown而不仅仅是表单

Public Class Form1

    <DllImportAttribute("user32.dll")> _
    Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
    End Function

    <DllImportAttribute("user32.dll")> Public Shared Function ReleaseCapture() As Boolean
    End Function

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown, Label1.MouseDown
        Const WM_NCLBUTTONDOWN As Integer = &HA1
        Const HT_CAPTION As Integer = &H2

        If e.Button = MouseButtons.Left Then
            ReleaseCapture()
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
        End If

    End Sub
End Class

答案 1 :(得分:0)

您好我有一些示例来源使表单可移动

Public Class Form1 
Dim drag As Boolean 
Dim mousex As Integer 
Dim mousey As Integer Private Sub 
Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown drag = True 
mousex = Windows.Forms.Cursor.Position.X - Me.Left 
mousey = Windows.Forms.Cursor.Position.Y - Me.Top 
End Sub Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 
If drag Then 
Me.Top = Windows.Forms.Cursor.Position.Y - mousey 
Me.Left = Windows.Forms.Cursor.Position.X - mousex 
End If 
End Sub 
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 
Me.MouseUp`enter code here`

我希望这可以帮到你

干杯:)

答案 2 :(得分:0)

Public Class Form1
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As  System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown, Label1.MouseDown
    drag = True
    mousex = Windows.Forms.Cursor.Position.X - Me.Left
    mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp, Label1.MouseUp
    drag = False
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove, Label1.MouseMove
    If drag Then
        Me.Top = Windows.Forms.Cursor.Position.Y - mousey
        Me.Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub
End Class