Mouse left button up event and openfiledialog

孤人 提交于 2019-12-22 09:49:30

问题


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 = new Microsoft.Win32.OpenFileDialog(); dlgOpenFile.DoModal();

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.

I am using wpf/c#/vs2010


回答1:


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);
    // ...
}


来源:https://stackoverflow.com/questions/3005027/mouse-left-button-up-event-and-openfiledialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!