Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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"

#endif
//...

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

Data Members

Construction

Constructs a CIconDialog object.

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

Operations

Notes

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:

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

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

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

Version History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow to do this in SDK?
Sachin_Developer
4:57 21 Apr '08  
How to do this in SDK?

Sky is the limit...

GeneralHow do i get the associated icon to a file class?
Miguel Lopes
9:38 17 Jan '03  
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
GeneralRe: How do i get the associated icon to a file class?
Armen Hakobyan
23:56 24 Jan '03  
SHFILEINFO shfi = { 0 };
::SHGetFileInfo( _T( "c:\\boot.ini" ), FILE_ATTRIBUTE_NORMAL,
&shfi, sizeof( SHFILEINFO ), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES );

if( shfi.hIcon != NULL )
{
CStatic* pImg = ( CStatic* )GetDlgItem( IDC_STATIC_1 );
if( pImg != NULL )
VERIFY( ::DestroyIcon( pImg->SetIcon( shfi.hIcon ) ) );
};


#ifndef
#define __ARMEN_H__
#endif
GeneralRe: How do i get the associated icon to a file class?
Armen Hakobyan
11:34 23 Jul '03  
Or ExtractAssociatedIcon

#define __ARMEN_H__
GeneralCool !! Very cool !!
WREY
13:03 24 Sep '02  
Very well done!!

Long may you reign!!

Cool

William
GeneralExcellent
Brian Delahunty
12:56 11 Sep '02  
Very good article and the code is excellent. I'm giving it a 5.

Regards,
Brian Dela Smile
GeneralRe: Excellent
Armen Hakobyan
4:51 12 Sep '02  
Thanks. Smile

#ifndef
#define __ARMEN_H__
#endif
GeneralVery good!
Nish [BusterBoy]
17:17 27 Mar '02  
Smile

Nish

p.s. Get a 5 from me!!!

I am the Keyboard Smasher



Last Updated 29 Mar 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010