鼠标左键事件和打开文件对话框 [英] Mouse left button up event and openfiledialog

查看:147
本文介绍了鼠标左键事件和打开文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格几张图片,然后当我点击一个按钮,一个打开文件对话框出现。(当然,在图像)

I have few images in a grid, then when i click a button, a "open file dialog" comes up.(of course, over the images)

Microsoft.Win32.OpenFileDialog dlgOpenFiles =新Microsoft.Win32.OpenFileDialog();
dlgOpenFile.DoModal();

Microsoft.Win32.OpenFileDialog dlgOpenFiles = new Microsoft.Win32.OpenFileDialog(); dlgOpenFile.DoModal();

图像有LeftButtonUp事件连接。问题是,如果我选择通过双击某个文件单击它,打开文件对话框关闭(这是好的),但除此之外,点击文件后面的图像接收LeftButtonUp消息,这是不好的。

The images have a LeftButtonUp event attached. The problem is that if i select a file by double clicking it, the open file dialog closes(which is good), but besides that, the image behind the clicked file is receiving a LeftButtonUp message which is not good at all.

我使用WPF / C#/ VS2010

I am using wpf/c#/vs2010

推荐答案

最简单的方式来获得围绕它,是每当你需要一个处理程序按钮事件,添加一个按钮按下事件,执行 CaptureMouse()在里面。现在,在你的按钮事件,你可以忽略所有的事件,这些事件发生没有 IsMouseCaptured 。并确保不要忘记 ReleaseMouseCapture()

The simple way to get around it, is whenever you need a handler to button-up event, add a button-down event, do CaptureMouse() in it. Now in your button-up event you can ignore all events, which happen without IsMouseCaptured. And make sure not to forget ReleaseMouseCapture():

private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    CaptureMouse();
}

private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (!IsMouseCaptured)
        return;
    ReleaseMouseCapture();
    var dlg = new OpenFileDialog();
    var res = dlg.ShowDialog(this);
    // ...
}

这篇关于鼠标左键事件和打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