none
How to get a folder icon with a preview? RRS feed

  • Question

  • Hi to all,

    I wanted to get folder icon with a preview ,but I get result  is empty folder icon:

    HICON GetSystemIcon(IShellFolder* folder, PCIDLIST_ABSOLUTE idpl) {
      IExtractIcon* extract_icon;
      folder->GetUIObjectOf(NULL, 1, &idpl, IID_IExtractIcon, NULL, (void**)&extract_icon);
      wchar_t icon_file[MAX_PATH] = {0};
      int icon_index = 0;
      UINT flag = GIL_SIMULATEDOC;
      extract_icon->GetIconLocation(GIL_FORSHELL, icon_file, MAX_PATH, &icon_index, &flag);
      HICON small_icon = NULL;
      HICON large_icon = NULL;
      UINT size = MAKELONG(32, 32);
      extract_icon->Extract(icon_file, icon_index, &large_icon, &small_icon, size);
    }

    How to do?

    Wednesday, November 13, 2019 9:57 AM

Answers

  • Hello jimmy_q,

    Here is an official sample for this purpose you can use or refer to: "Image Factory API Sample". It demonstrates how to use the IShellItemImageFactory interface to get the best possible image for an item.

    And make sure your folder looks exactly what you expected in File explorer.

    This is my target folder:

    enter image description here

    This is what I get using the sample:

    enter image description here

    The core related code lines:

            HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
            if (SUCCEEDED(hr))
            {
                // Getting the IShellItemImageFactory interface pointer for the file.
                IShellItemImageFactory *pImageFactory;
                hr = SHCreateItemFromParsingName(argv[2], NULL, IID_PPV_ARGS(&pImageFactory));
                if (SUCCEEDED(hr))
                {
                    SIZE size = { nSize, nSize };
    
                    //sz - Size of the image, SIIGBF_BIGGERSIZEOK - GetImage will stretch down the bitmap (preserving aspect ratio)
                    HBITMAP hbmp;
                    hr = pImageFactory->GetImage(size, SIIGBF_BIGGERSIZEOK, &hbmp);
                    if (SUCCEEDED(hr))
                    {
                        DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, DialogProc, (LPARAM)hbmp);
                        DeleteObject(hbmp);
                    }
                    else
                    {
                        pwszError = L"IShellItemImageFactory::GetImage failed with error code %x";
                    }
    
                    pImageFactory->Release();
                }
                else
                {
                    pwszError = L"SHCreateItemFromParsingName failed with error %x";
                }
    
                CoUninitialize();
            }

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jimmy_q Thursday, November 14, 2019 1:31 AM
    Thursday, November 14, 2019 1:12 AM

All replies

  • Hello jimmy_q,

    Here is an official sample for this purpose you can use or refer to: "Image Factory API Sample". It demonstrates how to use the IShellItemImageFactory interface to get the best possible image for an item.

    And make sure your folder looks exactly what you expected in File explorer.

    This is my target folder:

    enter image description here

    This is what I get using the sample:

    enter image description here

    The core related code lines:

            HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
            if (SUCCEEDED(hr))
            {
                // Getting the IShellItemImageFactory interface pointer for the file.
                IShellItemImageFactory *pImageFactory;
                hr = SHCreateItemFromParsingName(argv[2], NULL, IID_PPV_ARGS(&pImageFactory));
                if (SUCCEEDED(hr))
                {
                    SIZE size = { nSize, nSize };
    
                    //sz - Size of the image, SIIGBF_BIGGERSIZEOK - GetImage will stretch down the bitmap (preserving aspect ratio)
                    HBITMAP hbmp;
                    hr = pImageFactory->GetImage(size, SIIGBF_BIGGERSIZEOK, &hbmp);
                    if (SUCCEEDED(hr))
                    {
                        DialogBoxParamW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, DialogProc, (LPARAM)hbmp);
                        DeleteObject(hbmp);
                    }
                    else
                    {
                        pwszError = L"IShellItemImageFactory::GetImage failed with error code %x";
                    }
    
                    pImageFactory->Release();
                }
                else
                {
                    pwszError = L"SHCreateItemFromParsingName failed with error %x";
                }
    
                CoUninitialize();
            }

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jimmy_q Thursday, November 14, 2019 1:31 AM
    Thursday, November 14, 2019 1:12 AM
  • Thanks!This is answer I want.
    Thursday, November 14, 2019 2:02 AM
  • Glad to help.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 14, 2019 3:36 AM
  • You can also use IThumbnailCache (tested on Windows 10)

    Friday, November 15, 2019 2:00 PM