简体   繁体   English

窗体会忽略一些鼠标单击

[英]Form ignores some mouse clicks

I'm working on a winforms project where I need to display text and imagery when a mouse click is detected, but I"m running into a few problems. I cannot consistently detect mouse clicks. Here's a basic overview of the program, first the main form loads. Then the user loads another form to set up options. Once that is done, the other form is closed and the main form gets focus. The main form is what should be receiving mouse input. Once a mouse click is detected, a function is called that uses ThreadPool.QueueUserWorkItem to play sound and display imagery. 我正在开发一个winforms项目,该项目需要在检测到鼠标单击时显示文本和图像,但是我遇到了一些问题。我无法始终如一地检测到鼠标单击。这是该程序的基本概述,首先是加载主表单,然后用户加载另一个表单以设置选项。完成后,另一个表单将关闭,主表单将获得焦点。主表单应接收鼠标输入,一旦检测到鼠标单击,调用一个函数,该函数使用ThreadPool.QueueUserWorkItem播放声音并显示图像。

What happens is that mouse clicks are sometimes ignored in the main form. 发生的情况是在主窗体中有时会忽略鼠标单击。 Usually the first click is ignored, then I'd say more than half of future mouse clicks are ignored. 通常,第一次单击将被忽略,然后我会说超过一半的未来鼠标单击将被忽略。 I've added code to notify me when MouseUp fires in the event handler, so I'm certain the event handler is not getting called. 我添加了代码,以在事件处理程序中触发MouseUp时通知我,因此我确定未调用事件处理程序。 I've also set this.Capture = true; 我还设置了this.Capture = true; in the main form, so I'm not quite sure why some mouse clicks are being ignored. 以主要形式显示,因此我不太确定为什么忽略了一些鼠标单击。 Is there anything that could be interfering with mouse clicks? 有什么可能会干扰鼠标单击?

Yes

Plenty of things can interfere with mouse clicks, namely, other UI elements. 很多事情都会干扰鼠标单击,即其他UI元素。

If your event handler is on the base container control, and they click in, say, a text box; 如果您的事件处理程序位于基本容器控件上,并且它们单击了例如文本框; then the text box receives the click event and the container doesn't. 那么文本框会收到click事件,而容器不会。

If your "on top" controls don't need user input, you can simply set IsHitTestVisible (or the WinForms equivalent) to false. 如果“顶部”控件不需要用户输入,则只需将IsHitTestVisible (或WinForms等效项)设置为false。 If you do need user input, you need to either capture the clicks differently, or add a "MouseUp" event to each control as well. 如果确实需要用户输入,则需要以其他方式捕获点击,或者也向每个控件添加“ MouseUp”事件。

See Hittest transparency for an entire form and WinForms equivalent of WPF's IsHitTestVisible for ways to do IsHitTestVisible in Winforms. 有关在Winforms中执行IsHitTestVisible方法,请参见完整表单的Hittest透明度与WPF的IsHitTestVisible等效的 WinForms。 Alternatively, just use WPF. 或者,只需使用WPF。

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

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