[英]Can I ensure my control catches the first event regardless of whether my form has focus?
I am relatively new to C#. 我对C#比较陌生。 I have a window with buttons.
我有一个带按钮的窗口。 If the window is out of focus and I click on a button the first time, the first click grabs focus for the window and all subsequent clicks will perform their respective actions.
如果窗口没有聚焦,并且我第一次单击按钮,则第一次单击将抓住窗口的焦点,所有后续单击将执行各自的操作。
Is there a way to execute the event associated with the button instead of grabbing focus? 有没有一种方法可以执行与按钮关联的事件,而不是获取焦点?
It sounds like you are describing how ToolStrips operate, which does not fire a click event unless the application has the focus. 听起来您正在描述ToolStrips的操作方式,除非应用程序具有焦点,否则它不会触发click事件。
A work around is to use your own ToolStrip and let the mouse activation give the control the focus, which in turn will then let the button fire it's click event: 解决方法是使用您自己的ToolStrip,并通过鼠标激活使控件成为焦点,然后使按钮触发click事件:
public class ToolStripIgnoreFocus : ToolStrip {
private const int WM_MOUSEACTIVATE = 0x21;
protected override void WndProc(ref Message m) {
if (m.Msg == WM_MOUSEACTIVATE && this.CanFocus && !this.Focused)
this.Focus();
base.WndProc(ref m);
}
}
Rebuild your solution and you should see a ToolStripIgnoreFocus
control available in your tool box. 重建您的解决方案,您应该在工具框中看到一个
ToolStripIgnoreFocus
控件。 Try adding that to your form and then add your tool buttons accordingly. 尝试将其添加到表单中,然后相应地添加工具按钮。
这就是大多数Windows应用程序的工作方式-该应用程序必须先获得焦点,然后才能接收点击事件。
As far as I know, you've described an inherent Windows behaviour and as such it could be impossible to do. 据我所知,您已经描述了Windows固有的行为,因此可能无法做到。
An alternative is to harness 'Always on top' style of windows app which is explained here: 一种替代方法是利用Windows应用程序的“始终位于顶部”样式,在此处进行说明:
How to make a window always stay on top in .Net? 如何使窗口始终位于.Net顶部?
This is normal windows behavior. 这是正常的Windows行为。 Something that I don't think you can override (so that the click event fires, but doesn't bring your app to the foreground, and active state).
我认为您无法覆盖某些内容(以便触发click事件,但不会使您的应用程序处于前台和活动状态)。
If you don't want to bring focus to the window, but still want to provide some 'interaction' with the window itself, try keyboard hooks or hotkey events. 如果您不想将焦点放在窗口上,但仍想与窗口本身提供一些“交互”,请尝试使用键盘挂钩或热键事件。 Examples:
例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.