locked
WPF left click and hold mouse event RRS feed

  • Question

  • Hello,
     
    IS there a way in WPF to left click and hold on an image object, and once the user left clicks and holds it on the image, they can move the mouse up to zoom in, and move the mouse down, to zoom out. I am trying to find the event button on WPF but cant find it, regular C# is easy to find since its a little lightening bolt on the properties part, but if someone can tell me where it is and how the zoom in mouse can be done that would be great.

    Thank you

    Tuesday, January 13, 2009 10:55 PM

Answers

  • Well this is how I would do this;
    //In your code Global scope 
    Point referencePoint; 
     
    //In main thread 
    image1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(image1_PreviewMouseLeftButtonDown); 
    image1.MouseMove += new MouseEventHandler(image1_MouseMove); 
     
    //Then these are the event handler 
    private void image1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
          referencePoint = e.GetPosition(image1); 
     
     
    private void image1_MouseMove(object sender, MouseEventArgs e) 
        if (e.LeftButton == MouseButtonState.Pressed) 
        { 
             Point mousePosition = e.GetPosition(); 
             //Compare the coordinates here with referencePoint and Zoom in or out 
        } 

    This is just a draft and by no means been tested for working.
    For zooming in and out, I have never done it, but can think of a few ways so hopefully someone else can jump in and help you there.

    Hope this clears more for you.

    noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, January 14, 2009 5:00 PM
    • Marked as answer by Jim Zhou - MSFT Tuesday, January 20, 2009 10:10 AM
    Wednesday, January 14, 2009 6:12 AM
  • Hi,
    Yes absolutely you can. You need to catch PreviewMouseLeftButtonDown and MouseMove Events. When the user mouse left button is down you need to perform HitTest to see whether its image or not. If its image you will save the coordinates. When the user will move the mouse, Compare the coordinate difference earlier and now. Perform the zooming based on the coordinate difference.

    I was brief but I think it will give you a better picture. Please feel free to ask further.

    Thank you,
    Adil
    Adil - MCP
    • Proposed as answer by Adil Mughal Wednesday, January 14, 2009 4:52 PM
    • Marked as answer by Jim Zhou - MSFT Tuesday, January 20, 2009 10:10 AM
    Wednesday, January 14, 2009 4:52 PM

All replies

  • Well there are several events here. The way I would handle it would be:
    1. On MouseMove event I would check to see if the LeftButton is pressed
    2. If yes, the I would check to see if the e.MouseDevice.DirectlyOver equals what you want
    3. If yes, then do what you want (zoom in or out when the X-axis is increased or decreased)

    As far as I know, there is no single event you can use that does all, unless you hook your image up in other ways.

    Hope this helps.

    noorbakhsh

    Wednesday, January 14, 2009 12:28 AM
  • Cool Thanks Noor,

    Can you help me in addition to this, I get what you are saying, I need several events in this case, one event to leftclickdown and the other event to move mouse, is that correct? if so how would I incorporate that to zoom in, for example, would it be if mouse left button pressed down and hold, go to next event, then in next event if move mousebutton = true, then zoom in, does that make sense, how could I incorporate that in code now? lol, sorry, if I am confusing you but the main thing is able to zoom in if you left click and hold it then you can zoom in and out using the mouse going up or down.  But the way you describe sounds correct, can you post an example of what you are describing to help me understand it more.

    Thank you
    Wednesday, January 14, 2009 1:35 AM
  • Well this is how I would do this;
    //In your code Global scope 
    Point referencePoint; 
     
    //In main thread 
    image1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(image1_PreviewMouseLeftButtonDown); 
    image1.MouseMove += new MouseEventHandler(image1_MouseMove); 
     
    //Then these are the event handler 
    private void image1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
          referencePoint = e.GetPosition(image1); 
     
     
    private void image1_MouseMove(object sender, MouseEventArgs e) 
        if (e.LeftButton == MouseButtonState.Pressed) 
        { 
             Point mousePosition = e.GetPosition(); 
             //Compare the coordinates here with referencePoint and Zoom in or out 
        } 

    This is just a draft and by no means been tested for working.
    For zooming in and out, I have never done it, but can think of a few ways so hopefully someone else can jump in and help you there.

    Hope this clears more for you.

    noorbakhsh
    • Proposed as answer by noorbakhsh Wednesday, January 14, 2009 5:00 PM
    • Marked as answer by Jim Zhou - MSFT Tuesday, January 20, 2009 10:10 AM
    Wednesday, January 14, 2009 6:12 AM
  • Hello Noor,

    Thank you for your help,

    Hope someone else can shed some light on this as well?
    Wednesday, January 14, 2009 4:31 PM
  • Hi,
    Yes absolutely you can. You need to catch PreviewMouseLeftButtonDown and MouseMove Events. When the user mouse left button is down you need to perform HitTest to see whether its image or not. If its image you will save the coordinates. When the user will move the mouse, Compare the coordinate difference earlier and now. Perform the zooming based on the coordinate difference.

    I was brief but I think it will give you a better picture. Please feel free to ask further.

    Thank you,
    Adil
    Adil - MCP
    • Proposed as answer by Adil Mughal Wednesday, January 14, 2009 4:52 PM
    • Marked as answer by Jim Zhou - MSFT Tuesday, January 20, 2009 10:10 AM
    Wednesday, January 14, 2009 4:52 PM
  • Hello Adil,

    Yes that actually makes sense, let me try to refrase it in terms that atleast i know I understand, correct me if I am wrong, if mouseleftbuttondown = true, and mousemove events = null or maybe not equal to picture coordinates, then += 2, which is zoom in 2 in height and width correct? if not let me know, actually if you can give me an example that would also help to with the mouse left button down + hold to zoom in and out.

    Thank you
    Wednesday, January 14, 2009 5:10 PM