SHGetFileInfoW Returns the system index number of the icon

XUWANBIN 201 Reputation points
2023-01-13T13:54:32.1466667+00:00

Now I have the index number of the icon (say 81).

How to get the icon using the index number,(I know that SHGetFileInfoW can get the icon directly),

I want to save the index and call it when I need it

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,712 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,496 Reputation points
    2023-01-13T14:27:15.08+00:00

    For example, a test of Notepad icon :

                        SHFILEINFO sfi = { 0 };
                        HIMAGELIST hImageList = (HIMAGELIST)SHGetFileInfo(L"C:\\WINDOWS\\system32\\notepad.exe", 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);
                        if (hImageList)
                        {
                            int nX = 10, nY = 10;
                            HDC hDC = GetDC(NULL);
                            DrawIconEx(hDC, nX, nY, ImageList_GetIcon(hImageList, sfi.iIcon, ILD_NORMAL), 0, 0, 0, NULL, DI_NORMAL);
                            ReleaseDC(NULL, hDC);
                        }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 119.2K Reputation points
    2023-01-13T14:42:06.1333333+00:00

    According to documentation and some articles, you can obtain the system image list using a code like this:

    SHFILEINFO unused;
    HIMAGELIST hSysImgList = (HIMAGELIST)SHGetFileInfo(
           "C:", 
           FILE_ATTRIBUTE_NORMAL, 
           &unused, 
           sizeof(SHFILEINFO), 
           SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_ICON);
    

    The name of the file does not matter. Maybe you should also specify the SHGFI_SMALLICON flag.

    Then use ImageList_GetIcon to get the icon by index.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.