Enhanced: Drag and move WinForms (without having a titlebar)

Yesterday my colleague Alexander Jung read the articel I posted yesterday about dragging WinForms. And he showed me a solution for dragging windows that is much smarter than the solution I posted.

So I don’t want to keep back that solution.

Instead of implementing a drag routine for the WinForm, we can fool the system so that Windows thinks that the hole WinForm is a Caption (TitleBar). This can be done by intercepting the Windows Message WM_NCHITTEST and returning HTCAPTION if the user tries to drag the window.

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

///
/// Handling the window messages
///
protected override void WndProc(ref Message message)
{
    base.WndProc(ref message);

    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
        message.Result = (IntPtr)HTCAPTION;
}

The example solution can be downloaded using the following link.
Enhanced: Drag and move WinForms (without having a titlebar)

About these ads

2 Responses to “Enhanced: Drag and move WinForms (without having a titlebar)”

  1. mrcouthy Says:

    How to do it, if we want certain control in the form to act as title bar.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 103 other followers

%d bloggers like this: