Skip to content

Commit be411f5

Browse files
committed
Fix attribute value edit height and colmun list width for high DPI
1 parent 9d60da9 commit be411f5

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

resource/MainWindow.manifest

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@
1313
</requestedPrivileges>
1414
</security>
1515
</trustInfo>
16-
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
16+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
1717
<application>
1818
<!--This Id value indicates the application supports Windows Vista/Server 2008 functionality -->
1919
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
2020
<!--This Id value indicates the application supports Windows 7/Server 2008 R2 functionality-->
2121
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
2222
<!--This Id value indicates the application supports Windows 8/Server 2012 functionality-->
2323
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
24-
<!-- This Id value indicates the application supports Windows Blue/Server 2012 R2 functionality-->
24+
<!-- This Id value indicates the application supports Windows 8.1 Blue/Server 2012 R2 functionality-->
2525
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
26-
<!-- This Id value indicates the application supports Windows Threshold functionality-->
26+
<!-- This Id value indicates the application supports Windows Threshold functionality-->
2727
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
2828
</application>
2929
</compatibility>
3030
<asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
3131
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings" xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
32-
<dpiAware>true</dpiAware>
32+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
33+
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2,PerMonitor</dpiAwareness>
3334
<ws2:longPathAware>true</ws2:longPathAware>
3435
</asmv3:windowsSettings>
3536
</asmv3:application>

source/MainWindow.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@
44
#pragma once
55

66

7+
struct WindowDpiScaler
8+
{
9+
int32_t dpiX = 96;
10+
int32_t dpiY = 96;
11+
12+
void UpdateDpi(HWND hwnd)
13+
{
14+
// Ideally we'd support multiple monitors of varying DPI's...
15+
//
16+
// HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
17+
// HRESULT GetDpiForMonitor(honitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
18+
//
19+
// But GetDpiForMonitor is unavailable on Windows 7.
20+
// So hopefully an HDC suitable for the current window is good enough:
21+
22+
HDC hdc = GetDC(hwnd);
23+
dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
24+
dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
25+
ReleaseDC(hwnd, hdc);
26+
}
27+
28+
int32_t ScaleSizeX(int value) {return value * dpiX / 96;};
29+
int32_t ScaleSizeY(int value) {return value * dpiY / 96;};
30+
};
31+
732
class MainWindow
833
{
934
public:
@@ -165,6 +190,7 @@ class MainWindow
165190
std::u16string selectedAttributeValue_;
166191
std::u16string previousSettingsFilePath_;
167192
TextEscapeMode textEscapeMode_ = TextEscapeModeNone;
193+
WindowDpiScaler dpiScaler_;
168194

169195
std::vector<DrawableObjectAndValues> drawableObjects_;
170196
};

source/MainWindow.ixx

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
1-
/*
2-
-Custom font fallback
3-
-Custom font collection
4-
-EDIT/RichEdit
5-
-Show red attribute on parse error
6-
?Character glyph map
7-
?App translucency
8-
+Rendering mode
9-
+GDI AddFontMemResource
10-
+Add font open dialog for font file
11-
+Get all characters
12-
+Add font selection dialog
13-
+Hit test canvas
14-
+Pan scroll canvas
15-
+Font fallback enable/disable
16-
+Store settings
17-
+Load settings
18-
+Transform drawable objects
19-
+Context menu on drawable objects right-click
20-
+Drawable object absolute placement
21-
+Draw labels
22-
+Reflow drawable objects
23-
+GDI+ DrawString/MeasureString
24-
+GDI+ DrawDriverString
25-
+Show red box on drawing error
26-
+Draw object background colors and object together
27-
+Fix user32 DrawText for vertical
28-
+Save selected font file
29-
*/
30-
//----------------------------------------------------------------------------
1+
//----------------------------------------------------------------------------
312
// History: 2015-06-19 Dwayne Robinson - Created
3+
// 2022-03-17 Updated for high DPI
324
//----------------------------------------------------------------------------
335

