鼠标左键弹起事件和openfiledialog

发布于 2024-09-05 02:43:08 字数 310 浏览 1 评论 0原文

我的网格中只有几个图像,然后当我单击按钮时,会出现一个“打开文件对话框”。(当然,在图像上方)

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

这些图像附加了 LeftButtonUp 事件。问题是,如果我通过双击选择一个文件,打开的文件对话框将关闭(这很好),但除此之外,单击的文件后面的图像会收到一条 LeftButtonUp 消息,这根本不好。

我正在使用 wpf/c#/vs2010

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-09-12 02:43:09

解决这个问题的简单方法是,每当您需要处理按钮向上事件时,添加按钮向下事件,然后在其中执行 CaptureMouse() 。现在,在按钮弹起事件中,您可以忽略所有在没有 IsMouseCaptured 的情况下发生的事件。并确保不要忘记 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);
    // ...
}

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);
    // ...
}
陪你搞怪i 2024-09-12 02:43:09

12 年后...

repka 的答案(上面)在 WPF UserControl 实现中对我不起作用,但它让我走上了正确的道路。相同的概念,只是使用 bool 变量而不是 CaptureMouse()。到目前为止,测试结果呈阳性。

谢谢雷普卡!

例子:

    private bool _mouseDown = false;

    private void LinkButton_LeftMouseButtonDown(object sender, MouseButtonEventArgs e)
    {
        this._mouseDown = true;
    }

    private void LinkButton_LeftMouseButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!this._mouseDown)
             return;
        this._mouseDown = false;

        //MouseUp logic here
    }

12 years later...

repka's answer (above) didn't work for me in a WPF UserControl implementation, but it got me going down the right path. Same concept, just using a bool variable instead of CaptureMouse(). So far, testing has been positive.

Thanks repka!

Example:

    private bool _mouseDown = false;

    private void LinkButton_LeftMouseButtonDown(object sender, MouseButtonEventArgs e)
    {
        this._mouseDown = true;
    }

    private void LinkButton_LeftMouseButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!this._mouseDown)
             return;
        this._mouseDown = false;

        //MouseUp logic here
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文