在WIC中调用CreateBitmapFromHIcon时指定HICON索引

Specify HICON index when calling CreateBitmapFromHIcon in WIC

本文关键字:HICON 索引 CreateBitmapFromHIcon WIC 调用      更新时间:2023-10-16

根据文档,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;
}
}

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