tags:

views:

30

answers:

2

Hey guys (: I'm currently working on this project which consists of zoom function. Now, I am facing this problem where the zoom function that used by me required four mouse events, which are Mouse wheel, Mouse left button up, down etc. However, I am trying to make it like when a button is clicked, the zoom function can be carried out eventually. However, I faced problem in calling mouse event functions using button. Here is the code, please help me out (: thanks alot DDDDD

private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    imagePicture.CaptureMouse(); 
    var tt = (TranslateTransform)((TransformGroup)imagePicture.RenderTransform).Children.First(tr => tr is TranslateTransform); 
    start = e.GetPosition(border); 
    origin = new Point(tt.X, tt.Y);

}

private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    imagePicture.ReleaseMouseCapture(); 
}

private void image_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (!imagePicture.IsMouseCaptured) return;

    var tt = (TranslateTransform)((TransformGroup)imagePicture.RenderTransform).Children.First(tr => tr is TranslateTransform); 
    Vector v = start - e.GetPosition(border); 
    tt.X = origin.X - v.X; 
    tt.Y = origin.Y - v.Y; 
}

private void image_mousewheel(object sender, MouseWheelEventArgs e) 
{
    TransformGroup transformGroup = (TransformGroup)imagePicture.RenderTransform; 
    ScaleTransform transform = (ScaleTransform)transformGroup.Children[0]; 
    RenderOptions.SetBitmapScalingMode(imagePicture, BitmapScalingMode.NearestNeighbor);

    double zoom = e.Delta > 0 ? .01 : -.01;

    transform.ScaleX += zoom; transform.ScaleY += zoom;
}

Again, my question is How am i going to execute those four mouse event function ONLY WHEN THE BUTTON IS CLICKED, instead of being able to zoom straight when the mouse is brought to the image. For you info, the name of the image is called(image). I've tried binding method, unfortunately it doesn't work;(

Regards.

+1  A: 

Rather than setting ImageCapture and testing for that, set a bool e.g.:

change:

imagePicture.CaptureMouse(); 

to:

buttonIsDown = true;
imagePicture.CaptureMouse(); 

and:

imagePicture.ReleaseMouseCapture();

to:

imagePicture.ReleaseMouseCapture();
buttonIsDown = false;

then you can test using:

if (buttonIsDown)

Which might work better.

Jackson Pope
A: 

You can have the button invoke external DLL (user32.dll) to imitate real mouse event. First add this on top of the code:

using System.Runtime.InteropServices;

Then have those lines in your class:

   [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;

And finally have such function to imitate "real" click in current cursor position:

public void DoMouseClick()
{
      //Call the imported function with the cursor's current position
      int X = Cursor.Position.X;
      int Y = Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

(Taken from here, copied the code as mirror in case the link won't be available)

Shadow Wizard