Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,
I need to write a console/Window Service application in C# using Visual Studio 2005 which will run 24X7 on a system.Requirements are
1)An event should fire every 5 minutes to findout out which is current active window(which have keyboard focus)
running on a system
2)write it into text file -for a particular time which application was active window
3)This text file data should be encrypted.
Kindly help me out.
Regards,
Manushi
Posted
Updated 18-Mar-11 20:12pm
v4
Comments
Sergey Alexandrovich Kryukov 19-Mar-11 0:20am    
My 5 for non-trivial question (but only after a follow-up comment).
--SA

Most probably the window you need is not the foreground window. It works in most cases, but can fail to get desired effect when the application runs a dialog.

To do the next step, you should use P/Invoke.
Windows is very naughty, what you thing is obvious may be not what you want.

So here is what you do: call Windows API GetForegroundWindow, get some HWND as a return value. Use this HWND value as a parameter of the API GetLastActivePopup. Most likely, it will return the same HWND as its parameter, but in certain cases, it will return some other HWND of the windows owned by your foreground windows. This is what's really on top.

The last step depends on your application. From the previous step you got two window handles; they can be the same or not. You may need both of them or just one of them, depending on what you want to do with them.

[EDIT]
A follow-up comment OP confirm the actually the "last active popup" is what required.
"Active window" means "a popup window holding keyboard focus or having non-popup children, one of them holding keyboard focus". By weird Windows design, it is not always a foreground window. Microsoft… :-)

See for more information: http://msdn.microsoft.com/en-us/library/ff468919(v=VS.85).aspx[^].

—SA
 
Share this answer
 
v3
Comments
Albin Abel 19-Mar-11 12:53pm    
I understood what you are telling. But hard to believe type on a window (background) when an other window above it (foreground) . As you said it is illogical. If OP sending the text through message to set the text of the window the window will not come up to foreground. But if send keys a background window won't get the keys. Now a new keyword "last active popup". Agreed a popup no need to be a foreground window. But when OP wants to write it he has to bring it to foreground or he has to send message to set windows text. Later method is more complex and need to get the handle of the child window i.e writable area. OP hasn't notified anything in the question about a popup.

However I like this tweak very much that use the current handle to get the last active pop up and it returns some hwnd which is nothing related to the passed hwnd. Great tweak. 5+ for that and 5+ for the critical thinking
Sergey Alexandrovich Kryukov 19-Mar-11 15:24pm    
There are two things which complicates things in real life (this is not exactly situations OP asked about, but can be related):

#1) This is normal situation: If you have main windows (for example) which opens a dialog, a main window owns the dialog, which is an active pop-up. If you put such application on top using Alt+TAB, it restore z-order of the main windows and active the pop-up. If you this programmatically (which may be required for implementing single-instance application behavior: second instance case the first one to show up) -- it's easy to mess-up; and this is one way (but not the only way) to create a pathological situation.

#2) One pathological situation is this: main window is on top, pop-up is hidden behind it but activated. This is very typical. I would like Windows to make it impossible, but unfortunately, this is not the case. What to do if you already have the pathological situation by one or another reason and want to show the window and its last active pop-up on top and activate it? Is we always assume the pathological situation never happens (which is not the case), the operation would be not quite successful to my taste... :-)
--SA
Not clear about your requirements. But to get the active windows we use the API call GetForegroundWindow. The reference here http://msdn.microsoft.com/en-us/library/ms633505%28v=vs.85%29.aspx[^]

Google help how to call this in your c#
 
Share this answer
 
Comments
Espen Harlinn 18-Mar-11 5:35am    
Good advice, 5ed!
Albin Abel 18-Mar-11 5:49am    
Thanks Espen Harlinn
manushi88 18-Mar-11 23:17pm    
Hi AlbinAbel,
Thanks for your solution but I have already tried by using API call GetForegroundWindow().I require Active Window instead of Foreground window.
Anthony Mushrow 19-Mar-11 0:03am    
Well, the window a user is working on is always on top and as such is the foreground window. From MSDN Retrieves a handle to the foreground window (the window with which the user is currently working).
What's your definition of the Active window? Did GetForegroundWindow not return what you had expected?
Sergey Alexandrovich Kryukov 19-Mar-11 0:15am    
I understand. The problem is more complicated. I provided the Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900