0

My form is set to bordeless, and I found code that allows me to drag the form around by simply clicking someone on the form and dragging the window about.

Dim IsDraggingForm As Boolean = False
Private MousePos As New System.Drawing.Point(0, 0)

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
    If e.Button = MouseButtons.Left Then
        IsDraggingForm = True
        MousePos = e.Location
    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 IsDraggingForm = False
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
    If IsDraggingForm Then
        Dim temp As Point = New Point(Me.Location + (e.Location - MousePos))
        Me.Location = temp
        temp = Nothing
    End If
End Sub

The problem is that my form is mostly full of control objects such as Labels, and a ListBox that have no visible borders. Attempting to drag the window clicked on these control objects does not allow this.

daily001
  • 23
  • 3
  • 2
    This is UI design mistake. Your user will never figure out how the move the window. He's not going to click on a Label or ListBox, those are not controls that say "click me to drag the window". Somewhat inevitably, your code cannot figure it out either. Make it obvious, draw a caption. – Hans Passant Sep 19 '15 at 16:22

2 Answers2

0

A solution could be to enable KeyPreview and set a keyboard short-cut to enable moving the Form (you can use Control.MousePosition to follow the mouse) on the KeyDown event and release on KeyUp.

If you're definitely after moving everything with a Click, you can set a callback from using PInvoke ( Global mouse event handler ) checking the location of the mouse is within your Form area and the Form is in focus and then do the same job with Form.Location and Control.MousePosition.

Community
  • 1
  • 1
Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
0

Use the following code:- (After removing your code for dragging)

<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 Object_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles <Object>.MouseDown
If e.Button = MouseButtons.Left Then
    ReleaseCapture()
    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End If

End Sub

Replace <Object> (8th Line, after 'Handles') with the name of the object you want as the handle for dragging.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52