Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

The Favalias Application

Rate me:
Please Sign up or sign in to vote.
4.92/5 (27 votes)
30 Sep 20037 min read 70.8K   2.6K   52  
Favalias application enables you to manage your favorites web sites in an XML file and to launch your favorites application using aliases. You can also make your own addins (in any .NET language) to call your own code.
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using Favalias.SystemHotkeyNS.Win32;

namespace Favalias.SystemHotkeyNS
{
	/// <summary>
	/// Handles a System Hotkey
	/// </summary>
	public class SystemHotkey : System.ComponentModel.Component,IDisposable
	{
		private System.ComponentModel.Container components = null;
		protected DummyWindowWithEvent m_Window=new DummyWindowWithEvent();	//window for WM_Hotkey Messages
		protected Shortcut m_HotKey=Shortcut.None;
		protected Keys _mod;
		protected Keys _key;
		protected bool isRegistered=false;
		public event System.EventHandler Pressed;
		public event System.EventHandler Error;


		public SystemHotkey(System.ComponentModel.IContainer container)
		{
			container.Add(this);
			InitializeComponent();
			m_Window.ProcessMessage+=new MessageEventHandler(MessageEvent);
		}

		public SystemHotkey()
		{
			InitializeComponent();
			if (!DesignMode)
			{
				m_Window.ProcessMessage+=new MessageEventHandler(MessageEvent);
			}
		}

		public new void Dispose()
		{
			if (isRegistered)
			{
				if (UnregisterHotkey())
					System.Diagnostics.Debug.WriteLine("Unreg: OK");
			}
		}
		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

		protected void MessageEvent(object sender,ref Message m,ref bool Handled)
		{	//Handle WM_Hotkey event
			if ((m.Msg==(int)Msgs.WM_HOTKEY)&&(m.WParam==(IntPtr)this.GetType().GetHashCode()))
			{
				Handled=true;
				//System.Diagnostics.Debug.WriteLine("HOTKEY pressed!");
				if (Pressed!=null) Pressed(this,EventArgs.Empty);
			}
		}
	
		protected bool UnregisterHotkey()
		{	//unregister hotkey
			return User32.UnregisterHotKey(m_Window.Handle,this.GetType().GetHashCode());
		}

		protected bool RegisterHotkey(Shortcut key)
		{	//register hotkey
			int mod=0;
			Keys k2=Keys.None;
			if (((int)key & (int)Keys.Alt)==(int)Keys.Alt) {mod+=(int)Modifiers.MOD_ALT;k2=Keys.Alt;}
			if (((int)key & (int)Keys.Shift)==(int)Keys.Shift) {mod+=(int)Modifiers.MOD_SHIFT;k2=Keys.Shift;}
			if (((int)key & (int)Keys.Control)==(int)Keys.Control) {mod+=(int)Modifiers.MOD_CONTROL;k2=Keys.Control;}
			if (((int)key & (int)Keys.LWin)==(int)Keys.LWin) {mod+=(int)Modifiers.MOD_WIN;k2=Keys.LWin;}
			
			System.Diagnostics.Debug.Write(mod.ToString()+" ");
			System.Diagnostics.Debug.WriteLine((((int)key)-((int)k2)).ToString());

			return User32.RegisterHotKey(m_Window.Handle,this.GetType().GetHashCode(),(int)mod,((int)key)-((int)k2));
		}

		protected bool RegisterHotkey(Keys mod, Keys key)
		{	
			int modif = 0;
			if(((int)mod & (int)Keys.Alt) == (int)Keys.Alt) 
				modif += (int)Modifiers.MOD_ALT;
			if(((int)mod & (int)Keys.Shift) == (int)Keys.Shift) 
				modif += (int)Modifiers.MOD_SHIFT;
			if(((int)mod & (int)Keys.Control) == (int)Keys.Control) 
				modif += (int)Modifiers.MOD_CONTROL;
			if(((int)mod & (int)Keys.LWin) == (int)Keys.LWin) 
				modif += (int)Modifiers.MOD_WIN;

			System.Diagnostics.Debug.Write(((int)modif).ToString()+" ");
			System.Diagnostics.Debug.Write(((int)key).ToString()+" ");
			return User32.RegisterHotKey(m_Window.Handle,this.GetType().GetHashCode(),(int)modif, (int)key);
		}

		public bool IsRegistered
		{
			get{return isRegistered;}
		}

		public bool SetShortcut(Keys mod, Keys key)
		{
			if (DesignMode) {_mod=mod;_key=key;return false;}	//Don't register in Designmode
			if (isRegistered)	//Unregister previous registered Hotkey
			{
				if (UnregisterHotkey())
				{
					System.Diagnostics.Debug.WriteLine("Unreg hotkey: OK");
					isRegistered=false;
				}
				else 
				{
					if (Error!=null) Error(this,EventArgs.Empty);
					System.Diagnostics.Debug.WriteLine("Unreg hotkey: ERR");
				}
			}
			bool @return = false;
			//if (value==Shortcut.None) {m_HotKey=value;return;}
			if (RegisterHotkey(mod, key))	//Register new Hotkey
			{
				System.Diagnostics.Debug.WriteLine("Reg hotkey: OK");
				isRegistered=true;
				@return = true;
			}
			else 
			{
				if (Error!=null) Error(this,EventArgs.Empty);
				System.Diagnostics.Debug.WriteLine("Reg hotkey: ERR");
			}
			_mod = mod;
			_key = key;
			return @return;
		}

		[DefaultValue(Shortcut.None)]
		public Shortcut Shortcut
		{
			get { return m_HotKey; }
			set 
			{ 
				if (DesignMode) {m_HotKey=value;return;}	//Don't register in Designmode
				if ((isRegistered)&&(m_HotKey!=value))	//Unregister previous registered Hotkey
				{
					if (UnregisterHotkey())
					{
						System.Diagnostics.Debug.WriteLine("Unreg hotkey: OK");
						isRegistered=false;
					}
					else 
					{
						if (Error!=null) Error(this,EventArgs.Empty);
						System.Diagnostics.Debug.WriteLine("Unreg hotkey: ERR");
					}
				}
				if (value==Shortcut.None) {m_HotKey=value;return;}
				if (RegisterHotkey(value))	//Register new Hotkey
				{
					System.Diagnostics.Debug.WriteLine("Reg hotkey: OK");
					isRegistered=true;
				}
				else 
				{
					if (Error!=null) Error(this,EventArgs.Empty);
					System.Diagnostics.Debug.WriteLine("Reg hotkey: ERR");
				}
				m_HotKey=value;
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
I am an MCSD.NET and MCT. I give a lot of Microsoft Trainings (www.bdcworld.com) in France.

Comments and Discussions