How to Change Small Icon for FileOpen and Other Common Dialogs

Last reviewed: September 29, 1995
Article ID: Q130758
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95 version 4.0
    

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.

Code Sample

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 reference words: 4.00 user common dialog
KBCategory: kbui kbcode
KBSubcategory: UsrCmnDlg


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.