Skip to content

Commit 176e76b

Browse files
committed
Enable support of High DPI by default
1 parent 44e9010 commit 176e76b

File tree

12 files changed

+201
-19
lines changed

12 files changed

+201
-19
lines changed

WindowTextExtractor/Forms/MainForm.Designer.cs

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ public partial class MainForm : Form, IMessageFilter
5656
private AccurateTimer _writeVideoFrameTimer;
5757

5858

59-
public MainForm()
59+
public MainForm(ApplicationSettings settings)
6060
{
6161
InitializeComponent();
62-
63-
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
64-
Application.ThreadException += OnThreadException;
62+
6563
using var currentProcess = Process.GetCurrentProcess();
6664

6765
magnifier.BorderWidth = 1;
@@ -86,6 +84,7 @@ public MainForm()
8684
_startRecordingTime = null;
8785
_image = null;
8886
_videoWriter = new VideoFileWriter();
87+
_settings = settings;
8988
}
9089

9190
protected override void OnLoad(EventArgs e)
@@ -546,6 +545,12 @@ private void MenuItemCheckedClick(object sender, EventArgs e)
546545
_settings.Magnifier.Enabled = menuItem.Checked;
547546
}
548547
break;
548+
549+
case "menuItemHighDpiSupport":
550+
{
551+
_settings.HighDpiSupport = menuItem.Checked;
552+
}
553+
break;
549554
}
550555
SaveSettings(_settings);
551556
}
@@ -1399,7 +1404,6 @@ private void SetImage(string fileName)
13991404

14001405
private void LoadSettings()
14011406
{
1402-
_settings = ApplicationSettingsFile.Read();
14031407
_videoFileName = Path.Combine(AssemblyUtils.AssemblyDirectory, _settings.VideoFileName);
14041408
numericFps.Value = _settings.FPS;
14051409
numericScale.Value = _settings.Scale;
@@ -1413,6 +1417,7 @@ private void LoadSettings()
14131417
menuItemNotRepeated.Checked = _settings.NotRepeatedNewItems;
14141418
menuItemAlwaysRefreshTabs.Checked = _settings.AlwaysRefreshTabs;
14151419
menuItemMagnifierEnabled.Checked = _settings.Magnifier.Enabled;
1420+
menuItemHighDpiSupport.Checked = _settings.HighDpiSupport;
14161421
cmbRefresh.SelectedIndex = _settings.RefreshImage ? 0 : 1;
14171422
cmbCaptureCursor.SelectedIndex = _settings.CaptureCursor ? 0 : 1;
14181423
txtContent.Font = new Font(_settings.Font.Name, _settings.Font.Size, _settings.Font.Style, _settings.Font.Unit);
@@ -1434,14 +1439,5 @@ private void SaveSettings(ApplicationSettings settings)
14341439
MessageBox.Show($"Failed to save the settings.{Environment.NewLine}{e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
14351440
}
14361441
}
1437-
1438-
private void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
1439-
{
1440-
var ex = e.ExceptionObject as Exception ?? new Exception("OnCurrentDomainUnhandledException");
1441-
OnThreadException(sender, new ThreadExceptionEventArgs(ex));
1442-
}
1443-
1444-
private void OnThreadException(object sender, ThreadExceptionEventArgs e) =>
1445-
MessageBox.Show(e.Exception.Message, AssemblyUtils.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
14461442
}
14471443
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace WindowTextExtractor.Native.Enums
2+
{
3+
enum DpiAwarenessContext
4+
{
5+
DPI_AWARENESS_CONTEXT_UNAWARE = 16,
6+
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17,
7+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18,
8+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace WindowTextExtractor.Native.Enums
2+
{
3+
enum ProcessDpiAwareness
4+
{
5+
Process_DPI_Unaware = 0,
6+
Process_System_DPI_Aware = 1,
7+
Process_Per_Monitor_DPI_Aware = 2
8+
}
9+
}

WindowTextExtractor/Native/Shcore.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace WindowTextExtractor.Native
66
{
7-
static class Shcore
7+
static class SHCore
88
{
99
[DllImport("Shcore.dll")]
1010
public static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
11+
12+
[DllImport("SHCore.dll", SetLastError = true)]
13+
public static extern bool SetProcessDpiAwareness(ProcessDpiAwareness processDpiAwareness);
1114
}
1215
}

WindowTextExtractor/Native/User32.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,11 @@ static class User32
129129

130130
[DllImport("User32.dll")]
131131
public static extern IntPtr MonitorFromPoint([In] Point pt, [In] uint dwFlags);
132+
133+
[DllImport("user32.dll", SetLastError = true)]
134+
public static extern bool SetProcessDpiAwarenessContext(DpiAwarenessContext context);
135+
136+
[DllImport("user32.dll", SetLastError = true)]
137+
public static extern bool SetProcessDPIAware();
132138
}
133139
}

