如何获得带有预览的文件夹图标?

时间:2019-11-13 10:15:14

标签: c winapi icons

我想让一个文件夹图标看起来像这样

like this

但是我实际上看起来像这样

like this

我的实现:

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);
}

我应该如何更改?

1 个答案:

答案 0 :(得分:0)

这里是用于此目的的官方示例,您可以使用或参考:“ Image Factory API Sample”。它演示了如何使用IShellItemImageFactory接口来获取项目的最佳图像。

并确保您的文件夹与File Explorer中的文件夹完全一样。

这是我的目标文件夹:

enter image description here

这是我使用样本得到的:

enter image description here

与核心相关的代码行:

        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();
        }