Export (0) Print
Expand All

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)

protected virtual IntPtr HookProc (
	IntPtr hWnd,
	int msg,
	IntPtr wparam,
	IntPtr lparam
)
protected IntPtr HookProc (
	IntPtr hWnd, 
	int msg, 
	IntPtr wparam, 
	IntPtr lparam
)
protected function HookProc (
	hWnd : IntPtr, 
	msg : int, 
	wparam : IntPtr, 
	lparam : IntPtr
) : IntPtr

Parameters

hWnd

The handle to the dialog box window.

msg

The message being received.

wparam

Additional information about the message.

lparam

Additional information about the message.

Return Value

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.

NoteNote

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.

	switch(msg)
	{

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

	}

	// Always call the base class hook procedure.

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

}

    // Defines the constants for Windows messages.
    final int WM_SETFOCUS = 0x7;
    final int WM_INITDIALOG = 0x110;
    final int WM_LBUTTONDOWN = 0x201;
    final int WM_RBUTTONDOWN = 0x204;
    final int WM_MOVE = 0x3;

    // Overrides the base class hook procedure...
//    /** @attribute System.Security.Permissions.PermissionSet(System.Security.
//        Permissions.SecurityAction.Demand, Name = "FullTrust")
//     */
    /** @attribute SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)
     */
    protected IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, 
        IntPtr lParam)
    {
        // Evaluates the message parameter to determine the user action.
        switch (msg) {
            case WM_INITDIALOG :
                System.Diagnostics.Trace.Write(
                    "The WM_INITDIALOG message was received.");
                break;

            case WM_SETFOCUS :
                System.Diagnostics.Trace.Write(
                    "The WM_SETFOCUS message was received.");
                break;

            case WM_LBUTTONDOWN :
                System.Diagnostics.Trace.Write(
                    "The WM_LBUTTONDOWN message was received.");
                break;

            case WM_RBUTTONDOWN :
                System.Diagnostics.Trace.Write(
                    "The WM_RBUTTONDOWN message was received.");
                break;

            case WM_MOVE :
                System.Diagnostics.Trace.Write(
                    "The WM_MOVE message was received.");
                break;
        }

        // Always call the base class hook procedure.
        return super.HookProc(hWnd, msg, wParam, lParam);

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft