From 8069659912cf7319b94741d0d5ee9cab8df5f333 Mon Sep 17 00:00:00 2001 From: Bart House Date: Mon, 12 Nov 2018 22:40:36 -0800 Subject: [PATCH] Adjust menu item rendering based on monitor dpi. --- sys/winnt/win10.c | 20 ++++++++++++++++++++ sys/winnt/win10.h | 3 +++ win/win32/mhmap.c | 13 ++----------- win/win32/mhmenu.c | 27 ++++++++++++++++----------- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/sys/winnt/win10.c b/sys/winnt/win10.c index bf2ed9d39..3440f7ed3 100644 --- a/sys/winnt/win10.c +++ b/sys/winnt/win10.c @@ -44,3 +44,23 @@ void win10_init() } } + +int win10_monitor_dpi(HWND hWnd) +{ + UINT monitorDpi = 96; + + if (gWin10.Valid) { + monitorDpi = gWin10.GetDpiForWindow(hWnd); + if (monitorDpi == 0) + monitorDpi = 96; + } + + monitorDpi = max(96, monitorDpi); + + return monitorDpi; +} + +double win10_monitor_scale(HWND hWnd) +{ + return (double) win10_monitor_dpi(hWnd) / 96.0; +} \ No newline at end of file diff --git a/sys/winnt/win10.h b/sys/winnt/win10.h index 02b943b56..1896b4cc5 100644 --- a/sys/winnt/win10.h +++ b/sys/winnt/win10.h @@ -22,5 +22,8 @@ typedef struct { extern Win10 gWin10; void win10_init(); +int win10_monitor_dpi(HWND hWnd); +double win10_monitor_scale(HWND hWnd); + #endif // WIN10_H \ No newline at end of file diff --git a/win/win32/mhmap.c b/win/win32/mhmap.c index 4f69a6e31..730232fae 100644 --- a/win/win32/mhmap.c +++ b/win/win32/mhmap.c @@ -129,17 +129,8 @@ mswin_map_stretch(HWND hWnd, LPSIZE map_size, BOOL redraw) wnd_size.cx = client_rt.right - client_rt.left; wnd_size.cy = client_rt.bottom - client_rt.top; - /* calculate monitor dpi and monitor scale */ - UINT monitorDpi = 96; - - if (gWin10.Valid) { - monitorDpi = gWin10.GetDpiForWindow(hWnd); - if (monitorDpi == 0) - monitorDpi = 96; - } - - monitorDpi = max(96, monitorDpi); - data->monitorScale = (double) monitorDpi / 96.0; + // calculate back buffer scale + data->monitorScale = win10_monitor_scale(hWnd); if (data->bAsciiMode) { data->backScale = data->monitorScale; diff --git a/win/win32/mhmenu.c b/win/win32/mhmenu.c index a298f3893..4007ee177 100644 --- a/win/win32/mhmenu.c +++ b/win/win32/mhmenu.c @@ -2,6 +2,7 @@ /* Copyright (c) Alex Kompel, 2002 */ /* NetHack may be freely redistributed. See license for details. */ +#include "win10.h" #include "winMS.h" #include #include "resource.h" @@ -975,6 +976,9 @@ onDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam) int color = NO_COLOR, attr; boolean menucolr = FALSE; + double monitorScale = win10_monitor_scale(hWnd); + int tileXScaled = (int) (TILE_X * monitorScale); + int tileYScaled = (int) (TILE_Y * monitorScale); UNREFERENCED_PARAMETER(wParam); @@ -1024,15 +1028,15 @@ onDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam) break; } - y = (lpdis->rcItem.bottom + lpdis->rcItem.top - TILE_Y) / 2; + y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tileYScaled) / 2; SetBrushOrgEx(lpdis->hDC, x, y, NULL); saveBrush = SelectObject(lpdis->hDC, hbrCheckMark); - PatBlt(lpdis->hDC, x, y, TILE_X, TILE_Y, PATCOPY); + PatBlt(lpdis->hDC, x, y, tileXScaled, tileYScaled, PATCOPY); SelectObject(lpdis->hDC, saveBrush); DeleteObject(hbrCheckMark); } - x += TILE_X + spacing; + x += tileXScaled + spacing; if (item->accelerator != 0) { buf[0] = item->accelerator; @@ -1053,13 +1057,14 @@ onDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam) } x += tm.tmAveCharWidth + tm.tmOverhang + spacing; } else { - x += TILE_X + tm.tmAveCharWidth + tm.tmOverhang + 2 * spacing; + x += tileXScaled + tm.tmAveCharWidth + tm.tmOverhang + 2 * spacing; } /* print glyph if present */ if (NHMENU_HAS_GLYPH(*item)) { if (!IS_MAP_ASCII(iflags.wc_map_mode)) { HGDIOBJ saveBmp; + double monitorScale = win10_monitor_scale(hWnd); saveBmp = SelectObject(tileDC, GetNHApp()->bmpMapTiles); ntile = glyph2tile[item->glyph]; @@ -1068,21 +1073,21 @@ onDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam) t_y = (ntile / GetNHApp()->mapTilesPerLine) * GetNHApp()->mapTile_Y; - y = (lpdis->rcItem.bottom + lpdis->rcItem.top - - GetNHApp()->mapTile_Y) / 2; + y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tileYScaled) / 2; if (GetNHApp()->bmpMapTiles == GetNHApp()->bmpTiles) { /* using original nethack tiles - apply image transparently */ - (*GetNHApp()->lpfnTransparentBlt)(lpdis->hDC, x, y, TILE_X, TILE_Y, + (*GetNHApp()->lpfnTransparentBlt)(lpdis->hDC, x, y, + tileXScaled, tileYScaled, tileDC, t_x, t_y, TILE_X, TILE_Y, TILE_BK_COLOR); } else { /* using custom tiles - simple blt */ - BitBlt(lpdis->hDC, x, y, GetNHApp()->mapTile_X, - GetNHApp()->mapTile_Y, tileDC, t_x, t_y, SRCCOPY); + StretchBlt(lpdis->hDC, x, y, tileXScaled, tileYScaled, + tileDC, t_x, t_y, GetNHApp()->mapTile_X, GetNHApp()->mapTile_Y, SRCCOPY); } SelectObject(tileDC, saveBmp); - x += GetNHApp()->mapTile_X; + x += tileXScaled; } else { const char *sel_ind; switch (item->count) { @@ -1106,7 +1111,7 @@ onDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam) } } else { /* no glyph - need to adjust so help window won't look to cramped */ - x += TILE_X; + x += tileXScaled; } x += spacing;