Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Finding the currently actrive window from a background application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill - 08 Jun 2007 21:48 GMT
In C#...

I need to create and application that will run in the background and listen
for user mouse clicks anywhere on the desktop or any open application. When a
mouse click happens, it will catch that event and get a reference to the
active application...

Is this possible?

Can anyone give me an idea on how I would acheive this?

Thanks
Signature

Bill

Moty Michaely - 10 Jun 2007 07:27 GMT
> In C#...
>
[quoted text clipped - 10 lines]
> --
> Bill

Dear Bill,

You can use windows hooks for this purpose. It's a bit advanced
programming but it achieves your needs.
Check this detailed article with mouse hook example:
http://support.microsoft.com/kb/318804

Feel free to ask any further questions.
Hope this helps.
Cheers,
Moty
Bill - 11 Jun 2007 02:02 GMT
Thanks Moty,

This doesn't give me exactly what I need because it will not handle global
hooks.  However, after reading your post, I did a little searching on google
and found this piece of code that handles global hooks.

http://www.codeproject.com/csharp/globalhook.asp

That solves the first problem. The second problem is how can I determine
whch application the user clicked... ie: the active application??

Thanks for your help

Signature

Bill


> > In C#...
> >
[quoted text clipped - 22 lines]
> Cheers,
> Moty
Moty Michaely - 11 Jun 2007 06:53 GMT
> Thanks Moty,
>
[quoted text clipped - 38 lines]
> > Cheers,
> > Moty

Hi Bill,

Global Hooks is really what you need and I'm glad you managed to find
it helpful.

What do you exactly need about the application that caused the event?
It's process ID? It's main window handle?

In case you have the cursor position (which I assume you do) you can
get a window handle of the one that is under the cursor:
http://msdn2.microsoft.com/en-us/library/ms633558.aspx

Hope this helps.
Cheers,
Moty
Bill - 11 Jun 2007 15:10 GMT
Yes I have the mouse coordinates.

What I am working on is a tool, that records the steps that a user takes
through an application. The purpose is for application testing and experience
metrics. This particular module is an application recorder.

What I am trying to do is let the user press a start recording button and
then record their actions and generate script to repeat them. What I need to
be able to acheive on a mouse click is determine the current process and then
attach to it like a debugger does. Any help you can provide there would be
appreciated... I am an advanced programmer. C, C++, C#, VB. 15+ years... so I
good with advanced examples. I would prefer to be able to do this with
managed code.

From that point, other code I have created will do the recording. It
currently askes the user to specify the application and then loads and
executes it...

So I am really just trying to auto detect the application to record... which
could be the desktop.

Signature

Bill

> > Thanks Moty,
> >
[quoted text clipped - 54 lines]
> Cheers,
> Moty
Moty Michaely - 11 Jun 2007 19:30 GMT
> Yes I have the mouse coordinates.
>
[quoted text clipped - 78 lines]
> > Cheers,
> > Moty

Hi,

If you ask to record the mouse events, and would like to restrict the
events to a specific application/s, you should filter the messages
that are relevant to the restricted applications. In that case, you
should *ask* in which application the event occured, meaning under
what window was the mouse at the time the event occured (otherwise, I
can't see any other binding mechanism of a mouse event and an
application).

After getting the window handle, you can get the process ID of the
window by calling GetWindowThreadProcessId:
http://msdn2.microsoft.com/en-us/library/ms633522.aspx

To be able to allow the user to select the application, you should
enumerate the running processes and save the selection's process ID:
System.Diagnostics.Process.GetProcesses();

Hope this helps.

Feel free to ask any further questions.

Cheers and good luck!
Moty

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.