I'm having trouble figuring this out. I want to move around a form using mouse coordinates, much like a tooltip moves with the cursor. In other words I want the form to maintain a fixed relative position to the cursor. Any help would be much appreciated.
The problem with this code is, that Location is screen based cordinate and e.x, e.y are client based cordinates.
it gees like this:
privatebool first = true;
void Form1_MouseMove(object sender, MouseEventArgs e)
int aX = x-e.X;
int aY = y-e.Y;
// I do not remeber if it shoud be + or -this.Location = new Point(this.Location.X-aX, this.Location.Y-aY);
x = e.X;
y = e.Y;
Thanks, but is it possible to move the form with the cursor when its not over the form? Woulkd it be easier if I was moving a child form within the area of a parent form, and how would I communication the mousemove events to the child form?
That's cruel, Luc! He's been at this for days
(and before anyone says anything, yes, you need to use the original offset, and yes, you need to know if you are supposed to move the form, and yes, yada yada yada)
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
privatevoid timer1_Tick(object sender, EventArgs e)
Point pt = PointToClient(Control.MousePosition));
if (aX != pt.X || aY != pt.Y)
aX = (pt.X - x);
aY = (pt.Y - y);
this.Location = new Point(pt.X - aX, pt.Y - aY);
x = pt.X;
y = pt.Y;
If you do something like this you get a nasty oscillating effect. I'm not sure why?
BTW: if you apply your (or my) logic to just one object, there is no way it could oscillate. And generally it is wise to test new techniques on one object only, until it works satisfactorily; this helps observation and understanding of what is going on.
really? all PointToClient does is subtract the screen location of the containing Control. I admit MSDN could have made that clearer (as usual), but if you think about it, it can't be doing anything else. So pick the right sucker before calling PointToClient.
Homework: how does a container figure out its screen location?