给定KNOWNFOLDERID或其GUID,在Windows下获取相关图标。

问题描述 投票:0回答:1

寻找一种使用C++获得与桌面快捷方式相关联的图标的方法,我只有它的GUID--类似{82A74AEB-AEB4-465C-A014-D097EE346D63}或它的KNOWNFOLDERID。

c++ windows winapi
1个回答
2
投票

类似这样,基于 IShellItemImageFactory 界面,从Windows Vista开始就存在。

CoInitialize(NULL);
{
    CComPtr<IKnownFolderManager> mgr;
    if (SUCCEEDED(mgr.CoCreateInstance(CLSID_KnownFolderManager)))
    {
        CComPtr<IKnownFolder> folder;
        if (SUCCEEDED(mgr->GetFolder(FOLDERID_ControlPanelFolder, &folder)))
        {
            CComHeapPtr<ITEMIDLIST> pidl;
            // I pass 0 for flags here, but mileage may differ
            if (SUCCEEDED(folder->GetIDList(0, &pidl)))
            {
                CComPtr<IShellItemImageFactory> factory;
                if (SUCCEEDED(SHCreateItemFromIDList(pidl, IID_PPV_ARGS(&factory))))
                {
                    // you can vary size and SIIGBF flags
                    HBITMAP bmp = nullptr;
                    SIZE size = { 256, 256 };
                    if (SUCCEEDED(factory->GetImage(size, SIIGBF_ICONONLY, &bmp)))
                    {
                        // etc...
                        DeleteObject(bmp);
                    }
                }
            }
        }
    }
}
CoUninitialize();

还有一个更好的方法就是使用 SHCreateItemInKnownFolder函数。:

CoInitialize(NULL);
{
    CComPtr<IShellItemImageFactory> factory;
    if (SUCCEEDED(SHCreateItemInKnownFolder(FOLDERID_ControlPanelFolder, 0, NULL, IID_PPV_ARGS(&factory))))
    {
        // you can vary size and SIIGBF flags
        HBITMAP bmp = nullptr;
        SIZE size = { 256, 256 };
        if (SUCCEEDED(factory->GetImage(size, SIIGBF_ICONONLY, &bmp)))
        {
            // etc...
            DeleteObject(bmp);
        }
    }
}
CoUninitialize();

请注意,这将使你得到一个HBITMAP的图标。

© www.soinside.com 2019 - 2024. All rights reserved.