0

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?

msosa
  • 5
  • 1
  • 4

3 Answers3

1

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
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you for reply. In meanwhile i solved the problem, on similar way. I just added handlers for Label1 on Suad's code. – msosa Aug 03 '13 at 13:59
0

Hello I have some example source to make form moveable

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`

I hope this can help you

Cheers :)

  • Thanks for reply, but this is not the answer. I already used your code to make form movable and moving form itself is not the problem. Problem is that it won't move if i click on label. – msosa Aug 03 '13 at 13:21
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
Andre Silva
  • 4,782
  • 9
  • 52
  • 65