OnKillFocus() override in MFC triggering at odd times

2.2k Views Asked by At

I need to know when my Window goes out of input focus, so I overloaded the OnKillFocus() method of the CWnd.

However it doesn't invoke this method when I focus another application (alt+tab), or even minimize the window. But it DOES invoke the method when I restore it from being minimized. Are these the intended times for it to trigger this method?

2

There are 2 best solutions below

0
On

I think you'll need a CWnd::OnActivateApp() handler if you need to be sure of being notified when your application is switched out.

0
On

OnKillFocus() is normally only used consistently for controls that have a concept of gaining the focus - buttons, edit boxes, list boxes, etc. Normally CWnd does not accept the focus, so you can't rely on that - I'm surprised you get it at all.

In addition to WM_ACTIVATEAPP mentioned above, there's also WM_ACTIVATE when switching between windows within the same application you might want to trap.