将 GetFocus 限制为特定对话框和/或应用程序
我有一个 MS Visual C++ 项目,其中有一个基于 CDialog 的对话框,其中包含多个编辑控件。我想为用户突出显示当前焦点的控件。我通过使用 GetFocus() 获取指向具有当前焦点的窗口的指针,然后使用 GetDlgCtrlID() 获取焦点控件的 ID 来实现此目的:
CWnd *pWnd=GetFocus();
idfocus=(pWnd->GetDlgCtrlID());
然后,我将其与在成员函数 OnCtlColor 中设置 HBRUSH 的当前控件进行比较。
在我单击 PC 上运行的另一个应用程序的窗口之前,此方法工作正常。例如火狐浏览器。此时我的应用程序崩溃了。
在特定对话框中查找哪个控件(如果有)并且只有该特定对话框具有焦点的最佳方法是什么?或者可能仅限于同一应用程序。
I have a MS Visual C++ project where I have a CDialog based dialog box with several edit controls. I want to highlight the control with the current focus for the user. I implemented this by getting a pointer to the window that has the current focus with GetFocus() and then obtaining the ID for the focused control with GetDlgCtrlID():
CWnd *pWnd=GetFocus();
idfocus=(pWnd->GetDlgCtrlID());
I then compare this to the current control for which the HBRUSH is being set for in member function OnCtlColor.
This works fine until I click on the window for another application running on my PC. Such as Firefox. At which point my application crashes.
What is the best way to find which control, if any, in a specific dialog box, and only that particular dialog box, has focus? Or maybe restrict to only the same application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对话框中的所有控件基本上都是主对话框的子控件。因此,您可以通过在执行所需操作之前首先检查所聚焦的控件是否是对话框的子控件(可以使用 GetParent())来创建限制。
All the controls within your dialog are basically children of your main dialog. So you can create a restriction by checking first if the control being focused is a child of your dialog (you can use GetParent()) before performing the operations you want.