views:

133

answers:

1

Since I need to do some checks depending on which control is on focus in my app, I am getting the focused control ID like this:

HWND controlOnFocus = ::GetFocus();
int controlID = ::GetDlgCtrlID(controlOnFocus);

I am getting consistent IDs, but I can't find them in the resource file! Can I rely on the IDs I am getting or what?

Any help would be highly appreciated.

+1  A: 

Your snippet of code gets the control identifier from whichever window has the current focus. Your application is likely to include lots of windows that you haven't created yourself, such as common dialogue boxes, and the IDs for those won't be in your resource file. Also, not all windows have useful control IDs; For instance, static text controls usually have ID_STATIC (-1?).

RobH
see edit - some details added
JohnIdol
JohnIdol
Forgot to add My controls are dynamically generated - can I rely on the ID if don't change anything else?
JohnIdol
First, I'd recommend firing up SpyXX to check what windows there really are around your control. Secondly, note that if you right-clicking on a control doesn't set the focus. Instead, call WindowFromPoint to find out which window was under the pointer.
RobH
If you create a window with an ID, you can rely on the window having that ID for its lifetime. The ID actually gets stored in a window-specific area (in the OS, rather than in ATL). See ::GetWindowLong( hWnd, GWL_ID );
RobH
thanks it's clearer now
JohnIdol