[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Move a form without a title bar in C#

[Move a form without a title bar in C#]

When the user presses the mouse down over the green lblMoveForm Label in the form's upper right corner, the following event handler executes.

// On left button, let the user drag the form. private void lblMoveForm_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // Release the mouse capture started by the mouse down. lblMoveForm.Capture = false; // Create and send a WM_NCLBUTTONDOWN message. const int WM_NCLBUTTONDOWN = 0x00A1; const int HTCAPTION = 2; Message msg = Message.Create(this.Handle, WM_NCLBUTTONDOWN, new IntPtr(HTCAPTION), IntPtr.Zero); this.DefWndProc(ref msg); } }

The code sets the control's Capture property to false to release the mouse capture that was started by pressing the mouse button down.

Next the program makes a Message with message WM_NCLBUTTONDOWN and wParam set to HTCAPTION, and calls the form's DefWndProc method to process the message. This is the same message Windows sends to the form when the user presses the mouse down over the form's title bar so it starts a form move. The rest is automatic. As far as the form is concerned, the user is dragging the form's title bar to move the form.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.