Moving a captionless form -- Revisited

C

Chris Dunaway

If I have a form in my project for which I have set the border style to
None, I can use the code below to enable moving the form by clicking
and dragging anywhere on the form:

protected override void WndProc(ref Message m)
{
const int WmNcHitTest = 0x84;
const int HtCaption = 2;

if (m.Msg == WmNcHitTest)
m.Result = new IntPtr(HtCaption);
else
base.WndProc(ref m);
}

It works quite well and the form moves smoothly.

My next question concerns having a panel on the form that completely
covers the form's surface. Is there any way to allow the same
functionality by clicking and dragging on the panel so that the entire
form moves? I would prefer to use a method similar to above rather
than mouse events. Is it possible?

Thanks,

Chris
 
C

Chris Dunaway

In answer to my own question, I discovered this:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);

private void pnlAdmin_MouseDown(object sender, MouseEventArgs
e)
{
const uint WM_SYSCOMMAND = 0x0112;
const uint SC_MOVE = 0xF010;
const int HtCaption = 2;

pnlAdmin.Capture = false;

SendMessage(this.Handle, WM_SYSCOMMAND, new IntPtr(SC_MOVE
+ HtCaption),new IntPtr(0));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top