Set scale factor correctly for icons loaded from resources

::LoadIcon() selects the icon of scaled size in the DPI-aware programs,
which is nice, but only works right if we actually set the scale factor
for the icon correctly.
This commit is contained in:
Vadim Zeitlin
2021-12-16 01:17:35 +00:00
parent a1e4dca067
commit d78696243b
3 changed files with 23 additions and 3 deletions

View File

@@ -71,7 +71,7 @@ public:
#endif // WXWIN_COMPATIBILITY_3_0
WXHICON GetHICON() const { return (WXHICON)GetHandle(); }
bool InitFromHICON(WXHICON icon, int width, int height);
bool InitFromHICON(WXHICON icon, int width, int height, double scale = 1.0);
// create from bitmap (which should have a mask unless it's monochrome):
// there shouldn't be any implicit bitmap -> icon conversion (i.e. no

View File

@@ -586,7 +586,26 @@ bool wxICOResourceHandler::LoadIcon(wxIcon *icon,
}
}
return icon->CreateFromHICON((WXHICON)hicon);
if ( !hicon )
return false;
wxSize size;
double scale = 1.0;
if ( hasSize )
{
size.x = desiredWidth;
size.y = desiredHeight;
}
else // We loaded an icon of default size.
{
// LoadIcon() returns icons of scaled size, so we must use the correct
// scaling factor of them.
size = wxGetHiconSize(hicon);
if ( const wxWindow* win = wxApp::GetMainTopWindow() )
scale = win->GetDPIScaleFactor();
}
return icon->InitFromHICON((WXHICON)hicon, size.x, size.y, scale);
}
#if wxUSE_PNG_RESOURCE_HANDLER

View File

@@ -154,7 +154,7 @@ bool wxIcon::CreateFromHICON(WXHICON icon)
return InitFromHICON(icon, size.GetWidth(), size.GetHeight());
}
bool wxIcon::InitFromHICON(WXHICON icon, int width, int height)
bool wxIcon::InitFromHICON(WXHICON icon, int width, int height, double scale)
{
#if wxDEBUG_LEVEL >= 2
if ( icon != NULL )
@@ -170,6 +170,7 @@ bool wxIcon::InitFromHICON(WXHICON icon, int width, int height)
GetGDIImageData()->m_handle = (WXHANDLE)icon;
GetGDIImageData()->m_width = width;
GetGDIImageData()->m_height = height;
GetGDIImageData()->m_scaleFactor = scale;
return IsOk();
}