I have a shell extension with an icon handler that sets a file type's icons to a green or red icon, based on file contents. The icons seem to work properly except they are really blurry when using large icons, as if they were zoomed in from a really small size. The icon .ico
files contain all image sizes from 256x256 to 16x16.
I am using a very basic icon handler, but maybe there could still be some problem with caching or something. How could I make sure the icons are loaded properly?
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;
}
HRESULT icon_handler::Extract(PCWSTR psz_file, UINT n_icon_index, HICON* phicon_large, HICON* phicon_small, UINT n_icon_size)
{
int icon = ICON_GREEN;
if (m_icon_color_ == 1) {
icon = ICON_RED;
}
if (phicon_large != nullptr)
{
const int large_size = LOWORD(n_icon_size);
*phicon_large = HICON(LoadImageW(global_h_instance, MAKEINTRESOURCE(icon), IMAGE_ICON, large_size, large_size,
LR_DEFAULTCOLOR));
}
if (phicon_small != nullptr)
{
const int small_size = HIWORD(n_icon_size);
*phicon_small = HICON(LoadImageW(global_h_instance, MAKEINTRESOURCE(icon), IMAGE_ICON, small_size, small_size,
LR_DEFAULTCOLOR));
}
return S_OK;
}
When logging with DebugView the icon handler appears to request appropriate sizes:
[30100] phicon_large size:
[30100] 256
[30100] phicon_small size:
[30100] 16
Edit:
As per @Anders, if I check the size of the image loaded with LoadImage
it also appears to be correct:
*phicon_large = HICON(LoadImageW(global_h_instance, MAKEINTRESOURCE(icon), IMAGE_ICON, large_size, large_size,
LR_DEFAULTCOLOR));
ICONINFOEXW info = {sizeof(ICONINFOEXW)};
GetIconInfoEx(*phicon_large, &info)
BITMAP bmp;
GetObjectW(info.hbmMask, sizeof(BITMAP), &bmp);
OutputDebugStringW(L"Icon size:");
OutputDebugStringW(std::to_wstring(bmp.bmWidth).c_str());
[12376] phicon_large size:
[12376] 256
[12376] Icon size:
[12376] 256
[12376] phicon_small size:
[12376] 16
[12376] Icon size:
[12376] 16