我有一个用 vb.net 编写的 .net MDI 应用程序。我正在尝试编写一个表单,该表单允许用户选择一个特殊字符,例如°、μ、²、3、ɑ等,并将该字符插入到通过启动表单之前所关注的任何控件中。热键或 MDI 父级中的主菜单。
执行此操作的简单方法是找出调用字符选择表单时哪个控件集中于哪个 MDI 子表单,但我找不到有关如何执行此操作的任何信息。
有什么想法吗?
I have a .net MDI application written in vb.net. I'm trying to program a form that will allow the user to select a special character such as °, µ, ², ³, ɑ and so on and have that character inserted into whatever control was focused prior to their launching the form via a hot-key or the main menu in the MDI parent.
The easy way to do this would be to figure out which control was focused on which MDI Child form when the character selection form is invoked, but I can't find any information on how to do this.
Any ideas?
发布评论
评论(4)
看起来 WM_SETFOCUS 消息没有通过......我的错。我很确定他们会通过 Control.WndProc 方法。作为解决方法,我必须 p/invoke GetFocus 每当有消息到来时,就存储具有焦点的控件的句柄。
第一个代码部分是过滤器类,第二个代码部分是测试代码。
Looks like the WM_SETFOCUS messages aren't coming through.... My bad. I'm pretty sure they'd come through in the Control.WndProc method. As a work around, I had to p/invoke GetFocus evertime a message came and store the handle of the control that has focus.
The 1st code section is the filter class and the 2nd code section is the test code.
添加 IMessageFilter 并监视 Windows 消息例如 WM_SETFOCUS 或 WM_ACTIVATE。您可以使用 Control.FromHandle将 IntPtr 转换为 .NET 控件。
Add an IMessageFilter and monitor for windows messages such as WM_SETFOCUS or WM_ACTIVATE. You can use Control.FromHandle to convert IntPtrs to .NET Controls.
找到了一个更简单的方法——
Found an easier way -
您可以将这样的类添加到您的项目中:
然后,对于您希望成为“最近聚焦的控件”候选者的任何表单上的任何控件,您可以执行以下操作:
然后访问
FocusWatcher.FocusedControl< /code> 获取最近关注的控件。监视消息将起作用,但您必须忽略您不想要的消息(例如 Mdi 表单中的 WM_ACTIVATE)。
您可以遍历每个表单上的所有控件,并为 GotFocus 事件添加此处理程序,但肯定有一些控件您不希望使用此处理程序(例如,按钮)。您可以改为迭代并仅添加文本框的处理程序。
You could add a class like this to your project:
Then, for any control on any form that you want to be a candidate for "most recently-focused control", you would do this:
and then access
FocusWatcher.FocusedControl
to get the most recently-focused control. Monitoring messages will work, but you have to ignore messages that you don't want (like WM_ACTIVATE from the Mdi Form).You could iterate through all the controls on every form and add this handler for the GotFocus event, but surely there are controls that you don't want this for (like Buttons, for example). You could instead iterate and only add the handler for TextBoxes.