(Note: Also posted on Pelles C forums)
The problems with WM_ACTIVATE for setting keyboard focus are well known. This is one solution but it's still not right.
Can someone please suggest a better way...
I've just spent a whole day struggling with WM_ACTIVATE... I have a window with a treeview and a listview in the more or less standard explorer configuration... The goal is to have it place the keyboard focus back where it came from after the main window loses then regains forgeround status... It's insane! Half the time GetFocus() returns NULL. When the window is minimized it returns different values than if you just click on the desktop... worse still, if you don't handle the message at all, it comes back to foreground with *nothing* selected and you have to click it to get it going again...
Here's the handler code I came up with. It mostly works but there remains a problem that if you launch the program and immediately minimize it... or if you minimize it before it loses focus some other way, it resets back to the treeview control... (As you can see from my notes, I'm convinced there has to be a better way)
Code:// set keyboard focus
BOOL DoWMActivate(WPARAM Action) // note this is a horrible Kludge
{ static HWND Focus; // it seriously needs fixing
switch (Action) // Microsoft seriously needs fixing
{ case WA_INACTIVE :
Focus = GetFocus();
break;
case WA_ACTIVE :
case WA_CLICKACTIVE :
SetFocus( Focus ? Focus : gTreeView );
break;
default :
SetFocus(gTreeView); }
return FALSE; }