whjiang
asked on
How to get the focus ctrl window of another window?
I want to get the system-wide current focus ctrl window, no matter which is the child window of my application or not.
I know I can use the following method to do so:
hwnd_active=GetForegroundW indow();
AttachThreadInput(GetCurre ntThreadId (),GetWind owThreadPr ocessId(hw nd_active, NULL),TRUE );
hwnd_focus=GetFocus();
AttachThreadInput(GetCurre ntThreadId (),GetWind owThreadPr ocessId(hw nd_active, NULL),FALS E);
but, I am wondering whether it will consume much system resources and times or not. Is there any lighter method to achieve the same goal or not?
I know I can use the following method to do so:
hwnd_active=GetForegroundW
AttachThreadInput(GetCurre
hwnd_focus=GetFocus();
AttachThreadInput(GetCurre
but, I am wondering whether it will consume much system resources and times or not. Is there any lighter method to achieve the same goal or not?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Rob, no problem, after all our main duty is to help, and a code sample surely helps more than only showing the way. I have no problem, if you get the points, when you earned them... :-)
ASKER
I have tried the method you two recommended. But there is still a problem: if there is no window focus changed since my application started, then no CBT_SETFOCUS message can be recorded. So I can not tell the current focus window unless user change the focus window. From then on everything is ok.
How can this initialization problem being solved?
How can this initialization problem being solved?
Just use your AttachThreadInput code once to initialize the stuff.
Regards, Madshi.
Regards, Madshi.
Yep, my code works by tracking changes after the program starts up, hence it doesn't actually know where the focus starts off. Madshi's solution will get arround this limitation.
An alternative would be to use a system wide hook (SetWindowsHookEx) to get system wide access to the message WM_SETFOCUS (for me it was WM_SETCURSOR). A hook also costs some performance, but at least you won't have that double click problem anymore. This system wide hook solution is the one I'm currently using and it works quite fine.
P.S: Of course the callback function for the system wide hook has to be in a little dll...
Regards, Madshi.