msosa msosa - 6 months ago 67
Vb.net Question

Move form, click through Label vb.net

i have simple form without border and without title bar. There is only single label on it showing stopwatch. I need form to be movable by clicking mouse anywhere on form and then drag.

I solved that, but the problem is when i click on form on spot occupied by label, form will not move. In another words, i need Label only to be seen, not having any other function. How do i make label click through?

Answer

There is already an answer for this in this site, but that was in C#, so I repeat that answer here but translated in VB.NET. If you think that this is useful don't esitate the upvote that answer also....

The important thing to note here is the handling of the mousedown also for the Label1 and not only for the form

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