有没有什么办法来检测用户的控制之外的鼠标点击? [英] Is there any way to detect a mouseclick outside a user control?

查看:158
本文介绍了有没有什么办法来检测用户的控制之外的鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义的下拉框,我想被点击鼠标下拉框外,以隐藏它注册。是否有可能检测控制之外的点击?或者我应该包含窗体上的一些机制和检查时,点击鼠标的任何dropdownbox是开放

I'm creating a custom dropdown box, and I want to register when the mouse is clicked outside the dropdown box, in order to hide it. Is it possible to detect a click outside a control? or should I make some mechanism on the containing form and check for mouseclick when any dropdownbox is open?

推荐答案

所以,我终于明白,你只希望它关闭时,用户的点击的它之外。在这种情况下, 离开事件应该只是罚款...出于某种原因,我得到了你想要它关闭时,他们移动了鼠标的自定义下拉列表外的印象。只要您的控件失去焦点的离开事件引发的,如果用户点击别的东西,它肯定会失去焦点,因为他们在获得焦点点击的东西。

So I finally understand that you only want it to close when the user clicks outside of it. In that case, the Leave event should work just fine... For some reason, I got the impression you wanted it to close whenever they moved the mouse outside of your custom dropdown. The Leave event is raised whenever your control loses the focus, and if the user clicks on something else, it will certainly lose focus as the thing they clicked on gains the focus.

该文件还称,这一事件级联向上和向下的控制链为必要的:

The documentation also says that this event cascades up and down the control chain as necessary:

输入离开事件是分层的,并会级联上下父链,直到相应的控制达到。例如,假设你有两个组框控件的形式,每个GroupBox控件有一个TextBox控件。当插入符号从一个文本框移动到另一个时,离开事件引发的文本框和分组框中,而输入事件引发了其他分组框中和TextBox。

The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

覆盖您的用户控件的 OnLeave 方法来处理这​​个的最佳方式:

Overriding your UserControl's OnLeave method is the best way to handle this:

protected override void OnLeave(EventArgs e)
{
   // Call the base class
   base.OnLeave(e);

   // When this control loses the focus, close it
   this.Hide();
}

和则出于测试目的,我创建了一个表格,显示的下拉用户控件的命令:

And then for testing purposes, I created a form that shows the drop-down UserControl on command:

public partial class Form1 : Form
{
   private UserControl1 customDropDown;

   public Form1()
   {
      InitializeComponent();

      // Create the user control
      customDropDown = new UserControl1();

      // Add it to the form's Controls collection
      Controls.Add(customDropDown);
      customDropDown.Hide();
   }

   private void button1_Click(object sender, EventArgs e)
   {         
      // Display the user control
      customDropDown.Show();
      customDropDown.BringToFront();   // display in front of other controls
      customDropDown.Select();         // make sure it gets the focus
   }
}



一切完美与上面的代码,除了的一件事:如果用户点击表格的空白区域,用户控件不会关闭。嗯,为什么不呢?嗯,因为窗体本身不希望的焦点。只有的控件的可以得到的焦点,我们没有点击一个控制。而且,由于没有别的偷了焦点,离开事件从未被提起,这意味着用户控件不知道它是应该关闭自己。

Everything works perfectly with the above code, except for one thing: if the user clicks on a blank area of the form, the UserControl doesn't close. Hmm, why not? Well, because the form itself doesn't want the focus. Only controls can get the focus, and we didn't click on a control. And because nothing else stole the focus, the Leave event never got raised, meaning that the UserControl didn't know it was supposed to close itself.

如果您需要的用户控件,当用户点击关闭本身的空白区域的形式,你需要一些特殊的办案为。既然你说你只关心的点击的,你可以处理点击事件的形式,而将焦点设置到不同的控制:

If you need the UserControl to close itself when the user clicks on a blank area in the form, you need some special case handling for that. Since you say that you're only concerned about clicks, you can just handle the Click event for the form, and set the focus to a different control:

protected override void OnClick(EventArgs e)
{
   // Call the base class
   base.OnClick(e);

   // See if our custom drop-down is visible
   if (customDropDown.Visible)
   {
      // Set the focus to a different control on the form,
      // which will force the drop-down to close
      this.SelectNextControl(customDropDown, true, true, true, true);
   }
}



是的,这最后一部分的感觉就像一个黑客。更好的解决方案,如其他人所说,是使用 SetCapture 功能来指示Windows捕获鼠标在你的用户控件的窗口。控制的 拍摄物业提供了一个更简单的方式做同样的事情。

Yes, this last part feels like a hack. The better solution, as others have mentioned, is to use the SetCapture function to instruct Windows to capture the mouse over your UserControl's window. The control's Capture property provides an even simpler way to do the same thing.

这篇关于有没有什么办法来检测用户的控制之外的鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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