Moving form with MouseMove or WndProc

M

Mythran

Which *should* I use? Which is more elegant? IMO, the WndProc override is
more elegant but probably shouldn't be used for this?

Snip code below:

Variation #1:
private Point mLastMousePoint;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mLastMousePoint = new Point(e.X, e.Y);
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
this.Location = new Point(
this.Left - (mLastMousePoint.X - e.X),
this.Top - (mLastMousePoint.Y - e.Y)
);
}
}

Variation #2:
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

if (m.Msg == WM_NCHITTEST && m.Result.ToInt32() == HT_CLIENT) {
m.Result = (IntPtr) HT_CAPTION;
}
}


This is rewritten untested uncompiled code...should be enough info for the
question I want answered, but if it's not, lemme know :)

Thanks,
Mythran
 

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