简体   繁体   English

我可以确保我的控件捕捉到第一个事件,而不管我的表单是否具有焦点吗?

[英]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.

相关问题 如何确保我的 blazor 表单按钮元素的事件处理程序运行 - How can I ensure the event handlers for my blazor form button elements run 如何确保用户已登录我的系统? - How can I ensure user has logged into my system? 捕获所有键盘输入,无论Windows Form应用程序中的焦点在哪个控件上 - Capture all Keyboard input, regardless of what control has focus in Windows Form applicaion 如何确定我的应用程序是否处于活动状态(有焦点) - How to determine whether my application is active (has focus) 检测我的表单何时有焦点 - Detecting when my form has focus 当子控件具有焦点时,在窗体上捕获KeyUp事件 - Capture KeyUp event on form when child control has focus 有没有办法我可以强制Telerik RadGrid始终显示5行,无论我的数据源有多少行 - Is there a way I can force Telerik RadGrid to always show 5 rows regardless of my datasource has that much rows 我无法在 Windows 表单中使用我的用户控制组件 - I can't use my User Control Component in Windows Form 如何使我的 Form Control 变量可以访问我的 Form 类以外的类? - How can I make my Form Control variables acessible to classes other than my Form class? 如何在不关闭第一个表单的情况下隐藏第二个表单对话框? - how can i hide my second form dialog whilst not closing my first form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM