Moving Borderless Forms...

Kris Jacyna

Member
Joined
Feb 12, 2007
Messages
20
Programming Experience
Beginner
Is there a way that a user can move a form that has no border (or titlebar) during run-time?
I have checked the WinForms FAQ and the forum and can only find code for C# to do this.
Is it posible in VB?

Thanks,

Kris
 
Here's the Winforms FAQ code translated to VB.Net:
VB.NET:
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HTCAPTION As Integer = &H2
Declare Function ReleaseCapture Lib "user32.dll" () As Int32
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Int32, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Int32
 
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
    If e.Button = MouseButtons.Left Then
        ReleaseCapture()
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End If
End Sub
 
This can also be done with only managed code (if you don't count the Win32 constant message values.. :)) by sending the message to the forms own message handler directly:
VB.NET:
Private Sub frmDiv_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Me.Capture = False
        Const WM_NCLBUTTONDOWN As Integer = &HA1
        Const HTCAPTION As Integer = &H2
        Dim m As New Message
        m.HWnd = Me.Handle
        m.Msg = WM_NCLBUTTONDOWN
        m.WParam = HTCAPTION
        m.LParam = 0
        Me.DefWndProc(m)
    End If
End Sub
 
It works the same as with Win32 SendMessage, but instead of sending the message via OS messaging subsystem which would send it right back to the application, the DefWndProc method allows to create and send the message directly into the applications own WndProc message handler. This is also where regular messages from OS is passed through, but in this case the message was created locally and initiated to achieve the expected result.
 
The constants you can get from MSDN documentation, the constant values from ApiViewer
 
Back
Top