new Moving function

  • Thread starter Thread starter willem
  • Start date Start date
W

willem

Hi,

I want move my form with mouse when I press button over a different position
from normal bar of title.

I set MouseUp, MouseDown and, MouseMove events and all works good.

is it possible work with a more intelligent method ?

When I move a normal form only its border is visible.
How can I do this effect in my personal moving functions ?

Thanks a Lot
 
Hi,
To move the windows by yourself, you have to implement mousemove. To make
sure that you drag from your form, you have
to override MouseDown. Therefore, implement 3 methods is the only way to
move your form.
To have the border of the windows move, you should draw a rectangle with
Graphics object in MouseMove Event.
 
There is a clever way to achieve this, and have it work the same
way as when the window is moved using the title bar.

The trick is to override the WndProc method of the form and
handle the WM_NCHITTEST message and return HTCAPTION
for the appropriate mouse coordinates.

As a demo, create a new form and add this code to it:

protected override void WndProc(ref Message m)
{
// from WinUser.h
const int WM_NCHITTEST = 0x0084;
const int HTCAPTION = 2;

if (m.Msg == WM_NCHITTEST)
{
Point screenPoint = new Point((int)m.LParam % 65536,
(int)m.LParam / 65536);
Point clientPoint = PointToClient(screenPoint);

// Check that the user clicked in the area to be
// used for moving the window, in this case a circle
// of radius 50 centered at coordinates 100, 100
if (Math.Pow(clientPoint.X - 100, 2) +
Math.Pow(clientPoint.Y - 100, 2) < 50 * 50)
{
m.Result = (IntPtr)HTCAPTION;
return;
}
}
base.WndProc(ref m);
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Blue, 50, 50, 100, 100);
}

This obviously depends on how Windows handles messages, so it
probably isn't 100% "future proof". But right now it works great.

- Magnus
 
Back
Top