嘿伙计们(我正在研究这个由缩放功能组成的项目。现在,我正面临这个问题,我使用的缩放功能需要四个鼠标事件,鼠标滚轮,鼠标左键按钮但是,我试图让它像点击按钮一样,最终可以执行缩放功能。但是,我在使用按钮调用鼠标事件功能时遇到了问题。这是代码,请帮帮我(:非常感谢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;
}
同样,我的问题是如何在按钮被点击时执行这四个鼠标事件功能,而不是在鼠标被带到图像时能够直接缩放。对于您的信息,称为图像的名称(图像)。我尝试过绑定方法,不幸的是它不起作用;(
的问候。
答案 0 :(得分:1)
设置bool
例如:
变化:
imagePicture.CaptureMouse();
为:
buttonIsDown = true;
imagePicture.CaptureMouse();
和
imagePicture.ReleaseMouseCapture();
为:
imagePicture.ReleaseMouseCapture();
buttonIsDown = false;
然后您可以使用:
进行测试if (buttonIsDown)
哪种方法可能会更好。
答案 1 :(得分:0)
您可以让按钮调用外部DLL(user32.dll)来模仿真实的鼠标事件。 首先在代码之上添加:
using System.Runtime.InteropServices;
然后在你班上有这些行:
[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;
最后有这样的功能模仿当前光标位置的“真实”点击:
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);
}
(取自here,将代码复制为镜像以防链接无法使用)