Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get the focus ctrl window of another window?

Posted on 2001-08-02
6
Medium Priority
?
246 Views
Last Modified: 2013-12-03
   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=GetForegroundWindow();
        AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(hwnd_active,NULL),TRUE);
        hwnd_focus=GetFocus();
        AttachThreadInput(GetCurrentThreadId(),GetWindowThreadProcessId(hwnd_active,NULL),FALSE);

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?

0
Comment
Question by:whjiang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6347626
I'm not sure about the performance hit of the method you're using. But one thing I can say is this: If you do this check regularly in a loop or something like that, the system will begin to swallow some double clicks. I had this problem when I was doing the same thing (well, I needed the current mouse cursor (GetCursor), so was not 100% the same, but almost) every 500ms or something like that.

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.
0
 
LVL 5

Accepted Solution

by:
robpitt earned 1200 total points
ID: 6347721
If you need an example of what Madshi is talking about see http://website.lineone.net/~codebox/focuslog.zip

The above is a tiny sample I wrote for someone else, but I've adapted to to suit your needs.

It implements a WH_CBT hook dll and then keeps track of the currently active window.

It also (optionally) has a method to call you back whenever the focus changes.



Rob


PS Madshi, sorry for posting the same solution as you - but I had this working sample code lying arround. I leave it "whjiang" to figure out how to distribute the points.


0
 
LVL 20

Expert Comment

by:Madshi
ID: 6348176
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...   :-)
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:whjiang
ID: 6354808
   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?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6354929
Just use your AttachThreadInput code once to initialize the stuff.

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6354952
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question