Moving a form by clicking anywhere on it.

You've seen applications that allow you to move the form around by simply clicking anywhere on the form (not just the caption bar). Can you do this in .NET? Yes! And it's very easy. All you have to do is handle the proper windows message and the rest is easy.

Option Strict On
Option Explicit On

Imports System Imports System.Windows.Forms Imports System.Drawing
Public Class Form1 Inherits Form
Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_NCLBUTTONDOWN As Integer = &HA1 Private Const HTCAPTION As Integer = 2
Public Sub New() Me.Text = "Drag anywhere to move" AddHandler Me.MouseDown, AddressOf frmMain_MouseDown End Sub
<STAThread()> _ Shared Sub Main() Application.Run(New Form1()) End Sub
Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) 'Don't drag the sceen if it is the right button or the wheel. If e.Button = MouseButtons.Left Then ReleaseCapture() SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0) End If
End Sub
End Class


Comments have been disabled for this content.