346
#if USE_CPP_MODULES
@@ -279,6 +251,10 @@ MainWindow::DialogProcResult CALLBACK MainWindow::DialogProc(HWND hwnd, UINT mes
279251
Resize(IddMainWindow);
280252
break;
281253

254+
case WM_DPICHANGED:
255+
dpiScaler_.UpdateDpi(hwnd);
256+
break;
257+
282258
case WM_KEYDOWN:
283259
TranslateAccelerator(hwnd, g_accelTable, &Application::g_msg);
284260
break;
@@ -467,6 +443,8 @@ INT_PTR MainWindow::InitializeMainDialog()
467443
DefWindowProc(hwnd_, WM_SETICON, ICON_BIG, LPARAM(LoadIcon(Application::g_hModule, MAKEINTRESOURCE(1))));
468444
SetWindowText(hwnd_, BUILD_TITLE_STRING);
469445

446+
dpiScaler_.UpdateDpi(hwnd_); // Must be done before resizing.
447+
470448
Edit_LimitText(GetWindowFromId(hwnd_, IdcLog), 1048576);
471449

472450
// Subclass the values edit box for a few reasons.
@@ -586,7 +564,7 @@ void MainWindow::InitializeDrawableObjectsListView()
586564
// columns to data arrays (including strings) and enumerations than
587565
// simple numeric values.
588566
lc.iSubItem = attribute.id;
589-
lc.cx = (attribute.IsTypeArray() || attribute.id == 0) ? 120 : 80;
567+
lc.cx = dpiScaler_.ScaleSizeX((attribute.IsTypeArray() || attribute.id == 0) ? 120 : 80);
590568
lc.pszText = const_cast<LPWSTR>(ToWChar(attribute.display));
591569
ListView_InsertColumn(listViewHwnd, lc.iSubItem, &lc);
592570
}
@@ -652,8 +630,8 @@ void MainWindow::UpdateDrawableObjectsListView()
652630
void MainWindow::InitializeAttributesListView()
653631
{
654632
const static ListViewColumnInfo columnInfo[] = {
655-
{ 0, 120, u"Attribute" },
656-
{ 1, 400, u"Value" },
633+
{ 0, dpiScaler_.ScaleSizeX(120), u"Attribute" },
634+
{ 1, dpiScaler_.ScaleSizeX(400), u"Value" },
657635
};
658636
auto listViewHwnd = GetWindowFromId(hwnd_, IdcAttributesList);
659637
ListView_SetExtendedListViewStyle(listViewHwnd, LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERINALLVIEWS);
@@ -709,8 +687,8 @@ void MainWindow::FillAttributesListView()
709687
void MainWindow::InitializeAttributeValuesListView()
710688
{
711689
const static ListViewColumnInfo columnInfo[] = {
712-
{ 0, 120, u"Name" },
713-
{ 1, 400, u"Value" },
690+
{ 0, dpiScaler_.ScaleSizeX(120), u"Name" },
691+
{ 1, dpiScaler_.ScaleSizeX(400), u"Value" },
714692
};
715693
auto listViewHwnd = GetWindowFromId(hwnd_, IdcAttributeValuesList);
716694
ListView_SetExtendedListViewStyle(listViewHwnd, LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERINALLVIEWS);
@@ -3060,9 +3038,12 @@ void MainWindow::Resize(int id)
30603038
hwnd = hwnd_;
30613039
GetClientRect(hwnd, &clientRect);
30623040
if (IsRectEmpty(&clientRect))
3041+
{
30633042
return; // Avoid unnecessary resizing logic if minimized.
3043+
}
30643044

3065-
InflateRect(&clientRect, -spacing, -spacing);
3045+
auto inflatedSpacing = dpiScaler_.ScaleSizeX(spacing);
3046+
InflateRect(&clientRect, -inflatedSpacing, -inflatedSpacing);
30663047

30673048
WindowPosition windowPositions[] = {
30683049
/* 00 */ WindowPosition(GetWindowFromId(hwnd, IdcDrawableObjectListLoad), PositionOptionsAlignTop),
@@ -3100,12 +3081,14 @@ void MainWindow::Resize(int id)
31003081
WindowPosition& windowPositionAttributeValuesList = windowPositions[22];
31013082

31023083
// Apply initial overall resizing.
3103-
WindowPosition::ReflowGrid(windowPositions, uint32_t(countof(windowPositions)), clientRect, spacing, 0, PositionOptionsNone);
3104-
windowPositionDrawableObjectsList.ClampRect({0x7FFF,340});
3105-
windowPositionAttributesList.ClampRect({340,340});
3106-
windowPositionAttributeValuesList.ClampRect({340,340});
3107-
windowPositionEditText.ClampRect({0x7FFF,160});
3108-
WindowPosition::ReflowGrid(windowPositions, uint32_t(countof(windowPositions)), clientRect, spacing, 0, PositionOptionsNone);
3084+
const int32_t attributeListWidth = dpiScaler_.ScaleSizeX(340);
3085+
const int32_t attributeListHeight = dpiScaler_.ScaleSizeY(340);
3086+
WindowPosition::ReflowGrid(windowPositions, uint32_t(countof(windowPositions)), clientRect, inflatedSpacing, 0, PositionOptionsNone);
3087+
windowPositionDrawableObjectsList.ClampRect({0x7FFF,attributeListWidth});
3088+
windowPositionAttributesList.ClampRect({attributeListHeight,attributeListWidth});
3089+
windowPositionAttributeValuesList.ClampRect({attributeListHeight,attributeListWidth});
3090+
windowPositionEditText.ClampRect({0x7FFF, dpiScaler_.ScaleSizeY(160)});
3091+
WindowPosition::ReflowGrid(windowPositions, uint32_t(countof(windowPositions)), clientRect, inflatedSpacing, 0, PositionOptionsNone);
31093092

31103093
// Resize the objects edit and list controls.
31113094
RECT attributesRect = windowPositionAttributesList.rect;
@@ -3119,9 +3102,11 @@ void MainWindow::Resize(int id)
31193102
#if 0 // todo::: enable slider for variable fonts.
31203103
windowPositionAttributeValuesSlider.options &= ~PositionOptionsIgnored;
31213104
#endif
3105+
31223106
windowPositionAttributeValuesList.SetOptions(PositionOptionsFillHeight, PositionOptionsAlignVMask | PositionOptionsUseSlackHeight);
31233107
WindowPosition::ReflowGrid(&windowPositionAttributeValuesEdit, 3, attributeValuesRect, /*spacing*/0, 0, PositionOptionsFlowVertical | PositionOptionsUnwrapped);
3124-
windowPositionAttributeValuesEdit.ClampRect({0x7FFF, (GetWindowStyle(windowPositionAttributeValuesEdit.hwnd) & ES_MULTILINE) ? 48*3/2 : 12*3/2});
3108+
int windowPositionAttributeValuesEditHeight = (GetWindowStyle(windowPositionAttributeValuesEdit.hwnd) & ES_MULTILINE) ? 48*3/2 : 12*3/2;
3109+
windowPositionAttributeValuesEdit.ClampRect({0x7FFF, dpiScaler_.ScaleSizeX(windowPositionAttributeValuesEditHeight)});
31253110
WindowPosition::ReflowGrid(&windowPositionAttributeValuesEdit, 3, attributeValuesRect, /*spacing*/0, 0, PositionOptionsFlowVertical | PositionOptionsUnwrapped);
31263111

31273112
WindowPosition::Update(windowPositions, uint32_t(countof(windowPositions)));

0 commit comments

Comments
 (0)