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

Definition

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

C#
protected virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);

Parameters

hWnd
IntPtr

The handle to the dialog box window.

msg
Int32

The message being received.

wparam
IntPtr

Additional information about the message.

lparam
IntPtr

Additional information about the message.

Returns

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.

Examples

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.

C#

// 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...
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);
}

Remarks

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.

Notes to Inheritors

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

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10