HOWTO: Change Small Icon for FileOpen and Other Common Dialogs

ID: Q130758

The information in this article applies to:

SUMMARY

Applications that need to display icons on the caption bars of File Open and other Common Dialogs can do so by installing a hook function and sending the WM_SETICON message from within the hook function to the common dialog to change its small icon. Note that OFN_ENABLEHOOK flag (or relavant flags for other common dialogs) has to be set for your hook function to be called.

MORE INFORMATION

Under Windows 95, every popup or overlapped window can have two icons associated with it, a large icon used when the window is minimized and a small icon used for displaying the system menu icon.

Common Dialogs under Windows 95 do not display a small icon on their caption bars by default. If you want the application to display its own icon for the system menu, have the application install a hook funtion for that common dialog and send the WM_SETICON message when the hook callback function is called with the WM_INITDIALOG message.

The WM_SETICON message is sent to change or set the small and large icons of a window. In this case, because you are setting the small icon, wParam must be set to FALSE.

Sample Code

The following code shows how to do this for a File Open Common Dialog:

   // Fill in the OPENFILENAME structure to support
   // a hook and  a template (optional).

   OpenFileName.lStructSize       = sizeof(OPENFILENAME);
   OpenFileName.hwndOwner      = hWnd;
   OpenFileName.hInstance         = g_hInst;
     ...
     ...
     ...
     ...

   OpenFileName.lpfnHook          = ComDlg32HkProc;
   OpenFileName.lpTemplateName  = NULL;
   OpenFileName.Flags               = OFN_SHOWHELP |
                   OFN_EXPLORER | OFN_ENABLE_HOOK;

Note that the lpTemplateName parameter is set to NULL. To just install a hook, one does not need a custom template. The hook function will get called if it is sepcified in the structure.

Below is the Comdlg32HkgProc hook callback funtion that chages the small icon. This code below is for the open or save as dialog boxes only.

   BOOL CALLBACK ComDlg32HkProc(HWND hDlg,
                                UINT uMsg,
                                WPARAM wParam,
                                LPARAM lPar
   {

      HWND hWndParent;
      HICON  hIcon;

      switch (uMsg)
      {
         case WM_INITDIALOG:

            hWndParent = GetParent(hDlg);

            hIcon = LoadIcon(g_hInst, "CustomIcon");

            SendMessage(hWndParent,
                        WM_SETICON,
                       (WPARAM)(BOOL)FALSE,
                       (LPARAM)(HICON)hIcon);

           return TRUE;

           break;

         default:
           break;

       }

NOTE: This code calls GetParent() to get the actual window handle of the common dialog box. This is done for the FileOpen and SaveAs dialog boxes only. These dialogs, when created with the OFN_EXPLORER look with a hook and a template (optional), create a seperate dialog to hold all the controls. This is the dialog handle that is passed in the hook function. The parent of this dialog is the main common dialog window, whose caption icon must be modified. The FileOpen and SaveAs dialog boxes with the old style (no OFN_EXPLORER) need not call GetParent().

All other common dialogs, such as ChooseColor and ChooseFont, behave as the the Windows version 3.1 common dialogs behaved, so the code listed in this article does not need to call GetParent(). It can just send the WM_SETICON message to the hDlg that is passed to the hook function.

Additional query words: user common dialog

Keywords          : kbcode kbCmnDlg kbCmnDlgFileO kbCmnDlgSave kbNTOS400 kbGrpUser kbWinOS95 kbWinOS98 
Issue type        : kbhowto

Last Reviewed: December 26, 1998