From ad58051a92a3549ed3af1b02c21fc41f73ff7c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Sun, 24 May 2020 00:20:17 +0200 Subject: [PATCH] Small corrections to improve the DPI awareness (display scaling). --- GUI/AboutBox.Designer.cs | 4 +-- GUI/DpiHelper.cs | 54 ++++++++++++++++++++++++++++++++++++++ GUI/MainForm.Designer.cs | 11 +++----- GUI/MainForm.cs | 21 +++++++++++---- OpenHardwareMonitor.csproj | 1 + 5 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 GUI/DpiHelper.cs diff --git a/GUI/AboutBox.Designer.cs b/GUI/AboutBox.Designer.cs index 4637fb1..3a64aba 100644 --- a/GUI/AboutBox.Designer.cs +++ b/GUI/AboutBox.Designer.cs @@ -62,7 +62,7 @@ namespace OpenHardwareMonitor.GUI { this.pictureBox1.Location = new System.Drawing.Point(10, 11); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(48, 48); - this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; + this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; // @@ -169,4 +169,4 @@ namespace OpenHardwareMonitor.GUI { private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; private System.Windows.Forms.LinkLabel licenseLinkLabel; } -} \ No newline at end of file +} diff --git a/GUI/DpiHelper.cs b/GUI/DpiHelper.cs new file mode 100644 index 0000000..b33948b --- /dev/null +++ b/GUI/DpiHelper.cs @@ -0,0 +1,54 @@ +/* + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + Copyright (C) 2020 Michael Möller + +*/ + +using System; +using System.Drawing; + +namespace OpenHardwareMonitor.GUI { + + public static class DpiHelper { + public const double LogicalDpi = 96.0; + + private static double deviceDpi; + public static double DeviceDpi { + get { + if (deviceDpi == 0.0) { + try { + using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { + deviceDpi = g.DpiX; + } + } catch { } + if (deviceDpi == 0.0) + deviceDpi = LogicalDpi; + } + return deviceDpi; + } + } + + private static double logicalToDeviceUnitsScalingFactor; + public static double LogicalToDeviceUnitsScalingFactor { + get { + if (logicalToDeviceUnitsScalingFactor == 0.0) { + logicalToDeviceUnitsScalingFactor = DeviceDpi / LogicalDpi; + } + return logicalToDeviceUnitsScalingFactor; + } + } + + public static int LogicalToDeviceUnits(int value) { + return (int)Math.Round(LogicalToDeviceUnitsScalingFactor * (double)value); + } + + public static Size LogicalToDeviceUnits(Size logicalSize) { + return new Size(LogicalToDeviceUnits(logicalSize.Width), + LogicalToDeviceUnits(logicalSize.Height)); + } + } +} diff --git a/GUI/MainForm.Designer.cs b/GUI/MainForm.Designer.cs index f602030..aaff0c2 100644 --- a/GUI/MainForm.Designer.cs +++ b/GUI/MainForm.Designer.cs @@ -119,29 +119,26 @@ namespace OpenHardwareMonitor.GUI { // this.sensor.Header = "Sensor"; this.sensor.SortOrder = System.Windows.Forms.SortOrder.None; - this.sensor.TooltipText = null; - this.sensor.Width = 250; + this.sensor.TooltipText = null; // // value // this.value.Header = "Value"; this.value.SortOrder = System.Windows.Forms.SortOrder.None; - this.value.TooltipText = null; - this.value.Width = 100; + this.value.TooltipText = null; // // min // this.min.Header = "Min"; this.min.SortOrder = System.Windows.Forms.SortOrder.None; - this.min.TooltipText = null; - this.min.Width = 100; + this.min.TooltipText = null; // // max // this.max.Header = "Max"; this.max.SortOrder = System.Windows.Forms.SortOrder.None; this.max.TooltipText = null; - this.max.Width = 100; + // // nodeImage // diff --git a/GUI/MainForm.cs b/GUI/MainForm.cs index 905a809..6359af9 100644 --- a/GUI/MainForm.cs +++ b/GUI/MainForm.cs @@ -112,8 +112,14 @@ namespace OpenHardwareMonitor.GUI { nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText; nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing; + this.sensor.Width = DpiHelper.LogicalToDeviceUnits(250); + this.value.Width = DpiHelper.LogicalToDeviceUnits(100); + this.min.Width = DpiHelper.LogicalToDeviceUnits(100); + this.max.Width = DpiHelper.LogicalToDeviceUnits(100); + foreach (TreeColumn column in treeView.Columns) - column.Width = Math.Max(20, Math.Min(400, + column.Width = Math.Max(DpiHelper.LogicalToDeviceUnits(20), Math.Min( + DpiHelper.LogicalToDeviceUnits(400), settings.GetValue("treeView.Columns." + column.Header + ".Width", column.Width))); @@ -131,7 +137,8 @@ namespace OpenHardwareMonitor.GUI { systemTray.ExitCommand += exitClick; if (Hardware.OperatingSystem.IsUnix) { // Unix - treeView.RowHeight = Math.Max(treeView.RowHeight, 18); + treeView.RowHeight = Math.Max(treeView.RowHeight, + DpiHelper.LogicalToDeviceUnits(18)); splitContainer.BorderStyle = BorderStyle.None; splitContainer.Border3DStyle = Border3DStyle.Adjust; splitContainer.SplitterWidth = 4; @@ -142,7 +149,9 @@ namespace OpenHardwareMonitor.GUI { minTrayMenuItem.Visible = false; startMinMenuItem.Visible = false; } else { // Windows - treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18); + treeView.RowHeight = Math.Max(treeView.Font.Height + + DpiHelper.LogicalToDeviceUnits(1), + DpiHelper.LogicalToDeviceUnits(18)); gadget = new SensorGadget(computer, settings, unitManager); gadget.HideShowCommand += hideShowClick; @@ -617,8 +626,10 @@ namespace OpenHardwareMonitor.GUI { Rectangle newBounds = new Rectangle { X = settings.GetValue("mainForm.Location.X", Location.X), Y = settings.GetValue("mainForm.Location.Y", Location.Y), - Width = settings.GetValue("mainForm.Width", 470), - Height = settings.GetValue("mainForm.Height", 640) + Width = settings.GetValue("mainForm.Width", + DpiHelper.LogicalToDeviceUnits(470)), + Height = settings.GetValue("mainForm.Height", + DpiHelper.LogicalToDeviceUnits(640)) }; Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue, diff --git a/OpenHardwareMonitor.csproj b/OpenHardwareMonitor.csproj index 1cb783c..978c757 100644 --- a/OpenHardwareMonitor.csproj +++ b/OpenHardwareMonitor.csproj @@ -69,6 +69,7 @@ +