Skip to content

Scale custom button image according to current DPI #2248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Src/Lib/ResourceHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,14 @@ HICON CreateDisabledIcon( HICON hIcon, int iconSize )
}

// Loads an image file into a bitmap and optionally resizes it
HBITMAP LoadImageFile( const wchar_t *path, const SIZE *pSize, bool bUseAlpha, bool bPremultiply, std::vector<unsigned int> *pButtonAnim )
HBITMAP LoadImageFile( const wchar_t *path, const SIZE *pSize, bool bUseAlpha, bool bPremultiply, std::vector<unsigned int> *pButtonAnim, UINT dpi )
{
HBITMAP srcBmp=NULL;
if (_wcsicmp(PathFindExtension(path),L".bmp")==0)
{
srcBmp=(HBITMAP)LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
}
if (srcBmp && !pSize)
if (srcBmp && !pSize && !dpi)
return srcBmp;
CComPtr<IWICImagingFactory> pFactory;
if (FAILED(pFactory.CoCreateInstance(CLSID_WICImagingFactory)))
Expand Down Expand Up @@ -482,6 +482,12 @@ HBITMAP LoadImageFile( const wchar_t *path, const SIZE *pSize, bool bUseAlpha, b
else
frameHeightD=frameWidthD*frameHeightS/frameWidthS;
}

if (dpi)
{
frameWidthD=ScaleForDpi(dpi,frameWidthD);
frameHeightD=ScaleForDpi(dpi,frameHeightD);
}
}

BITMAPINFO bi={0};
Expand Down Expand Up @@ -946,7 +952,12 @@ UINT GetDpi(HWND hwnd)
return dpi;
}

int ScaleForDpi(UINT dpi, int value)
{
return MulDiv(value, dpi, USER_DEFAULT_SCREEN_DPI);
}

int ScaleForDpi(HWND hwnd, int value)
{
return MulDiv(value, GetDpi(hwnd), USER_DEFAULT_SCREEN_DPI);
return ScaleForDpi(GetDpi(hwnd), value);
}
4 changes: 3 additions & 1 deletion Src/Lib/ResourceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ HICON ShExtractIcon( const char *path, int index, int iconSize );
HBITMAP BitmapFromIcon( HICON hIcon, int iconSize, unsigned int **pBits, bool bDestroyIcon );

// Loads an image file into a bitmap and optionally resizes it
HBITMAP LoadImageFile( const wchar_t *path, const SIZE *pSize, bool bUseAlpha, bool bPremultiply, std::vector<unsigned int> *pButtonAnim );
HBITMAP LoadImageFile( const wchar_t *path, const SIZE *pSize, bool bUseAlpha, bool bPremultiply, std::vector<unsigned int> *pButtonAnim, UINT dpi=0 );

// Loads a bitmap from a IMAGE resource
HBITMAP LoadImageResource( HMODULE hModule, const wchar_t *name, bool bTopDown, bool bPremultiply );
Expand Down Expand Up @@ -88,6 +88,8 @@ HFONT CreateFontSetting( const wchar_t *fontStr, int dpi );
// Return DPI of given window (or system DPI on older systems)
UINT GetDpi(HWND hwnd = nullptr);

// Scale given value according to given DPI
int ScaleForDpi(UINT dpi, int value);
// Scale given value according to DPI of window
int ScaleForDpi(HWND hwnd, int value);

Expand Down
4 changes: 2 additions & 2 deletions Src/StartMenu/StartMenuDLL/StartButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,16 @@ void CStartButton::LoadBitmap( void )
}
else
{
int dpi=GetDpi(GetParent());
bool bResource=false;
std::vector<unsigned int> buttonAnim;
if (*path)
{
m_Bitmap=LoadImageFile(path,&size,true,true,&buttonAnim);
m_Bitmap=LoadImageFile(path,&size,true,true,&buttonAnim,dpi);
}
if (!m_Bitmap)
{
int id;
int dpi=GetDpi(GetParent());
if (dpi<120)
id=IDB_BUTTON96;
else if (dpi<144)
Expand Down