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 / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Catch a mouse click on any app in the taskbar

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jay.maiurano@gmail.com - 19 Aug 2006 21:04 GMT
I'd like to get the handle of any application that is clicked in the
taskbar.  Any thoughts?  Thanks!
Jared - 20 Aug 2006 18:25 GMT
Jay,
.Net doesn't support Global hooks (http://support.microsoft.com/kb/318804/),
which is the only way I know of to do exactly what you are asking.  However,
you should be able to use a workaround if your application doesn't need to
interject/handle the actual mouse click event.  The attached cllass will
allow you to register an event handler and wait for the handle.  It works off
a timer set to 500ms which may/may not be acceptable for you.  Either way,
you can modify it to fit your needs.

Jared

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace NewsgroupWindowsApp
{
   public sealed class TaskBarMonitor : IDisposable
   {

       #region Ctors/Finalizer

       static TaskBarMonitor()
       {
           _instance = new TaskBarMonitor();
       }

       private TaskBarMonitor()
       {
           _foregroundWindow = Win32.GetForegroundWindow();
           _timer = new System.Timers.Timer(500);
           _timer.Elapsed += new
System.Timers.ElapsedEventHandler(_timer_Elapsed);
           _timer.Start();
       }

       ~TaskBarMonitor()
       {
           this.Dispose();
       }

       #endregion

       #region Fields

       private static TaskBarMonitor _instance;
       private static IntPtr _foregroundWindow;
       private static System.Timers.Timer _timer;

       #endregion

       #region Properties

       public static TaskBarMonitor Instance
       {
           get { return _instance; }
       }

       #endregion

       #region Delegates/Events
       
       public delegate void ForegroundWindowChangedHandler(object sender,
ForgroundWindowChangedEventArgs e);
       public event ForegroundWindowChangedHandler ForgroundWindowChanged;

       #endregion

       #region Methods/Handlers

       private void OnForegroundWindowChanged()
       {
           if (ForgroundWindowChanged != null)
           {
               ForgroundWindowChangedEventArgs e = new
ForgroundWindowChangedEventArgs(_foregroundWindow);
               ForgroundWindowChanged(this, e);
           }
       }
       
       void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
       {
           IntPtr currentWindow = Win32.GetForegroundWindow();
           if (currentWindow.Equals(_foregroundWindow) == false)
           {
               _foregroundWindow = currentWindow;
               this.OnForegroundWindowChanged();
           }
       }

       #endregion

       #region IDisposable Members

       public void Dispose()
       {
           _timer.Dispose();
       }

       #endregion

       public class ForgroundWindowChangedEventArgs
       {
           #region Ctors

           public ForgroundWindowChangedEventArgs(IntPtr windowHandle)
           {
               _title = this.GetTitle(windowHandle);
           }

           #endregion

           #region Fields

           private string _title;

           #endregion

           #region Properties

           public string Title {
               get
               {
                   if (string.IsNullOrEmpty(_title))
                   {
                       return "Unknown";
                   }
                   return _title;
               }
           }

           public IntPtr Handle
           {
               get { return _foregroundWindow; }
           }

           #endregion

           #region Methods

           private string GetTitle(IntPtr windowHandle)
           {
               StringBuilder sb = new StringBuilder(255);
               Win32.GetWindowText(windowHandle, sb, sb.Capacity);
               return sb.ToString();
           }

           #endregion
       }

       private static class Win32
       {

       [DllImport("user32.dll")]
       public static extern IntPtr GetForegroundWindow();

       [DllImport("user32.dll", EntryPoint = "GetWindowTextA")]
       public static extern int GetWindowText(
            IntPtr hwnd,
            StringBuilder lpString,
            int cch);

       [DllImport("kernel32.dll", EntryPoint = "FormatMessageA")]
       public static extern int FormatMessage(
            int dwFlags,
            int lpSource,
            int dwMessageId,
            int dwLanguageId,
            StringBuilder lpBuffer,
            int nSize,
            int Arguments);

       }

   }
}

> I'd like to get the handle of any application that is clicked in the
> taskbar.  Any thoughts?  Thanks!
Jay Maiurano - 21 Aug 2006 15:11 GMT
Thanks Jared!

> Jay,
> .Net doesn't support Global hooks (http://support.microsoft.com/kb/318804/),
[quoted text clipped - 176 lines]
> > I'd like to get the handle of any application that is clicked in the
> > taskbar.  Any thoughts?  Thanks!

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.