Skip to content

Commit ad58051

Browse files
committed
Small corrections to improve the DPI awareness (display scaling).
1 parent c623d6b commit ad58051

5 files changed

+77
-14
lines changed

GUI/AboutBox.Designer.cs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/DpiHelper.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
Copyright (C) 2020 Michael Möller <[email protected]>
8+
9+
*/
10+
11+
using System;
12+
using System.Drawing;
13+
14+
namespace OpenHardwareMonitor.GUI {
15+
16+
public static class DpiHelper {
17+
public const double LogicalDpi = 96.0;
18+
19+
private static double deviceDpi;
20+
public static double DeviceDpi {
21+
get {
22+
if (deviceDpi == 0.0) {
23+
try {
24+
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) {
25+
deviceDpi = g.DpiX;
26+
}
27+
} catch { }
28+
if (deviceDpi == 0.0)
29+
deviceDpi = LogicalDpi;
30+
}
31+
return deviceDpi;
32+
}
33+
}
34+
35+
private static double logicalToDeviceUnitsScalingFactor;
36+
public static double LogicalToDeviceUnitsScalingFactor {
37+
get {
38+
if (logicalToDeviceUnitsScalingFactor == 0.0) {
39+
logicalToDeviceUnitsScalingFactor = DeviceDpi / LogicalDpi;
40+
}
41+
return logicalToDeviceUnitsScalingFactor;
42+
}
43+
}
44+
45+
public static int LogicalToDeviceUnits(int value) {
46+
return (int)Math.Round(LogicalToDeviceUnitsScalingFactor * (double)value);
47+
}
48+
49+
public static Size LogicalToDeviceUnits(Size logicalSize) {
50+
return new Size(LogicalToDeviceUnits(logicalSize.Width),
51+
LogicalToDeviceUnits(logicalSize.Height));
52+
}
53+
}
54+
}

GUI/MainForm.Designer.cs

+4-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/MainForm.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,14 @@ public MainForm() {
112112
nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
113113
nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
114114

115+
this.sensor.Width = DpiHelper.LogicalToDeviceUnits(250);
116+
this.value.Width = DpiHelper.LogicalToDeviceUnits(100);
117+
this.min.Width = DpiHelper.LogicalToDeviceUnits(100);
118+
this.max.Width = DpiHelper.LogicalToDeviceUnits(100);
119+
115120
foreach (TreeColumn column in treeView.Columns)
116-
column.Width = Math.Max(20, Math.Min(400,
121+
column.Width = Math.Max(DpiHelper.LogicalToDeviceUnits(20), Math.Min(
122+
DpiHelper.LogicalToDeviceUnits(400),
117123
settings.GetValue("treeView.Columns." + column.Header + ".Width",
118124
column.Width)));
119125

@@ -131,7 +137,8 @@ public MainForm() {
131137
systemTray.ExitCommand += exitClick;
132138

133139
if (Hardware.OperatingSystem.IsUnix) { // Unix
134-
treeView.RowHeight = Math.Max(treeView.RowHeight, 18);
140+
treeView.RowHeight = Math.Max(treeView.RowHeight,
141+
DpiHelper.LogicalToDeviceUnits(18));
135142
splitContainer.BorderStyle = BorderStyle.None;
136143
splitContainer.Border3DStyle = Border3DStyle.Adjust;
137144
splitContainer.SplitterWidth = 4;
@@ -142,7 +149,9 @@ public MainForm() {
142149
minTrayMenuItem.Visible = false;
143150
startMinMenuItem.Visible = false;
144151
} else { // Windows
145-
treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18);
152+
treeView.RowHeight = Math.Max(treeView.Font.Height +
153+
DpiHelper.LogicalToDeviceUnits(1),
154+
DpiHelper.LogicalToDeviceUnits(18));
146155

147156
gadget = new SensorGadget(computer, settings, unitManager);
148157
gadget.HideShowCommand += hideShowClick;
@@ -617,8 +626,10 @@ private void MainForm_Load(object sender, EventArgs e) {
617626
Rectangle newBounds = new Rectangle {
618627
X = settings.GetValue("mainForm.Location.X", Location.X),
619628
Y = settings.GetValue("mainForm.Location.Y", Location.Y),
620-
Width = settings.GetValue("mainForm.Width", 470),
621-
Height = settings.GetValue("mainForm.Height", 640)
629+
Width = settings.GetValue("mainForm.Width",
630+
DpiHelper.LogicalToDeviceUnits(470)),
631+
Height = settings.GetValue("mainForm.Height",
632+
DpiHelper.LogicalToDeviceUnits(640))
622633
};
623634

624635
Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,

OpenHardwareMonitor.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Reference Include="System.Xml" />
7070
</ItemGroup>
7171
<ItemGroup>
72+
<Compile Include="GUI\DpiHelper.cs" />
7273
<Compile Include="GUI\GadgetWindow.cs" />
7374
<Compile Include="GUI\Gadget.cs" />
7475
<Compile Include="GUI\HardwareTypeImage.cs" />

0 commit comments

Comments
 (0)