简体   繁体   中英

MFC: How to catch set focus of each control of dialog in one function

I have "n" dialogs which have the same base dialog. Each dialog has its own controls

  • edit boxes
  • combo boxes
  • list controls
  • etc.

In base dialog, how do I set focus messages of each control and,for example, give a Message box with

text("Hello I got focus, my ID is %d")?

The easiest way is using the classical subclassing method. The problem is that WM_SETFOCUS is not pumped through the message Loop, so PreTranslateMessage will not help.

Thee are some nice classes that help to do additional subclassing without disturbing the MFC stuff. Paul Di Lascia wrote CSubclassWnd . PJ Naughter wrote CHookWnd . And with the ATL has CWindowsImpl .

All this classes allow easy additional subclassing even if a window is already subclassed by the MFC.

You can use "standard subclassing" GetWindowLong / SetWindowLong too.

As Jerry already said make a hook, get parent window handler via GetParent() and SendMessage(hParentWND, WM_MESSAGE, lParam, wParam) .
Of course, you should handle WM_MESSAGE in your parent window.
Btw, framework calls OnSetFocus function when window gained focus.

According to this SO article , you can hook the WM_SETFOCUS message.

You can get the Control ID by using GetDlgCtrlID with the hwnd returned by the hook.

But beware of popping up a MessageBox , that will change the focus and trigger your hook proc, making it go into a loop!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM