Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Window Messages to Implement Global System Hooks in C#

, 3 May 2007
Explains how to implement any type of global system hook in C# by using an unmanaged C++ DLL and Windows messages.
wilsonglobalsystemhooks_src.zip
GlobalCbtHook
Debug
GlobalCbtHook.dll
GlobalCbtHook.aps
GlobalCbtHook.def
Release
GlobalHooksTest
App.ico
bin
Debug
GlobalCbtHook.dll
Release
GlobalHooksTest.exe
GlobalHooksTest.csproj.user
TransparencyMenu
App.ico
bin
Debug
GlobalCbtHook.dll
Release
TransparencyMenu.csproj.user
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace TransparencyMenu
{
	class Win32
	{
		public delegate bool EnumWindowDelegate(IntPtr hwnd, int lParam);

		[DllImport("user32.dll")]
		public static extern int EnumWindows(EnumWindowDelegate x, int y);
		[DllImport("user32.dll")]
		public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
		[DllImport("user32.dll")]
		public static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
		[DllImport("user32.dll")]
		public static extern bool IsWindowVisible(IntPtr hWnd);
		[DllImport("user32.dll")]
		public static extern int RegisterWindowMessage(string lpString);
		[DllImport("user32.dll")]
		public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool Revert);
		[DllImport("user32.dll")]
		public static extern bool InsertMenu(IntPtr hMenu, int uPosition, int uFlags, IntPtr uIDNewItem, string lpNewItem);
		[DllImport("user32.dll")]
		public static extern bool InsertMenu(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, string lpNewItem);
		[DllImport("user32.dll")]
		public static extern bool RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);
		[DllImport("user32.dll")]
		public static extern int GetMenuItemCount(IntPtr hMenu);
		[DllImport("user32.dll")]
		public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, int dwFlags);
		[DllImport("user32.dll")]
		public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
		[DllImport("user32.dll")]
		public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
		[DllImport("user32.dll")]
		public static extern IntPtr CreateMenu();
		[DllImport("user32.dll")]
		public static extern bool DestroyMenu(IntPtr hMenu);

		// Menus
		public static int MF_STRING = 0x00000000;
		public static int MF_POPUP = 0x00000010;
		public static int MF_BYCOMMAND = 0x00000000;
		public static int MF_BYPOSITION = 0x00000400;
		public static int MF_SEPARATOR = 0x00000800;

		// GetWindow
		public static int GW_OWNER = 4;

		// LayeredWindowAttributes
		public static int LWA_COLORKEY = 0x00000001;
		public static int LWA_ALPHA = 0x00000002;

		// WindowLong
		public static int GWL_WNDPROC         = (-4);
		public static int GWL_HINSTANCE       = (-6);
		public static int GWL_HWNDPARENT      = (-8);
		public static int GWL_STYLE           = (-16);
		public static int GWL_EXSTYLE         = (-20);
		public static int GWL_USERDATA        = (-21);
		public static int GWL_ID              = (-12);

		// WindowStyle
		public static int WS_EX_LAYERED = 0x00080000;

		// Window Messages
		public static int WM_CREATE = 0x0001;
		public static int WM_DESTROY = 0x0002;
		public static int WM_MOVE = 0x0003;
		public static int WM_SIZE = 0x0005;
		public static int WM_ACTIVATE = 0x0006;
		public static int WM_COMMAND = 0x0111;
		public static int WM_SYSCOMMAND = 0x0112;
		public static int WM_MENUCOMMAND = 0x0126;
		public static int WM_MENUSELECT = 0x011F;
	}
}

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 GNU General Public License (GPLv3)

Share

About the Author

ChrisP1118

Burkina Faso Burkina Faso
I currently live in a small town in Burkina Faso, where I spend some of my free time (during the hours when there's electricity) playing around with C#. For a bit more about my less nerdy activities, check out http://chrisburkina.blogspot.com or http://www.youtube.com/watch?v=Rw1Pb7hlIWw -- neither of them, however, has anything to do with C#.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 3 May 2007
Article Copyright 2007 by ChrisP1118
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid