首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在CPP中获得Windows应用程序的图标?

如何在CPP中获得Windows应用程序的图标?
EN

Stack Overflow用户
提问于 2022-04-03 19:00:51
回答 1查看 311关注 0票数 1

我正试图在Win32中构建一个文件管理器,而且图标也有问题。每当我试图获得像.png (Photos )这样的windows 10应用程序的图标时,图标就是白纸。我做错什么了?事先谢谢,请回答:)

代码语言:javascript
运行
复制
BOOL InitTreeViewImageLists(HWND hwndTV) {
    HIMAGELIST himl;  // handle to image list 
    HBITMAP hbmp;     // handle to bitmap 


    // Create the image list. 
    if ((himl = ImageList_Create(16,
        16,
        ILC_COLOR16 | ILC_MASK,
        3, 0)) == NULL)
        return FALSE;


    // Add the open file, closed file, and document bitmaps.
    HICON hIcon;
    SHFILEINFO sfi;
    LPCWSTR path = L"C:\\Users\\Shalev\\Desktop\\WhatsApp.lnk";
    sfi = GetShellInfo(path);
    HICON* iconHandles = new HICON;
    hIcon = sfi.hIcon;
    cout << hIcon << endl;
    g_nOpen = sfi.iIcon;
    ImageList_AddIcon(himl, hIcon);
    sfi = GetShellInfo(L"C:\\");
    hIcon = sfi.hIcon;
    g_nClosed = sfi.iIcon;
    ImageList_AddIcon(himl, hIcon);
    sfi = GetShellInfo(L"C:\\");
    hIcon = sfi.hIcon;
    g_nDocument = sfi.iIcon;
    ImageList_AddIcon(himl, hIcon);

    // Associate the image list with the tree-view control. 
    TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);

    return TRUE;

}

SHFILEINFO GetShellInfo(LPCWSTR path) {
    SHFILEINFO sfi;
    SecureZeroMemory(&sfi, sizeof(sfi));
    SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SHELLICONSIZE | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
    return sfi;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-03 20:38:26

您可以使用IShellItemImageFactory接口,如下所示:

代码语言:javascript
运行
复制
...
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // need this somewhere when your thread begins, not for every call
...
IShellItemImageFactory* factory;
if (SUCCEEDED(SHCreateItemFromParsingName(path, nullptr, IID_PPV_ARGS(&factory))))
{
    // the GetImage method defines a required size and multiple flags
    // with which you can specify you want the icon only or the thumbnail, etc.
    HBITMAP bmp;
    if (SUCCEEDED(factory->GetImage(SIZE{ 256, 256 }, SIIGBF_ICONONLY, &bmp)))
    {
        ... // do something with the HBITMAP
        DeleteObject(bmp);
    }
    factory->Release();
}

...
CoUninitialize(); // call this each time you successfully called CoInitializeEx
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71729018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档