0

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

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

4

3 回答 3

1

在这个网站上已经有一个答案,但那是在 C# 中,所以我在这里重复这个答案,但在 VB.NET 中翻译。如果您认为这很有用,请不要对这个答案也投赞成票....

这里要注意的重要一点是鼠标按下的处理也适用于 Label1 而不仅仅是表单

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
于 2013-08-03T13:51:00.663 回答
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`

我希望这可以帮助你

干杯:)

于 2013-08-03T13:15:37.773 回答
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
于 2014-06-16T01:18:40.390 回答