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")?
有帮助吗?

解决方案 2

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 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top