This documentation is archived and is not being maintained.

CommonDialog.HookProc Method

Defines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected virtual IntPtr HookProc(
	IntPtr hWnd,
	int msg,
	IntPtr wparam,
	IntPtr lparam


Type: System.IntPtr
The handle to the dialog box window.
Type: System.Int32
The message being received.
Type: System.IntPtr
Additional information about the message.
Type: System.IntPtr
Additional information about the message.

Return Value

Type: System.IntPtr
A zero value if the default dialog box procedure processes the message; a nonzero value if the default dialog box procedure ignores the message.

A hook procedure is a mechanism by which a function can intercept events before they reach an application. When you override the HookProc method for a CommonDialog class, the operating system invokes your override of the function to post operating system messages to the window.

By default, the hook procedure centers the dialog box on the screen in response to a WM_INITDIALOG message.


This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

This property also uses the SecurityAction.InheritanceDemand security attribute; to override this member, the derived class must have the CustomPermission permission.

Notes to Inheritors

Inheriting classes can override this method to add specific functionality to a common dialog box. When overriding HookProc in a derived class, be sure to call the base class's HookProc method.

The following code example demonstrates how to override the HookProc method. The example consists of a class that inherits the CommonDialog class. In the class's HookProc override, the example evaluates the method's msg parameter against constant values for particular Windows messages. If the msg parameter equals the specified constant, the example writes trace output identifying the Windows message that was passed to the HookProc method. This example assumes that the class in which the HookProc method is declared inherits the CommonDialog class.

// Defines the constants for Windows messages.

const int WM_SETFOCUS = 0x0007;
const int WM_INITDIALOG = 0x0110;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_RBUTTONDOWN = 0x0204;
const int WM_MOVE = 0x0003;

// Overrides the base class hook procedure...
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)

	// Evaluates the message parameter to determine the user action.


			System.Diagnostics.Trace.Write("The WM_INITDIALOG message was received.");
			System.Diagnostics.Trace.Write("The WM_SETFOCUS message was received.");
			System.Diagnostics.Trace.Write("The WM_LBUTTONDOWN message was received.");
			System.Diagnostics.Trace.Write("The WM_RBUTTONDOWN message was received.");
		case WM_MOVE:
			System.Diagnostics.Trace.Write("The WM_MOVE message was received.");


	// Always call the base class hook procedure.

	return base.HookProc(hWnd, msg, wParam, lParam);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.