LPOFNHOOKPROC callback function (commdlg.h)

[Starting with Windows Vista, the Open and Save As common dialog boxes have been superseded by the Common Item Dialog. We recommended that you use the Common Item Dialog API instead of these dialog boxes from the Common Dialog Box Library.]

Receives notification messages sent from the dialog box. The function also receives messages for any additional controls that you defined by specifying a child dialog template. The OFNHookProc hook procedure is an application-defined or library-defined callback function that is used with the Explorer-style Open and Save As dialog boxes.

The LPOFNHOOKPROC type defines a pointer to this callback function. OFNHookProc is a placeholder for the application-defined function name.


LPOFNHOOKPROC Lpofnhookproc;

UINT_PTR Lpofnhookproc(
  HWND unnamedParam1,
  UINT unnamedParam2,
  WPARAM unnamedParam3,
  LPARAM unnamedParam4



A handle to the child dialog box of the Open or Save As dialog box. Use the GetParent function to get the handle to the Open or Save As dialog box.


The identifier of the message being received.


Additional information about the message. The exact meaning depends on the value of the unnamedParam2 parameter.


Additional information about the message. The exact meaning depends on the value of the unnamedParam2 parameter. If the unnamedParam2 parameter indicates the WM_INITDIALOG message, unnamedParam4 is a pointer to an OPENFILENAME structure containing the values specified when the dialog box was created.

Return value

If the hook procedure returns zero, the default dialog box procedure processes the message.

If the hook procedure returns a nonzero value, the default dialog box procedure ignores the message.

For the CDN_SHAREVIOLATION and CDN_FILEOK notification messages, the hook procedure should return a nonzero value to indicate that it has used the SetWindowLong function to set a nonzero DWL_MSGRESULT value.


If you do not specify the OFN_EXPLORER flag when you create an Open or Save As dialog box, and you want a hook procedure, you must use an old-style OFNHookProcOldStyle hook procedure. In this case, the dialog box will have the old-style user interface.

When you use the GetOpenFileName or GetSaveFileName functions to create an Explorer-style Open or Save As dialog box, you can provide an OFNHookProc hook procedure. To enable the hook procedure, use the OPENFILENAME structure that you passed to the dialog creation function. Specify the pointer to the hook procedure in the lpfnHook member and specify the OFN_ENABLEHOOK flag in the Flags member.

If you provide a hook procedure for an Explorer-style common dialog box, the system creates a dialog box that is a child of the default dialog box. The hook procedure acts as the dialog procedure for the child dialog. This child dialog is based on the template you specified in the OPENFILENAME structure, or it is a default child dialog if no template is specified. The child dialog is created when the default dialog procedure is processing its WM_INITDIALOG message. After the child dialog processes its own WM_INITDIALOG message, the default dialog procedure moves the standard controls, if necessary, to make room for any additional controls of the child dialog. The system then sends the CDN_INITDONE notification message to the hook procedure.

The hook procedure does not receive messages intended for the standard controls of the default dialog box. You can subclass the standard controls, but this is discouraged because it may make your application incompatible with later versions. However, the Explorer-style common dialog boxes provide a set of messages that the hook procedure can use to monitor and control the dialog. These include a set of notification messages sent from the dialog, as well as messages that you can send to retrieve information from the dialog. For a complete list of these messages, see Explorer-Style Hook Procedures.

If the hook procedure processes the WM_CTLCOLORDLG message, it must return a valid brush handle to painting the background of the dialog box. In general, if it processes any WM_CTLCOLOR* message, it must return a valid brush handle to painting the background of the specified control.

Do not call the EndDialog function from the hook procedure. Instead, the hook procedure can call the PostMessage function to post a WM_COMMAND message with the IDCANCEL value to the dialog box procedure. Posting IDCANCEL closes the dialog box and causes the dialog box function to return FALSE. If you need to know why the hook procedure closed the dialog box, you must provide your own communication mechanism between the hook procedure and your application.


Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header commdlg.h (include Windows.h)

See also

Common Dialog Box Library