首页 > 解决方案 > 从图标处理程序外壳扩展中的资源加载位图图标

问题描述

我需要.bmp从资源中加载一个图标IExtractIcon::Extract,但我无法弄清楚它为什么不起作用。我不断得到一个黑色或白色的矩形图标应该是。

在此处输入图像描述

我在项目资源.rc文件中声明了两个图标:ICON_16_BITMAPICON_BITMAP. 绝对应该加载图标,因为它们在LoadImageW.

// IExtractIcon

HRESULT icon_handler::GetIconLocation(UINT u_flags, PWSTR psz_icon_file, UINT cch_max, int* pi_index, UINT* pw_flags)
{
    *pw_flags = GIL_NOTFILENAME | GIL_DONTCACHE;
    return S_OK;
}

extern HINSTANCE global_h_instance;

HRESULT icon_handler::Extract(PCWSTR psz_file, UINT n_icon_index, HICON* phicon_large, HICON* phicon_small, UINT n_icon_size)
{
    const int small_size = HIWORD(n_icon_size);
    const int large_size = LOWORD(n_icon_size);

    if (phicon_large != nullptr)
    {
        OutputDebugStringW((L"Extract large icon: " + std::to_wstring(large_size)).c_str());

        *phicon_large = HICON(LoadImageW(global_h_instance, MAKEINTRESOURCE(ICON_BITMAP), IMAGE_BITMAP, large_size, large_size,
            LR_SHARED));
    }
    if (phicon_small != nullptr)
    {
        OutputDebugStringW((L"Extract small icon: " + std::to_wstring(small_size)).c_str());

        *phicon_small = HICON(LoadImageW(global_h_instance, MAKEINTRESOURCE(ICON_16_BITMAP), IMAGE_BITMAP, small_size, small_size,
            LR_SHARED));
    }

    return S_OK;
}

我尝试过很多教程,但这似乎很简单,我可以去,但到目前为止还没有成功。有什么突出的地方可能是图标不起作用的原因吗?

标签: windowswinapishell-extensions

解决方案


BMP 与图标格式不一样。

它不能被 HICON 强制转换。

最简单的方法是通过转换图片工具将BMP文件转换成图标文件,然后加载到资源中。


推荐阅读