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!