Search code examples
c++mfcsetfocus

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")?

Solution

  • 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!