收录日期:2019/12/11 02:47:45 时间:2010-10-28 14:11:29 标签:visual-c++,focus,cwnd

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.

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.