CommonDialog.HookProc Method (IntPtr, Int32, IntPtr, IntPtr)


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)

	Flags := SecurityPermissionFlag.UnmanagedCode)>
<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.UnmanagedCode)>
Protected Overridable Function HookProc (
	hWnd As IntPtr,
	msg As Integer,
	wparam As IntPtr,
	lparam As IntPtr
) As IntPtr


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 WM_SETFOCUS = &H7
    Const WM_INITDIALOG = &H110
    Const WM_LBUTTONDOWN = &H201
    Const WM_RBUTTONDOWN = &H204
    Const WM_MOVE = &H3
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function HookProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

        ' Evaluates the message parameter to determine the user action.

        Select Case msg

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

        End Select

        ' Always call the base class hook procedure.

        Return MyBase.HookProc(hWnd, msg, wParam, lParam)

    End Function


for inheriting classes to call unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode


for the immediate caller to call unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode

.NET Framework
Available since 1.1
Return to top