Click here to Skip to main content
15,883,938 members
Articles / Programming Languages / C#

Using Hooks from C#

Rate me:
Please Sign up or sign in to vote.
4.58/5 (39 votes)
30 Dec 2009CPOL5 min read 504.7K   21.2K   193  
An article on using Windows hooks from .NET, demonstrated with a MouseHook.
using System;
using System.ComponentModel;
using System.Diagnostics;

namespace Microsoft.Win32
{
	/// <summary>
	/// A component based MouseHook that can be placed on a design surface
	/// and automatically installs itself when created with a valid container
	/// </summary>
	public class MouseHookComponent : System.ComponentModel.Component
	{
		private MouseHook hook = null;

		/// <summary>
		/// Creates a new instance of the MouseHookComponent class
		/// and installs the hook
		/// </summary>
		/// <param name="container">Parent Component container</param>
		public MouseHookComponent(System.ComponentModel.IContainer container) : this()
		{
			container.Add( this );

			if ( !base.DesignMode )
				hook.Install();
		}

		/// <summary>
		/// Creates a new instance of the MouseHookComponent class
		/// and does not install the hook. The client is responsible for calling install
		/// </summary>
		public MouseHookComponent()
		{
			hook = new MouseHook();

			hook.MouseDoubleClick += new MouseHookEventHandler(hook_MouseDoubleClick);
			hook.MouseDown += new MouseHookEventHandler(hook_MouseDown);
			hook.MouseMove += new MouseHookEventHandler(hook_MouseMove);
			hook.MouseUp += new MouseHookEventHandler(hook_MouseUp);
		}

		#region Disposer
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			hook.Dispose();

			base.Dispose( disposing );
		}
		#endregion

		#region Events
		public event MouseHookEventHandler MouseDown;
		private void hook_MouseDown(object sender, MouseHookEventArgs e)
		{
			if ( MouseDown != null )
				MouseDown( this, e );
		}

		public event MouseHookEventHandler MouseUp;
		private void hook_MouseUp(object sender, MouseHookEventArgs e)
		{
			if ( MouseUp != null )
				MouseUp( this, e );
		}

		public event MouseHookEventHandler MouseMove;
		private void hook_MouseMove(object sender, MouseHookEventArgs e)
		{
			if ( MouseMove != null )
				MouseMove( this, e );
		}

		public event MouseHookEventHandler MouseDoubleClick;
		private void hook_MouseDoubleClick(object sender, MouseHookEventArgs e)
		{
			if ( MouseDoubleClick != null )
				MouseDoubleClick( this, e );
		}
		#endregion

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions