如何防止菜单和工具栏点击从浮动窗体中窃取焦点? [英] How do I prevent menu and toolbar clicks stealing focus from a floating form?

查看:243
本文介绍了如何防止菜单和工具栏点击从浮动窗体中窃取焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想达到与Visual Studio 2010+相同的效果,如果你在主窗体顶部浮动一个编辑器,点击主窗体菜单或工具栏按钮不会导致偷取重点的主要形式;而不是浮动的窗口保持焦点。

如果没有这个,当焦点控件在浮动窗口中时,调用任何主控菜单命令无法调用聚焦控件 - 因为你想操作的控件就像命令被调用一样失去焦点。





(具有讽刺意味的是,如果仔细观察这个图片,您会看到编辑器被静音,表明编辑器控件不具有焦点,Visual Studio似乎做了一些微妙的处理,以达到浮动窗口仍然聚焦的幻想。)

解决方案

如果您使用 TActionMainMenuBar TActionToolBar首先菜单栏: TActionMainMenuBar 有一个属性 AutoFocus ,默认为True。将其设置为False,然后单击菜单将不会自动将焦点对准包含菜单的表单。现在工具栏: TActionToolBar 不具有焦点一个AutoFocus属性。相反,您需要处理 OnMouseActivate 事件并返回 maNoActivate



请注意,从OnMouseActivate处理程序返回maNoActivate不适用于所有控件。有些控件(如TButton)会在单击时为自己提供焦点,而不管OnMouseActivate处理程序的返回结果如何。但在TActionToolBar的情况下,我们确实得到了我们之后的效果。



我花了一段时间才发现这一点,甚至在Google上搜索一个包含 AutoFocus和OnMouseActivate没有得到有用的结果(除了大概这个页面被索引之外)。所以我希望这个答案能够帮助别人。


I want to achieve the same effect as in Visual Studio 2010+, where if you float an editor on top of the main form, clicking on a main form menu or toolbar button doesn't cause the main form to steal focus; instead the floating window retains focus.

Without this it becomes impossible to invoke any main menu command that operates on a focused control when the focused control is in a floating window - because the control you wanted to operate on loses focus just as the command is invoked.

(Ironically, if you look closely at this image you'll see that the selection in the editor is muted, indicating that the editor control doesn't have focus. Visual Studio seems to be doing something slightly devious to achieve the illusion that the floating window is still focused.)

解决方案

The solution is very easy if you are using TActionMainMenuBar and TActionToolBar.

First the menu bar: TActionMainMenuBar has a property AutoFocus, which is True by default. Set this to False and then clicking on the menu won't automatically give focus to the form containing the menu. Instead, the form that previously had focus will retain it.

Now the tool bar: TActionToolBar doesn't have an AutoFocus property. Instead you need to handle its OnMouseActivate event and return maNoActivate.

Note that returning maNoActivate from an OnMouseActivate handler doesn't work for all controls. Some controls such as TButton will give themselves the focus when clicked regardless of the return result from the OnMouseActivate handler. But in the case of TActionToolBar we do get the effect we are after.

It took me a while to find this though, and even searching Google for a page containing both 'AutoFocus' and 'OnMouseActivate' elicits no useful results (other than, presumably, this page once it gets indexed). So I hope this answer will help someone else.

这篇关于如何防止菜单和工具栏点击从浮动窗体中窃取焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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