Question

I know this would work If I wasn't using a web browser filling up my form.

Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)
    '--- Alter the return value of WM_NCHITTEST when the ALT key is down
    If m.Msg = &H84 AndAlso (Control.ModifierKeys And Keys.Alt) <> 0 Then
        '--- Turn HTCLIENT into HTCAPTION
        If m.Result = 1 Then m.Result = 2
    End If
End Sub

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

    Timer1.Enabled = True
    Timer1.Interval = 2500
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
    Timer1.Enabled = False
    drag = False
End Sub

However I found a program for Windows that has that Linux Alt+Drag window functionality called AltWindowDrag I know I can use Process.Start to run the program as I did below...

Process.Start(Application.StartupPath & "\AltWindowDrag\AltWindowDrag.exe")

However the problem with this is when I close my app, AltWindowDrag is still running. Anyway I can have AltWindowDrag close when my form closes?

Any help would be greatly appreciated.

EDIT!

A dude on another forum helped me out with this, so for others that are experiencing the same problem. Here's the code.

Dim proc As Process

Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

    proc = Process.Start(Application.StartupPath & "\AltWindowDrag\AltWindowDrag.exe")
End Sub

Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

    If Not proc Is Nothing Then

        If Not proc.HasExited Then

            proc.Kill()
        End If
    End If
End Sub
Was it helpful?

Solution

It is a pretty simple trick, Windows asks your app what was clicked when the mouse goes down. You can simply lie and tell it that the caption bar was clicked instead of the client area. Which makes Windows automatically move the window when you move the mouse. Paste this code into your form class:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.WndProc(m)
    '--- Alter the return value of WM_NCHITTEST when the ALT key is down
    If m.Msg = &H84 AndAlso (Control.ModifierKeys And Keys.Alt) <> 0 Then
        '--- Turn HTCLIENT into HTCAPTION
        If m.Result = 1 Then m.Result = 2
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top