469,072 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,072 developers. It's quick & easy.

Moving a form via a PictureBox

Hi !

I've got this strange problem :

When I press the left-button and move my mouse, my form moves just as
intended. But if I make big movements, the cursor shifts slightly. And
it ends with the mouse cursor completly out of the form, which is not
what I want, of course.

Here's the relevant snippet of my code :

private int lastX, lastY;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(this.pictureBox_grip.Capture)
{
this.Location = new Point(this.Location.X + MousePosition.X -
this.lastX, this.Location.Y + MousePosition.Y - this.lastY);
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}
}

I also tried to use (e.X,e.Y) instead of MousePosition but the result
is worse : the form flickers a lot and the cursor shifts at once.

What am I doing wrong ?

Any help would be appreciated... It's driving me mad.

Nov 16 '05 #1
3 5549
Hi Robert,

maybe you should try it with:

private int mouseStartX, mouseStartY;
private int formStartX , formStartY;
private bool FormDragging = false;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.mouseStartX = MousePosition.X;
this.mouseStartY = MousePosition.Y;
this.formStartX = this.Location.X;
this.formStartY = this.Location.Y;
FormDragging = true;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (FormDragging)
{
this.Location = new Point(
this.formStartX + MousePosition.X - this.mouseStartX,
this.formStartY + MousePosition.Y - this.mouseStartY
);
}
}

private void pictureBox_grip_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
FormDragging = false;
}

HTH,
Stefan
Robert Lochon schrieb:
Hi !

I've got this strange problem :

When I press the left-button and move my mouse, my form moves just as
intended. But if I make big movements, the cursor shifts slightly. And
it ends with the mouse cursor completly out of the form, which is not
what I want, of course.

Here's the relevant snippet of my code :

private int lastX, lastY;

private void pictureBox_grip_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}

private void pictureBox_grip_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(this.pictureBox_grip.Capture)
{
this.Location = new Point(this.Location.X + MousePosition.X -
this.lastX, this.Location.Y + MousePosition.Y - this.lastY);
this.lastX = MousePosition.X;
this.lastY = MousePosition.Y;
}
}

I also tried to use (e.X,e.Y) instead of MousePosition but the result
is worse : the form flickers a lot and the cursor shifts at once.

What am I doing wrong ?

Any help would be appreciated... It's driving me mad.

Nov 16 '05 #2
Thanks a lot ! It works perfectly.

I understand that your solution is to compute the new position of the
form from its position when the mouse button was first pressed. Whereas
my solution was to compute its new position from its last position.
It's still not clear to me why my way was the wrong one... :o/

Nov 16 '05 #3
Hi Robert,

even though i'm not perfectly sure I can remember having the same
problem myself in another programming language.
The problem there was caused by multithreading, because new mouse events
were raised before the execution of the current one was finished
(redrawing a form is a pretty expensive process).
This would also explain why you encountered the problems only with fast
mouse movements and not with slow ones.

If you want you can try to secure your code by using locks or some other
form of semaphores.

HTH,
Stefan
Robert Lochon schrieb:
Thanks a lot ! It works perfectly.

I understand that your solution is to compute the new position of the
form from its position when the mouse button was first pressed. Whereas
my solution was to compute its new position from its last position.
It's still not clear to me why my way was the wrong one... :o/

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Serdar C. | last post: by
18 posts views Thread by Ed Bitzer | last post: by
2 posts views Thread by pat.saunders | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.