在From中设置Capture = true仅捕获鼠标单击事件一次

时间:2018-05-25 14:25:09

标签: c# winforms

我在表单中遇到了 Capture 属性的奇怪行为。我将其设置为 true 并订阅表单中的鼠标单击事件,以检查每次在表单的客户区域内单击鼠标时是否正在触发事件。在一个带有表单和三个按钮的简单测试中,我看到捕获只在第一次尝试时才能正常工作,之后不会调用事件处理程序。这是代码:

public partial class Form1 : Form
{
    public Form1()
    {
        //Form1 has three buttons that are created inside InitializeComponent. It also subscribes Form1_MouseClick handler to the MouseClick event of the Form
        InitializeComponent();
        // Setting Capture to true will force to handle any mouse click on any control in the form
        Capture = true;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        // This message box should appear after any click in the form or control inside it
        MessageBox.Show("Click!");
    }
}

我还尝试订阅 MouseCaptureChanged 事件以重新启用 Capture 属性到 true ,但没有成功。有人能解释一下这种行为的原因吗?

0 个答案:

没有答案
相关问题