Skip to content

Commit

Permalink
Fix toolbar image auto scaling, avoid enlarge external large enough t…
Browse files Browse the repository at this point in the history
…oolbar images.
  • Loading branch information
zufuliu committed Apr 10, 2020
1 parent d28215f commit 1b483f5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ static INT_PTR CALLBACK SelectEncodingDlgProc(HWND hwnd, UINT umsg, WPARAM wPara
// we need to determine icon size first, then resize the encoding mask bitmap accordingly.

HBITMAP hbmp = (HBITMAP)LoadImage(g_hInstance, MAKEINTRESOURCE(IDB_ENCODING), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
hbmp = ResizeImageForCurrentDPI(hbmp);
hbmp = ResizeImageForCurrentDPI(hbmp, 16); // 32x16
BITMAP bmp;
GetObject(hbmp, sizeof(BITMAP), &bmp);
HIMAGELIST himl = ImageList_Create(bmp.bmHeight, bmp.bmHeight, ILC_COLOR32 | ILC_MASK, 0, 0);
Expand Down
19 changes: 17 additions & 2 deletions src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ HBITMAP LoadBitmapFile(LPCWSTR path) {
return hbmp;
}

HBITMAP ResizeImageForDPI(HBITMAP hbmp, UINT dpi) {
HBITMAP EnlargeImageForDPI(HBITMAP hbmp, UINT dpi) {
BITMAP bmp;
if (dpi > USER_DEFAULT_SCREEN_DPI && GetObject(hbmp, sizeof(BITMAP), &bmp)) {
const int width = MulDiv(dpi, bmp.bmWidth, USER_DEFAULT_SCREEN_DPI);
Expand All @@ -413,6 +413,21 @@ HBITMAP ResizeImageForDPI(HBITMAP hbmp, UINT dpi) {
return hbmp;
}

HBITMAP ResizeImageForDPI(HBITMAP hbmp, UINT dpi, int height) {
BITMAP bmp;
if (dpi > USER_DEFAULT_SCREEN_DPI && GetObject(hbmp, sizeof(BITMAP), &bmp)) {
height = MulDiv(dpi, height, USER_DEFAULT_SCREEN_DPI);
// keep aspect ratio
const int width = MulDiv(height, bmp.bmWidth, bmp.bmHeight);
HBITMAP hCopy = (HBITMAP)CopyImage(hbmp, IMAGE_BITMAP, width, height, LR_COPYRETURNORG | LR_COPYDELETEORG);
if (hCopy != NULL) {
hbmp = hCopy;
}
}

return hbmp;
}

//=============================================================================
//
// PrivateSetCurrentProcessExplicitAppUserModelID()
Expand Down Expand Up @@ -1092,7 +1107,7 @@ void MultilineEditSetup(HWND hwndDlg, int nCtlId) {
void MakeBitmapButton(HWND hwnd, int nCtlId, HINSTANCE hInstance, WORD wBmpId) {
HWND hwndCtl = GetDlgItem(hwnd, nCtlId);
HBITMAP hBmp = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(wBmpId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
hBmp = ResizeImageForCurrentDPI(hBmp);
hBmp = ResizeButtonImageForCurrentDPI(hBmp);
BITMAP bmp;
GetObject(hBmp, sizeof(BITMAP), &bmp);
BUTTON_IMAGELIST bi;
Expand Down
20 changes: 17 additions & 3 deletions src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,23 @@ BOOL IsElevated(void);
#define SetExplorerTheme(hwnd) SetWindowTheme((hwnd), L"Explorer", NULL)

HBITMAP LoadBitmapFile(LPCWSTR path);
HBITMAP ResizeImageForDPI(HBITMAP hbmp, UINT dpi);
NP2_inline HBITMAP ResizeImageForCurrentDPI(HBITMAP hbmp) {
return ResizeImageForDPI(hbmp, g_uCurrentDPI);
HBITMAP EnlargeImageForDPI(HBITMAP hbmp, UINT dpi);
HBITMAP ResizeImageForDPI(HBITMAP hbmp, UINT dpi, int height);

NP2_inline HBITMAP EnlargeImageForCurrentDPI(HBITMAP hbmp) {
return EnlargeImageForDPI(hbmp, g_uCurrentDPI);
}

NP2_inline HBITMAP ResizeImageForCurrentDPI(HBITMAP hbmp, int height) {
return ResizeImageForDPI(hbmp, g_uCurrentDPI, height);
}

NP2_inline HBITMAP ResizeButtonImageForCurrentDPI(HBITMAP hbmp) {
return ResizeImageForDPI(hbmp, g_uCurrentDPI, 16);
}

NP2_inline HBITMAP ResizeToolbarImageForCurrentDPI(HBITMAP hbmp) {
return ResizeImageForDPI(hbmp, g_uCurrentDPI, 16);
}

BOOL BitmapMergeAlpha(HBITMAP hbmp, COLORREF crDest);
Expand Down
6 changes: 3 additions & 3 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance) {
hbmp = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDR_MAINWND), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
}
if (bAutoScaleToolbar) {
hbmp = ResizeImageForCurrentDPI(hbmp);
hbmp = ResizeToolbarImageForCurrentDPI(hbmp);
}
HBITMAP hbmpCopy = NULL;
if (!bExternalBitmap) {
Expand All @@ -1827,7 +1827,7 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance) {
hbmp = LoadBitmapFile(tchToolbarBitmapHot);
if (hbmp != NULL) {
if (bAutoScaleToolbar) {
hbmp = ResizeImageForCurrentDPI(hbmp);
hbmp = ResizeToolbarImageForCurrentDPI(hbmp);
}
GetObject(hbmp, sizeof(BITMAP), &bmp);
himl = ImageList_Create(bmp.bmHeight, bmp.bmHeight, ILC_COLOR32 | ILC_MASK, 0, 0);
Expand All @@ -1842,7 +1842,7 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance) {
hbmp = LoadBitmapFile(tchToolbarBitmapDisabled);
if (hbmp != NULL) {
if (bAutoScaleToolbar) {
hbmp = ResizeImageForCurrentDPI(hbmp);
hbmp = ResizeToolbarImageForCurrentDPI(hbmp);
}
GetObject(hbmp, sizeof(BITMAP), &bmp);
himl = ImageList_Create(bmp.bmHeight, bmp.bmHeight, ILC_COLOR32 | ILC_MASK, 0, 0);
Expand Down

0 comments on commit 1b483f5

Please sign in to comment.