首页 > 解决方案 > 在 WIC 中调用 CreateBitmapFromHIcon 时指定 HICON 索引

问题描述

根据文档,ExtractIconEx返回一个指向 HICON 数组的指针。在传递给CreateBitmapFromHICON时,如何指定要使用此数组中的哪个项目。

#include <iostream>
        #include <Windows.h>
        #include <Wincodec.h>
        #pragma comment(lib,"Windowscodecs.lib")

HICON hiconLarge = NULL;
HICON hiconSmall = NULL;

int main()
{
    CoInitialize(NULL);
    double x, y;

    IWICImagingFactory* piFactory = NULL;
    IWICBitmap* piBitmap = NULL;

    //Create the COM imaging factory.
    HRESULT hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IWICImagingFactory,
        (LPVOID*)&piFactory);

    UINT nIcons;
    nIcons = ExtractIconEx(L"c:\\windows\\system32\\shell32.dll",
        -1,
        NULL,
        NULL,
        0);

        ExtractIconEx(L"c:\\windows\\system32\\shell32.dll",
        0,
        &hiconLarge,
        &hiconSmall,
        nIcons);

    std::cout << nIcons << " icons found." << std::endl;

    HRESULT hResult = piFactory->CreateBitmapFromHICON(hiconLarge, &piBitmap);
    if (hResult == S_OK)
    {
        piBitmap->GetResolution(&x, &y);
        std::cout << "Resolution x=" << x << " y=" << y << std::endl;
    }
}

标签: c++winapi

解决方案


nIcons HICON在第二次调用 之前,您需要为值分配足够的空间,ExtractIconEx因此应该是指向 HICON 对象 ( ) 的指针。目前你只有一个足够的空间。然后,您可以像通常使用数组一样访问这些值。hiconLargehiconSmallHICON *


推荐阅读