WindowTextExtractor/Program.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Windows.Forms;
3+
using System.Threading;
34
using WindowTextExtractor.Forms;
5+
using WindowTextExtractor.Utils;
6+
using WindowTextExtractor.Settings;
47
using WindowTextExtractor.Native;
58

69
namespace WindowTextExtractor
@@ -13,10 +16,21 @@ static class Program
1316
[STAThread]
1417
static void Main(string[] args)
1518
{
19+
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
20+
Application.ThreadException += OnThreadException;
1621
#if WIN32
22+
var settings = ApplicationSettingsFile.Read();
23+
24+
// Enable support of high DPI
25+
if (settings.HighDpiSupport)
26+
{
27+
SystemUtils.EnableHighDpiSupport();
28+
}
29+
30+
1731
Application.EnableVisualStyles();
1832
Application.SetCompatibleTextRenderingDefault(false);
19-
Application.Run(new MainForm());
33+
Application.Run(new MainForm(settings));
2034
#else
2135
var hwndCaller = 0;
2236
var hwndTarget = 0;
@@ -35,5 +49,14 @@ static void Main(string[] args)
3549
}
3650
#endif
3751
}
52+
53+
static void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
54+
{
55+
var ex = e.ExceptionObject as Exception ?? new Exception("OnCurrentDomainUnhandledException");
56+
OnThreadException(sender, new ThreadExceptionEventArgs(ex));
57+
}
58+
59+
static void OnThreadException(object sender, ThreadExceptionEventArgs e) =>
60+
MessageBox.Show(e.Exception.Message, AssemblyUtils.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
3861
}
3962
}

WindowTextExtractor/Settings/ApplicationSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class ApplicationSettings
77
private const decimal DefaultScale = 1;
88
private const int DefaultBorderWidth = 3;
99
private const string DefaultBorderColor = "Blue";
10+
private const bool DefaultHighDpiSupport = true;
1011

1112
public const int ImageSize = 48;
1213
public const int IconSize = 32;
@@ -35,6 +36,8 @@ public class ApplicationSettings
3536

3637
public bool CaptureCursor { get; set; }
3738

39+
public bool HighDpiSupport { get; set; }
40+
3841
public TargetIconType TargetIcon { get; set; }
3942

4043
public FontSettings Font { get; set; }
@@ -55,6 +58,7 @@ public class ApplicationSettings
5558
AlwaysRefreshTabs = true,
5659
RefreshImage = true,
5760
CaptureCursor = true,
61+
HighDpiSupport = DefaultHighDpiSupport,
5862
TargetIcon = TargetIconType.Default,
5963
Font = new FontSettings(),
6064
Magnifier = new MagnifierSettings()

WindowTextExtractor/Utils/DPIUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static uint GetDpi(Control control, Point monitorPoint)
8888
{
8989
var monitorFromPoint = User32.MonitorFromPoint(monitorPoint, 2);
9090

91-
Shcore.GetDpiForMonitor(monitorFromPoint, DpiType.Effective, out dpiX, out _);
91+
SHCore.GetDpiForMonitor(monitorFromPoint, DpiType.Effective, out dpiX, out _);
9292
}
9393
else
9494
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using WindowTextExtractor.Native;
3+
using WindowTextExtractor.Native.Enums;
4+
5+
namespace WindowTextExtractor.Utils
6+
{
7+
static class SystemUtils
8+
{
9+
public static void EnableHighDpiSupport()
10+
{
11+
if (Environment.OSVersion.Version.Major <= 5)
12+
{
13+
return;
14+
}
15+
16+
if (Environment.OSVersion.Version >= new Version(6, 3, 0)) // win 8.1 added support for per monitor dpi
17+
{
18+
if (Environment.OSVersion.Version >= new Version(10, 0, 15063)) // win 10 creators update added support for per monitor v2
19+
{
20+
User32.SetProcessDpiAwarenessContext(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
21+
}
22+
else
23+
{
24+
SHCore.SetProcessDpiAwareness(ProcessDpiAwareness.Process_Per_Monitor_DPI_Aware);
25+
}
26+
}
27+
else
28+
{
29+
User32.SetProcessDPIAware();
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)