Byte
Byte

Reputation: 57

Moving Borderless Forms

I need your help with some code I found on the web a long time ago. Sadly I don't remember from where it is :( To move the borderless forms in my project I use this code snipped:

protected override void OnMouseDown(MouseEventArgs e)
{
     base.OnMouseDown(e);
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
               this.Capture = false;
               Message msg = Message.Create(this.Handle, 0XA1, new IntPtr(2), IntPtr.Zero);
               this.WndProc(ref msg);
          }
}

My problem is that I don't completely understand how the code works. As far as I understand the event gets activated when a mouse button is clicked on the forms. Then follows the query, if the mouse click is a left click. And from there I don't know what the following code does :(

Upvotes: 2

Views: 468

Answers (3)

BradleyDotNET
BradleyDotNET

Reputation: 61349

At a basic level, you are sending a message to your window and having it handle it.

You are giving it a 0xA1 (WM_NCLBUTTONDOWN) and by sending a 0x02 as the parameter (HTCAPTION) you fool the process into thinking you are on the caption bar. Drags on a caption bar move the window around, hence you can drag the window by using your code.

Samples of doing this at:

C#: How to drag a from by the form and it's controls?

http://www.catch22.net/tuts/win32-tips-tricks

Upvotes: 1

GWLlosa
GWLlosa

Reputation: 24403

You're basically posting a message to the window. A little MSDN research uncovers that the message you're posting is WM_NCLBUTTONDOWN. Basically, you're telling the underlying window that the left mouse button is being held down and it needs to respond to that. That response typically happens to be dragging the window about.

Upvotes: 0

Marc Johnston
Marc Johnston

Reputation: 1286

The this.Capture=false tells the OS to stop capturing mouse events. The Message.Create creates a new message to be send to the message loop of the current application. 0xA1 is WM_NCLBUTTONDOWN; which is a non-client left-button down message. Meaning it simulated clicking the left mouse button on the missing border.

Windows then picks up the rest of the process.

Upvotes: 2

Related Questions