Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I'm trying to pan an image that I drew to a panel. The thing is in order to pan it, I need to keep track of the mouse location once the user pressed down the left mouse button and move the button.

Well, the problem is when the mouse moves how can I determine the start and end location of the mouse? If it was a mouse down event, I could get the starting location when the mouse is pressed and the end location when it is released. But with the mouse move action, how can one tell the start and end of these small movements that I'm making???

Here is my code if it helps to see what I'm talking about

private void PanPanel_MouseMove(object sender, MouseEventArgs e)
{
        if(e.Button == MouseButtons.Left)
        {
            panStartPoint = e.Location;
            panEndPoint = .... // how do I get this point?
        }

}
Posted

You should handle 3 events:


  1. MouseDown: in the event handler you save the starting position for the pan
  2. MouseMove: in the event handler you update the view on screen
  3. MouseUp: in the event handler you process the pan using the current mouse position and the one previously saved
 
Share this answer
 
Use MouseDown to start the move - mark that you are moving, start location
Use MouseMove to pan the control, if MouseDown has happened.
Use MouseUp to end the pan.

[edit]
Sorry, that was a little blunt. It has all the info you need, but could be better explained.

On MouseDown, you mark that you are moving and record the position at which you started.

On MouseUp, you mark you are not moving any more.

On MouseMove, check if you are moving, and if so adjust the image position by the difference between the start and the current position (or the last position and the current - up to you).

You will get a lot of mouse moves, each with the new current position.
[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900