From 7b7fcdfef7487770d3089bafaedf467e30ed3afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 1 Apr 2022 13:29:25 +0200 Subject: Support per-monitor DPI. --- c/common.c | 18 ++++++++++++++++++ c/datalistview.c | 4 +++- c/defs.h | 1 + c/episodelistview.c | 9 ++++++--- c/listview.c | 2 +- c/main.c | 18 +++++++++++++++++- 6 files changed, 46 insertions(+), 6 deletions(-) (limited to 'c') diff --git a/c/common.c b/c/common.c index 804611d..a44462d 100644 --- a/c/common.c +++ b/c/common.c @@ -4,6 +4,24 @@ #include "resource.h" #include "defs.h" +/* Scale integer according to DPI. */ +int Dpi(int i) +{ + extern int IDPI; + + if (IDPI == -1) { + HMODULE hModule; + FARPROC GetDpiForWindow; + extern HWND HWnd; + IDPI = 96; + hModule = LoadLibrary(TEXT("User32.dll")); + if (hModule && (GetDpiForWindow = GetProcAddress(hModule, "GetDpiForWindow"))) + IDPI = GetDpiForWindow(HWnd); + } + + return MulDiv(i, IDPI, 96); +} + /* Convert normal string to TSTR using given codepage. */ TCHAR * TszFromSz(const char *sz, int iCp) diff --git a/c/datalistview.c b/c/datalistview.c index 901f16f..637350d 100644 --- a/c/datalistview.c +++ b/c/datalistview.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -17,7 +18,8 @@ DlvCreate() lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM; lvc.iSubItem = 0; lvc.pszText = TEXT("Key"); - lvc.cx = 42; + + lvc.cx = Dpi(42); ListView_InsertColumn(HDlv, 0, &lvc); lvc.iSubItem = 1; diff --git a/c/defs.h b/c/defs.h index afa3927..82bde40 100644 --- a/c/defs.h +++ b/c/defs.h @@ -5,6 +5,7 @@ #include /* common.c */ +int Dpi(int); TCHAR *TszFromSz(const char *, int); int Watched(int); diff --git a/c/episodelistview.c b/c/episodelistview.c index 95f234a..76c99d9 100644 --- a/c/episodelistview.c +++ b/c/episodelistview.c @@ -23,7 +23,7 @@ ElvCreate() lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM; lvc.iSubItem = 0; lvc.pszText = TEXT("#"); - lvc.cx = 42; + lvc.cx = Dpi(42); ListView_InsertColumn(HElv, 0, &lvc); lvc.iSubItem = 1; @@ -117,10 +117,13 @@ ElvSelectRecent() term_t t; t = T(1); - P("track_episodes","most_recently_watched",1,t) return; + P("track_episodes","most_recently_watched",1,t) { + iEpisode = 1; + goto sel; + } GI(t,&iEpisode) return; - lvfi.flags = LVFI_PARAM; +sel: lvfi.flags = LVFI_PARAM; lvfi.lParam = iEpisode; iItem = ListView_FindItem(HElv, -1, &lvfi); diff --git a/c/listview.c b/c/listview.c index ce81adf..628ed13 100644 --- a/c/listview.c +++ b/c/listview.c @@ -74,7 +74,7 @@ LvHeight(HWND hLv) { int iCount; iCount = ListView_GetItemCount(hLv); - return iCount? 27+iCount*19: 0; + return iCount? Dpi(27)+iCount*Dpi(19): 0; } /* Enable/disable non-classic list view theme. */ diff --git a/c/main.c b/c/main.c index 4b3a91d..3f60407 100644 --- a/c/main.c +++ b/c/main.c @@ -11,6 +11,7 @@ HFONT HfBold; HMENU HPopupMenu; HWND HFocus; HWND HWnd; +int IDPI = -1; static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); static int Attach(void); @@ -60,6 +61,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, tszErr = TEXT("Could not register window class."); if (!RegisterClassEx(&wc)) goto f; + /* Create window. */ hWnd = CreateWindowEx( @@ -67,11 +69,12 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, TEXT("Episode Browser"), TEXT("Episode Browser"), WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, 510, 400, + CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, hInstance, NULL ); tszErr = TEXT("Could not create main window."); if (!hWnd) goto f; + SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(400), SWP_NOMOVE); SetupFonts(); ShowWindow(hWnd, nCmdShow); @@ -121,6 +124,19 @@ WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_THEMECHANGED: UpdateTheme(); break; + case 0x02E0: /* WM_DPICHANGED */ + { + LPRECT lpr; + lpr = (LPRECT)lParam; + IDPI = HIWORD(wParam); + SetWindowPos(hWnd, NULL, + lpr->left, lpr->top, + lpr->right-lpr->left, + lpr->bottom-lpr->top, + SWP_NOZORDER | SWP_NOACTIVATE); + UpdateLayout(); + break; + } case WM_ACTIVATE: switch (wParam) { case WA_INACTIVE: -- cgit v1.2.3