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
"willem" <wi****@libero.it> wrote in message
news:OG*************@TK2MSFTNGP12.phx.gbl...
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