Advertisement

Best way to track window focus

Started by November 18, 1999 10:41 PM
3 comments, last by Splat 25 years, 1 month ago
The only thing I can come up with is either WM_ACTIVATEAPP or WM_CANCELMODE. Or do these messages get sent (paired) with a WM_ACTIVATE message?

Why are you checking it twice (bit manipulation is not my specialty yet)?

Wish I could have suggested something better!

Well, the above code is a combination of documentation and analysis. The reason to check wParam twice is because it contains two pieces of data: one in the low word and one in the high.

Experimentally, this works in all cases. But I really need confirmation because this is really a hack and does not make much logical sense in the context of the MSDN docs.

- Splat

Advertisement
I would use the WM_ACTIVATEAPP message, when there is about to be a change in the window focus, this message is sent to the app who of the window that will loose focus and to the app of the window that will gain it.
wParam will be equal to TRUE if it is the window gaining focus or FALSE if its loosing it. lParam contains the thread ID of the app that owns the window that will gain focus. That is pretty much what is said in the MSDN Library.
Been playing with this a bit, and the best I can come up with is using the WM_ACTIVATE message and this code:

if ((wParam & 0xffff) == WA_INACTIVE) {
isActive = false;
}
else {
if ((wParam >> 16) == 0)
isActive = true;
}

Anyone have any suggestions? This seems to work in all circumstances, but there may be something I am overlooking.

- Splat

That was what I had originally, but it fails to detect special cases with minimizing the application. Mainly it will not detect the deactivation if it's task bar box is clicked to minimize it (i think, the old code is now only in the SourceSafe database). Simply, it didn't work all the time. What I have above works all the time, but I've only tested on Win2000. Which is certainly not my target audience.

- Splat

This topic is closed to new replies.

Advertisement