From a34de8dbdd0a1a6a91ae67e99ef6115e79a358ae Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Mon, 9 Mar 2020 18:22:11 +0100 Subject: [PATCH] + try fix: AboutBox's RichEdit ctrl for DPI scaling --- scintilla/win32/ScintillaWin.cxx | 4 +- src/Dialogs.c | 117 +++++++++++++++++-------------- src/Dialogs.h | 10 +-- src/Notepad3.c | 36 ++++------ src/Styles.c | 4 +- 5 files changed, 86 insertions(+), 85 deletions(-) diff --git a/scintilla/win32/ScintillaWin.cxx b/scintilla/win32/ScintillaWin.cxx index 43147aed1..4446682f4 100644 --- a/scintilla/win32/ScintillaWin.cxx +++ b/scintilla/win32/ScintillaWin.cxx @@ -1022,8 +1022,8 @@ Sci::Position ScintillaWin::EncodedFromUTF8(const char *utf8, char *encoded) con } } -bool ScintillaWin::PaintDC(HDC hdc) { - +bool ScintillaWin::PaintDC(HDC hdc) +{ if (technology == SC_TECHNOLOGY_DEFAULT) { AutoSurface surfaceWindow(hdc, this); if (surfaceWindow) { diff --git a/src/Dialogs.c b/src/Dialogs.c index 4c1bfed4e..1b02f2cf6 100644 --- a/src/Dialogs.c +++ b/src/Dialogs.c @@ -685,56 +685,65 @@ INT_PTR CALLBACK AboutDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam GetLngStringA(IDS_MUI_ABOUT_RTF_6, pAboutRes, COUNTOF(pAboutRes)); StringCchCatA(pAboutResource, COUNTOF(pAboutResource), pAboutRes); + // paint richedit box pAboutInfo = pAboutResource; EDITSTREAM editStreamIn = { (DWORD_PTR)&pAboutInfo, 0, _LoadRtfCallback }; SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_STREAMIN, SF_RTF, (LPARAM)&editStreamIn); CenterDlgInParent(hwnd, NULL); } - // fall-through + break; + case WM_DPICHANGED: { - // get current richedit box format - CHARFORMAT2 currentFormat; ZeroMemory(¤tFormat, sizeof(CHARFORMAT2)); currentFormat.cbSize = sizeof(CHARFORMAT2); - currentFormat.dwMask = CFM_ALL2; // CFM_SIZE | CFM_FACE | CFM_CHARSET | CFM_LCID; CFM_ALL; CFM_ALL2; - SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)¤tFormat); - UpdateWindowLayoutForDPI(hwnd, 0, 0, 0, 0); - // --- keep original font size --- - CHARFORMAT dpiCharFmt; ZeroMemory(&dpiCharFmt, sizeof(CHARFORMAT)); dpiCharFmt.cbSize = sizeof(CHARFORMAT); - dpiCharFmt.dwMask = CFM_SIZE; //~ | CFM_FACE; - dpiCharFmt.yHeight = currentFormat.yHeight; // keep size - //~StringCchCopy(dpiCharFmt.szFaceName, COUNTOF(dpiCharFmt.szFaceName), L"Consolas"); - SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&dpiCharFmt); + DPI_T const dpi = GetCurrentDPI(hwnd); + SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_SETZOOM, (WPARAM)dpi.y, (LPARAM)USER_DEFAULT_SCREEN_DPI); - // --- larger bold version string - if (hVersionFont) { DeleteObject(hVersionFont); } - if ((hVersionFont = (HFONT)SendDlgItemMessage(hwnd, IDC_VERSION, WM_GETFONT, 0, 0)) == NULL) { - hVersionFont = GetStockObject(DEFAULT_GUI_FONT); - } - LOGFONT lf; GetObject(hVersionFont, sizeof(LOGFONT), &lf); - lf.lfWeight = FW_BOLD; - lf.lfWidth = ScaleIntFontSizeWidth(hwnd, 8); - lf.lfHeight = ScaleIntFontSizeHeight(hwnd, 22); - // lf.lfQuality = ANTIALIASED_QUALITY; - hVersionFont = CreateFontIndirect(&lf); - SendDlgItemMessage(hwnd, IDC_VERSION, WM_SETFONT, (WPARAM)hVersionFont, true); + //~~// get current richedit box format + //~~CHARFORMAT2 currentFormat; ZeroMemory(¤tFormat, sizeof(CHARFORMAT2)); currentFormat.cbSize = sizeof(CHARFORMAT2); + //~~currentFormat.dwMask = CFM_ALL2; // CFM_SIZE | CFM_FACE | CFM_CHARSET | CFM_LCID; CFM_ALL; CFM_ALL2; + //~~SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)¤tFormat); + //~~ + //~~//CHARFORMAT dpiCharFmt; ZeroMemory(&dpiCharFmt, sizeof(CHARFORMAT)); dpiCharFmt.cbSize = sizeof(CHARFORMAT); + //~~//dpiCharFmt.dwMask = CFM_ALL; CFM_SIZE; //~ | CFM_FACE; + //~~CHARFORMAT2 dpiCharFmt = currentFormat; + //~~dpiCharFmt.yHeight = (currentFormat.yHeight == 180) ? ScaleIntToDPI_Y(hwnd, 180) : currentFormat.yHeight; // keep size + //~~//~StringCchCopy(dpiCharFmt.szFaceName, COUNTOF(dpiCharFmt.szFaceName), L"Segoe UI"); + //~~SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&dpiCharFmt); } - return true; + break; case WM_PAINT: { if (Globals.hDlgIcon128) { int const iconSize = 128; - int const dpiScaledWidth = ScaleIntToHwndDPIX(hwnd, iconSize); - int const dpiScaledHeight = ScaleIntToHwndDPIY(hwnd, iconSize); + int const dpiScaledWidth = ScaleIntToDPI_X(hwnd, iconSize); + int const dpiScaledHeight = ScaleIntToDPI_Y(hwnd, iconSize); HDC const hdc = GetWindowDC(hwnd); - DrawIconEx(hdc, 22, 44, Globals.hDlgIcon128, dpiScaledWidth, dpiScaledHeight, 0, NULL, DI_NORMAL); + DrawIconEx(hdc, ScaleIntToDPI_X(hwnd, 22), ScaleIntToDPI_Y(hwnd, 44), + Globals.hDlgIcon128, dpiScaledWidth, dpiScaledHeight, 0, NULL, DI_NORMAL); ReleaseDC(hwnd, hdc); } + + // --- larger bold condensed version string + if (hVersionFont) { DeleteObject(hVersionFont); } + if ((hVersionFont = (HFONT)SendDlgItemMessage(hwnd, IDC_VERSION, WM_GETFONT, 0, 0)) == NULL) { + hVersionFont = GetStockObject(DEFAULT_GUI_FONT); + } + LOGFONT lf; GetObject(hVersionFont, sizeof(LOGFONT), &lf); + lf.lfWeight = FW_BOLD; + lf.lfWidth = ScaleIntToDPI_X(hwnd, 8); + lf.lfHeight = ScaleIntToDPI_Y(hwnd, 22); + //StringCchCopy(lf.lfFaceName, LF_FACESIZE, L"Segoe UI"); + hVersionFont = CreateFontIndirect(&lf); + SendDlgItemMessage(hwnd, IDC_VERSION, WM_SETFONT, (WPARAM)hVersionFont, true); + + // rich edit control + SendDlgItemMessage(hwnd, IDC_RICHEDITABOUT, EM_SETZOOM, 0, 0); } return false; @@ -836,7 +845,7 @@ INT_PTR CALLBACK AboutDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam StringCchCat(wchVerInfo, COUNTOF(wchVerInfo), wchBuf); DPI_T dpi = GetCurrentDPI(hwnd); - StringCchPrintf(wchBuf, COUNTOF(wchBuf), L"\n- Display-DPI = %i x %i (Scale: %i%%).", dpi.x, dpi.y, ScaleIntToHwndDPIX(hwnd, 100)); + StringCchPrintf(wchBuf, COUNTOF(wchBuf), L"\n- Display-DPI = %i x %i (Scale: %i%%).", dpi.x, dpi.y, ScaleIntToDPI_X(hwnd, 100)); StringCchCat(wchVerInfo, COUNTOF(wchVerInfo), wchBuf); StringCchPrintf(wchBuf, COUNTOF(wchBuf), L"\n- Rendering-Technology = '%s'", Settings.RenderingTechnology ? L"DIRECT-WRITE" : L"GDI"); @@ -4184,9 +4193,6 @@ HWND CreateThemedDialogParam(HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndP - - - //============================================================================= // // ConvertIconToBitmap() @@ -4236,6 +4242,24 @@ void SetUACIcon(const HMENU hMenu, const UINT nItem) } + + +//============================================================================= +// +// GetCurrentPPI() +// (font size) points per inch +// +DPI_T GetCurrentPPI(HWND hwnd) { + HDC const hDC = GetDC(hwnd); + DPI_T ppi; + ppi.x = max_u(GetDeviceCaps(hDC, LOGPIXELSX), USER_DEFAULT_SCREEN_DPI); + ppi.y = max_u(GetDeviceCaps(hDC, LOGPIXELSY), USER_DEFAULT_SCREEN_DPI); + ReleaseDC(hwnd, hDC); + return ppi; +} + + + //============================================================================= // // GetCurrentDPI() @@ -4271,10 +4295,7 @@ DPI_T GetCurrentDPI(HWND hwnd) { } if (curDPI.x == 0) { - HDC hDC = GetDC(hwnd); - curDPI.x = GetDeviceCaps(hDC, LOGPIXELSX); - curDPI.y = GetDeviceCaps(hDC, LOGPIXELSY); - ReleaseDC(hwnd, hDC); + curDPI = GetCurrentPPI(hwnd); // fallback to device caps } curDPI.x = max_u(curDPI.x, USER_DEFAULT_SCREEN_DPI); @@ -4283,20 +4304,6 @@ DPI_T GetCurrentDPI(HWND hwnd) { } -//============================================================================= -// -// GetCurrentPPI() -// (font size) points per inch -// -DPI_T GetCurrentPPI(HWND hwnd) { - HDC const hDC = GetDC(hwnd); - DPI_T ppi; - ppi.x = max_u(GetDeviceCaps(hDC, LOGPIXELSX), USER_DEFAULT_SCREEN_DPI); - ppi.y = max_u(GetDeviceCaps(hDC, LOGPIXELSY), USER_DEFAULT_SCREEN_DPI); - ReleaseDC(hwnd, hDC); - return ppi; -} - /* if (!bSucceed) { NONCLIENTMETRICS ncm; @@ -4318,12 +4325,12 @@ if (!bSucceed) { int GetSystemMetricsDPIScaledX(HWND hwnd, const int nValue) { - return ScaleIntToHwndDPIX(hwnd, GetSystemMetrics(nValue)); + return ScaleIntToDPI_X(hwnd, GetSystemMetrics(nValue)); } int GetSystemMetricsDPIScaledY(HWND hwnd, const int nValue) { - return ScaleIntToHwndDPIY(hwnd, GetSystemMetrics(nValue)); + return ScaleIntToDPI_Y(hwnd, GetSystemMetrics(nValue)); } //============================================================================= @@ -4374,8 +4381,8 @@ HBITMAP ResizeImageForCurrentDPI(HWND hwnd, HBITMAP hbmp) BITMAP bmp; if (GetObject(hbmp, sizeof(BITMAP), &bmp)) { - WORD const width = (WORD)ScaleIntToHwndDPIX(hwnd, bmp.bmWidth); - WORD const height = (WORD)ScaleIntToHwndDPIY(hwnd, bmp.bmHeight); + int const width = ScaleIntToDPI_X(hwnd, bmp.bmWidth); + int const height = ScaleIntToDPI_Y(hwnd, bmp.bmHeight); if (((LONG)width != bmp.bmWidth) || ((LONG)height != bmp.bmHeight)) { HBITMAP hCopy = CopyImage(hbmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION | LR_COPYRETURNORG | LR_COPYDELETEORG); @@ -4404,4 +4411,6 @@ LRESULT SendWMSize(HWND hwnd, RECT* rc) return SendMessage(hwnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(wndrc.right, wndrc.bottom)); } + + // End of Dialogs.c diff --git a/src/Dialogs.h b/src/Dialogs.h index 102f4f7dd..45495ad10 100644 --- a/src/Dialogs.h +++ b/src/Dialogs.h @@ -112,14 +112,14 @@ int Toolbar_SetButtons(HANDLE, int, LPCWSTR, void*, int); int GetSystemMetricsDPIScaledX(HWND hwnd, const int nValue); int GetSystemMetricsDPIScaledY(HWND hwnd, const int nValue); -DPI_T GetCurrentDPI(HWND hwnd); DPI_T GetCurrentPPI(HWND hwnd); +DPI_T GetCurrentDPI(HWND hwnd); -inline int ScaleIntToHwndDPIX(HWND hwnd, int val) { DPI_T const dpi = GetCurrentDPI(hwnd); return MulDiv((val), dpi.x, USER_DEFAULT_SCREEN_DPI); } -inline int ScaleIntToHwndDPIY(HWND hwnd, int val) { DPI_T const dpi = GetCurrentDPI(hwnd); return MulDiv((val), dpi.y, USER_DEFAULT_SCREEN_DPI); } +inline int ScaleIntToDPI_X(HWND hwnd, int val) { DPI_T const dpi = GetCurrentDPI(hwnd); return MulDiv((val), dpi.x, USER_DEFAULT_SCREEN_DPI); } +inline int ScaleIntToDPI_Y(HWND hwnd, int val) { DPI_T const dpi = GetCurrentDPI(hwnd); return MulDiv((val), dpi.y, USER_DEFAULT_SCREEN_DPI); } -inline int ScaleFloatToHwndDPIX(HWND hwnd, float fVal) { DPI_T const dpi = GetCurrentDPI(hwnd); return (int)lroundf((fVal * dpi.x) / (float)USER_DEFAULT_SCREEN_DPI); } -inline int ScaleFloatToHwndDPIY(HWND hwnd, float fVal) { DPI_T const dpi = GetCurrentDPI(hwnd); return (int)lroundf((fVal * dpi.y) / (float)USER_DEFAULT_SCREEN_DPI); } +inline int ScaleFloatToDPI_X(HWND hwnd, float fVal) { DPI_T const dpi = GetCurrentDPI(hwnd); return (int)lroundf((fVal * dpi.x) / (float)USER_DEFAULT_SCREEN_DPI); } +inline int ScaleFloatToDPI_Y(HWND hwnd, float fVal) { DPI_T const dpi = GetCurrentDPI(hwnd); return (int)lroundf((fVal * dpi.y) / (float)USER_DEFAULT_SCREEN_DPI); } inline int ScaleIntFontSizeWidth(HWND hwnd, int val) { DPI_T const dpi = GetCurrentDPI(hwnd); diff --git a/src/Notepad3.c b/src/Notepad3.c index 1d29b4b8a..e4551d70b 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -155,8 +155,6 @@ static int s_iExprError = -1; static WIN32_FIND_DATA s_fdCurFile; -static HMODULE s_hRichEdit = INVALID_HANDLE_VALUE; - static int const INISECTIONBUFCNT = 32; // .ini file load buffer in KB static TBBUTTON s_tbbMainWnd[] = { @@ -771,11 +769,6 @@ static void _CleanUpResources(const HWND hwnd, bool bIsInitialized) FreeLanguageResources(); - if (s_hRichEdit) { - FreeLibrary(s_hRichEdit); - s_hRichEdit = INVALID_HANDLE_VALUE; - } - if (bIsInitialized) { UnregisterClass(s_wchWndClass, Globals.hInstance); } @@ -878,11 +871,6 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, // ---------------------------------------------------- - if (s_hRichEdit == INVALID_HANDLE_VALUE) { - //s_hRichEdit = LoadLibrary(L"RICHED20.DLL"); // Use RICHEDIT_CONTROL_VER for control in common_res.h - s_hRichEdit = LoadLibrary(L"MSFTEDIT.DLL"); // Use "RichEdit50W" for control in common_res.h - } - // ICON_BIG int const cxb = GetSystemMetrics(SM_CXICON); int const cyb = GetSystemMetrics(SM_CYICON); @@ -2145,7 +2133,7 @@ static HIMAGELIST CreateScaledImageListFromBitmap(HWND hWnd, HBITMAP hBmp) int const cx = (bmp.bmWidth - mod) / NUMTOOLBITMAPS; int const cy = bmp.bmHeight; - HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, 0, 0); + HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, NUMTOOLBITMAPS, NUMTOOLBITMAPS); ImageList_AddMasked(himl, hBmp, CLR_DEFAULT); DPI_T dpi = GetCurrentDPI(hWnd); @@ -2157,10 +2145,10 @@ static HIMAGELIST CreateScaledImageListFromBitmap(HWND hWnd, HBITMAP hBmp) // Scale button icons/images - int const scx = ScaleIntToCurrentDPIX(hWnd, cx); - int const scy = ScaleIntToCurrentDPIX(hWnd, cy); + int const scx = ScaleIntToDPI_X(hWnd, cx); + int const scy = ScaleIntToDPI_Y(hWnd, cy); - HIMAGELIST hsciml = ImageList_Create(scx, scy, ILC_COLOR32 | ILC_MASK | ILC_HIGHQUALITYSCALE, 0, 0); + HIMAGELIST hsciml = ImageList_Create(scx, scy, ILC_COLOR32 | ILC_MASK | ILC_HIGHQUALITYSCALE, NUMTOOLBITMAPS, NUMTOOLBITMAPS); for (int i = 0; i < NUMTOOLBITMAPS; ++i) { @@ -3498,11 +3486,8 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) case SCEN_CHANGE: EditUpdateVisibleIndicators(); MarkAllOccurrences(Settings2.UpdateDelayMarkAllOccurrences, false); - UpdateToolbar(); - UpdateStatusbar(false); break; - case IDT_TIMER_UPDATE_STATUSBAR: _UpdateStatusbarDelayed((bool)lParam); return FALSE; @@ -5377,6 +5362,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); } CheckCmd(GetMenu(Globals.hwndMain), IDM_VIEW_ALWAYSONTOP, Settings.AlwaysOnTop); + UpdateToolbar(); break; @@ -5524,8 +5510,15 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) ShellExecute(0, 0, ONLINE_HELP_WEBSITE, 0, 0, SW_SHOW); break; - case IDM_HELP_ABOUT: - ThemedDialogBox(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_ABOUT), hwnd, AboutDlgProc); + case IDM_HELP_ABOUT: + { + //~HMODULE hRichEdit = LoadLibrary(L"RICHED20.DLL"); // Use RICHEDIT_CONTROL_VER for control in common_res.h + HMODULE hRichEdit = LoadLibrary(L"MSFTEDIT.DLL"); // Use "RichEdit50W" for control in common_res.h; + if (hRichEdit != INVALID_HANDLE_VALUE) { + ThemedDialogBox(Globals.hLngResContainer, MAKEINTRESOURCE(IDD_MUI_ABOUT), hwnd, AboutDlgProc); + FreeLibrary(hRichEdit); + } + } break; case IDM_SETPASS: @@ -8053,7 +8046,6 @@ static void _UpdateToolbarDelayed() EnableTool(Globals.hwndToolbar, IDT_VIEW_TOGGLE_VIEW, b2 && IsFocusedViewAllowed()); CheckTool(Globals.hwndToolbar, IDT_VIEW_TOGGLE_VIEW, tv); - SendWMSize(Globals.hwndToolbar, NULL); } diff --git a/src/Styles.c b/src/Styles.c index f5ed145ef..101d15145 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -1639,7 +1639,7 @@ void Style_HighlightCurrentLine(HWND hwnd, int iHiLitCurLn) if (!Style_StrGetSizeInt(szValue, &iFrameSize)) { iFrameSize = 2; } - iFrameSize = max_i(1, ScaleIntToHwndDPIY(hwnd, iFrameSize)); + iFrameSize = max_i(1, ScaleIntToDPI_Y(hwnd, iFrameSize)); SendMessage(hwnd, SCI_SETCARETLINEFRAME, iFrameSize, 0); } @@ -1661,7 +1661,7 @@ static int _GetMarkerMarginWidth(HWND hwnd) Style_StrGetSize(GetCurrentStdLexer()->Styles[STY_MARGIN].szValue, &fSize); // relative to LineNumber Style_StrGetSize(GetCurrentStdLexer()->Styles[STY_BOOK_MARK].szValue, &fSize); // settings float const zoomPercent = (float)SciCall_GetZoom(); - return ScaleFloatToHwndDPIX(hwnd, (fSize * zoomPercent) / 100.0f); + return ScaleFloatToDPI_X(hwnd, (fSize * zoomPercent) / 100.0f); } //=============================================================================