Skip to content

Commit 56b0a7c

Browse files
committed
Implemented Win32Utils::LoadIconFromFileAsBitmap32Bpp()
1 parent 5ffa505 commit 56b0a7c

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/shared/Win32Utils.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,49 @@ namespace Win32Utils
14271427
return hReturn;
14281428
}
14291429

1430+
HBITMAP LoadIconFromFileAsBitmap32Bpp(const char* filename, int index)
1431+
{
1432+
HBITMAP hBitmap = INVALID_BITMAP_HANDLE;
1433+
HICON hIconSmall = NULL;
1434+
1435+
std::wstring icon_filename_wide = ra::unicode::Utf8ToUnicode(filename);
1436+
1437+
//check how many icons the file contains
1438+
UINT num_icon_in_file = ExtractIconExW(icon_filename_wide.c_str(), -1, NULL, NULL, 1);
1439+
if (num_icon_in_file == 0)
1440+
{
1441+
// ERROR: "File '" << filename << "' does not contains an icon.";
1442+
}
1443+
else
1444+
{
1445+
//the file contains 1 or more icons, try to load a small one
1446+
UINT num_icon_loaded = 0;
1447+
if (num_icon_in_file >= 1)
1448+
num_icon_loaded = ExtractIconExW(icon_filename_wide.c_str(), index, NULL, &hIconSmall, 1);
1449+
if (num_icon_in_file >= 1 && num_icon_loaded == 0)
1450+
{
1451+
// ERROR: "Failed to load icon index " << icon_index << " from file '" << icon_filename << "'.";
1452+
}
1453+
else
1454+
{
1455+
SIZE menu_icon_size = Win32Utils::GetIconSize(hIconSmall);
1456+
// ERROR: "Loaded icon " << icon_index << " from file '" << icon_filename << "' is " << menu_icon_size.cx << "x" << menu_icon_size.cy << ".";
1457+
1458+
//Convert the icon to a 32bpp bitmap with alpha channel (invisible background)
1459+
hBitmap = Win32Utils::CopyAsBitmap(hIconSmall);
1460+
if (hBitmap == INVALID_BITMAP_HANDLE)
1461+
{
1462+
// ERROR: "Icon " << icon_index << " from file '" << icon_filename << "' has failed to convert to bitmap.";
1463+
}
1464+
1465+
if (hIconSmall != NULL)
1466+
DestroyIcon(hIconSmall);
1467+
}
1468+
}
1469+
1470+
return hBitmap;
1471+
}
1472+
14301473
BOOL IsFullyTransparent(HBITMAP hBitmap)
14311474
{
14321475
SIZE bitmap_size = GetBitmapSize(hBitmap);

src/shared/Win32Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
namespace Win32Utils
3232
{
33+
static const HBITMAP INVALID_BITMAP_HANDLE = NULL;
34+
3335
void GetWindowsVersion(int& major, int& minor);
3436
std::string GetWindowsProductName();
3537
bool EnableMonitorDpiAwareness();
@@ -59,6 +61,7 @@ namespace Win32Utils
5961
bool SaveAs32BppBitmapV3File(const char* path, HBITMAP hBitmap);
6062
bool SaveBitmapFile(const char* path, HBITMAP hBitmap);
6163
HBITMAP LoadBitmapFromFile(const char* path);
64+
HBITMAP LoadIconFromFileAsBitmap32Bpp(const char* filename, int index);
6265
BOOL IsFullyTransparent(HBITMAP hBitmap);
6366
BOOL IsFullyTransparent(const std::string& buffer);
6467
std::string GetMenuItemDetails(HMENU hMenu, UINT pos);

0 commit comments

Comments
 (0)