简体   繁体   English

在MouseDown上显示MessageBox会占用MouseUp事件

[英]Showing MessageBox on MouseDown eats up MouseUp event

I am working on a WPF application where i handled a mouse down event which eventually shows up MessageBox.. But after MessageBox appears on mouseDown, it eats up corresponding MouseUp event of a control. 我正在处理一个WPF应用程序,我处理了一个最终显示MessageBox的鼠标按下事件。但是在mouseDown上出现MessageBox后,它会占用控件的相应MouseUp事件。

Scenario can be easily reproduced by simply handling MouseDown and MouseUP event in WPF window as:- 只需在WPF窗口中处理MouseDown和MouseUP事件即可轻松复制场景:

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
   MessageBox.show("Hello, Mouse down");
}

private void Window_MouseUP(object sender, MouseButtonEventArgs e)
{
   MessageBox.show("Hello, Mouse Up");
}

MouseUp message is never shown, once messagebox appears on MouseDown event. 一旦消息框出现在MouseDown事件上,MouseUp消息就不会显示。

What about initializing a new instance of System.Threading.Thread to call the MessageBox so that the main user interface thread would not be interrupted by the prompt? 如何初始化System.Threading.Thread的新实例以调用MessageBox以便主用户界面线程不会被提示中断?

Example

private void Window_MouseDown(object sender, MouseEventArgs e)
{
    Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Down")); //Initialize a new Thread to show our MessageBox within 
    mythread.Start(); //Start the thread
}

private void Window_MouseUP(object sender, MouseEventArgs e)
{
    Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Up")); //Initialize a new Thread to show our MessageBox within 
    mythread.Start(); //Start the thread
}

Screenshot 截图

在另一个线程中调用MessageBox以避免中断主用户界面线程

Thanks, 谢谢,
I hope you find this helpful :) 我希望这个对你有用 :)

As the commenter on your original post said, it seems that what's happening here is that the user's mouse wanders out of focus to click in the message box, or even just to display it, so the mouse moves "up" anyway - the event is never called. 正如你原来帖子上的评论者所说的那样,这里发生的事情似乎是用户的鼠标徘徊在焦点以点击消息框,甚至只是为了显示它,所以鼠标无论如何都向上移动 - 事件是从未打电话过 If you're just wanting to display messageboxes, then simply using: 如果您只是想显示消息框,那么只需使用:

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
   MessageBox.show("Hello, Mouse down");
   MessageBox.show("Hello, your mouse must be up because you've shifted focus!");
}

should do the job. 应该做的工作。 If this behaviour repeats for something like changing a window title, or anything that doesn't require user input, then this could be a problem, but I am 100% sure that this is just an issue with regards to the MessageBox. 如果这种行为重复更改窗口标题或任何不需要用户输入的内容,那么这可能是一个问题,但我100%确定这只是一个与MessageBox有关的问题。 Hope this helped. 希望这有帮助。

@picrofo solution is also good and easy but i did by this way @picrofo解决方案也很好,很简单,但我这样做了

  DialogResult result;
 private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        string message = "would you like to see mouse up event?";
        string caption = "event trick";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;

        result = MessageBox.Show(message, caption, buttons);
        textBox1.Text = result.ToString();
        if (result == System.Windows.Forms.DialogResult.Yes)
        {
            button1_MouseUp(sender, e);

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM