Click here to Skip to main content
13,594,656 members
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 26 Mar 2002
Licenced CPOL

CIconDialog - Selecting Icons

, 28 Mar 2004
Rate this:
Please Sign up or sign in to vote.
The CIconDialog class allows you to add an icon-selection dialog box to your applications


The introduction of this article was: "Once developing a wizard application I needed a dialog to select an icon from executables, but did not find anything about icon selection in MSDN. So I have searched the CodeProject and found two articles on icon selection. The first solution, by PJ Naughter (article CIconDialog - Icon Selection Dialog), uses CDialog derived class with template dialog resource to display icon selection dialog and handle its behavior, and the second, by Henk Devos (article How to display the Pick Icon Dialog), uses undocumented Windows API functions to show the system built-in icon selection dialog. I prefer to use the Windows API even if undocumented to dragging resources around, and so wrote a small class that wraps the APIs published by Henk Devos.".

But this is the 21-st centuary now and even undocumented functions become documented. So finally Microsoft included the function name we imported by ordinal 62 in the shell32.dll 5.0 exports. Its name is PickIconDlg and only UNICODE version is available starting with Windows 2000. But for the compatibility we should import it by ordinal to use under Windows NT4.0, Windows 95/98 and Me and for using the ANSI version under Windows 2000 or later.

Sample Usage

The CIconDialog is derived from CCommonDialog and acts like any common dialog. See sample usage:

#ifndef __ICONDLG_H__
    #include "IconDialog.h"
void CSomeDialog::OnSomeBtnClicked( void ) 
    // If icon container file is not specified in the parameter
    // then by default it will open Shell32.dll file:
    // CIconDialog dlg( NULL, 0, this );
    // You can specify any initial file name and icon index ( if exist ).
    // In this case it will open index + 1 icon
     // ( index is 0 - based ) selected:  
    CIconDialog dlg( _T( "%SystemRoot%\\system32\\SHELL32.dll" ), 
        148, this );

    if( dlg.DoModal() == IDOK  )
        HICON hIcon = dlg.GetIconHandle();
        SetIcon( hIcon, FALSE );
        // Or:
        // HICON hIcon = ::ExtractIcon( AfxGetInstanceHandle(), 
        //          dlg.GetIconFile(), dlg.GetIconIndex() );

See demo project source for more.

Class Members

Base Class

  • CCommonDialog

Data Members

  • m_szIconFile - Specifies the icon file name.
  • m_dwIconIndex - Specifies icon index.
  • m_hIconHandle - Contains icon handle (last open).
  • m_uIconCount - Icon count in a file.


Constructs a CIconDialog object.

CIconDialog(LPCTSTR lpszIconFile = NULL, DWORD dwIconIndex = 0, CWnd* pParentWnd = NULL)

  • lpszIconFile - The initial icon library whose icons should be open in the dialog.
  • dwIconIndex - The icon index initially selected when the dialog opens.
  • pParentWnd - A pointer to the file dialog-box object's parent or owner window.


  • DoModal( void )- Displays the dialog box and allows the user to make a selection.
  • GetIconHandle( void ) const - Returns the handle of the last selected icon.
  • GetIconCount( void ) const - Returns the icon count of the selected icon file.
  • GetIconIndex( void ) const - Returns the index of the selected icon.
  • GetIconFile( void ) const - Returns the full path of the selected icon file.


This class compiles without any warning at level 4, and fully supports both ANSI and UNICODE.

Version Requirements

Finally Microsoft included support for the API function this class uses in the shell32.dll 5.0 or later. So we have no problem of missing ordinal using this class under:

  • Windows 2000 Pro/Server - NT 5.0.2195 with/out SP1/SP2/SP3/SP4
  • Windows XP Professional - NT 5.1.2600 with/out SP1
  • Windows 2003 Server - NT 5.2 - Standard/Enterprise/Datacenter/Web Edition

While importing by ordinal, this class was tested and ran properly under:

  • Windows 95 OSR2 - 4.00.950B
  • Windows 98 - 4.10.1998
  • Windows 98 SE - 4.10.2222A ( Second Edition )
  • Windows Me - 4.90.3000
  • Windows NT 4.0 Workstation - NT 4.0.1381 with SP 6i

It was not tested (though expected to run properly) under:

  • Windows 95 - 4.00.950
  • Windows 95 SP1 - 4.00.950A
  • Windows 95 OSR 2.5 - 4.00.950C
  • Windows NT 4.0 Workstation - NT 4.0.1381 with SP1-SP5
  • Windows NT 4.0 Server -NT 4.0.1381 with SP1-SP6i

If anyone tested it on above-mentioned versions of Windows, please post a reply.

Version History

  • 27 Mar 2002
    - Posted the article.
  • 04 Nov 2002
    - Added support for both MFC 6.0 and 7.0, some code changes.
  • 24 May 2003
    - Some code changes.
    - Added more detailed version support.
  • 29 March 2004
    - Added new PickIconDlg function information.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Armen Hakobyan
Software Developer (Senior) SafeNet Inc
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to do this in SDK? Pin
Sachin_Developer21-Apr-08 3:57
memberSachin_Developer21-Apr-08 3:57 
QuestionHow do i get the associated icon to a file class? Pin
Miguel Lopes17-Jan-03 8:38
memberMiguel Lopes17-Jan-03 8:38 
Hi. Do you know how can i get the associated icon with a file class?
I have a CListCtrl with files names from a database and i wanted to get the associated icon with each icon. All the methods i saw till now are based on searching a specific file in disk and retrieving the icon. How do i get the associated icon with a file class?

Thanks in advanced Smile | :)
AnswerRe: How do i get the associated icon to a file class? Pin
Armen Hakobyan24-Jan-03 22:56
memberArmen Hakobyan24-Jan-03 22:56 
AnswerRe: How do i get the associated icon to a file class? Pin
Armen Hakobyan23-Jul-03 10:34
memberArmen Hakobyan23-Jul-03 10:34 
GeneralCool !! Very cool !! Pin
WREY24-Sep-02 12:03
memberWREY24-Sep-02 12:03 
GeneralExcellent Pin
Brian Delahunty11-Sep-02 11:56
editorBrian Delahunty11-Sep-02 11:56 
GeneralRe: Excellent Pin
Armen Hakobyan12-Sep-02 3:51
memberArmen Hakobyan12-Sep-02 3:51 
GeneralVery good! Pin
Nish [BusterBoy]27-Mar-02 16:17
memberNish [BusterBoy]27-Mar-02 16:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 29 Mar 2004
Article Copyright 2002 by Armen Hakobyan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid