Moving a form via a PictureBox

R

Robert Lochon

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.
 
S

Stefan L

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
 
R

Robert Lochon

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... :blush:/
 
S

Stefan L

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
 

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