Skip to content

Commit 96adce7

Browse files
committed
Chore: Refactoring / cleanup
1 parent fd46a3d commit 96adce7

File tree

2 files changed

+16
-45
lines changed

2 files changed

+16
-45
lines changed

Source/NETworkManager/Controls/PowerShellControl.xaml.cs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ private void WindowsFormsHost_DpiChanged(object sender, DpiChangedEventArgs e)
2525
{
2626
ResizeEmbeddedWindow();
2727

28-
if (!IsConnected || _process == null || _process.HasExited)
28+
if (!IsConnected)
2929
return;
3030

31-
// PowerShell runs inside conhost.exe, a console host process. The Windows
32-
// Console API (kernel32.dll) provides cross-process access to the console's
33-
// font settings, so we can rescale fonts directly without any window message
34-
// passing — completely bypassing the WM_DPICHANGED cross-process delivery
35-
// problem that affects the PuTTY (non-console) embedding approach.
31+
// Rescale the console font of the embedded conhost process so it remains the same physical size when the DPI changes.
3632
NativeMethods.TryRescaleConsoleFont(
3733
(uint)_process.Id,
3834
e.NewDpi.PixelsPerInchX / e.OldDpi.PixelsPerInchX);
@@ -182,18 +178,10 @@ private async Task Connect()
182178

183179
if (_appWin != IntPtr.Zero)
184180
{
185-
// Capture the DPI of conhost's window before embedding. The process
186-
// might have started on a different monitor than ours, so its font
187-
// may be scaled for a different DPI. We correct this after embedding.
181+
// Capture DPI before embedding to correct font scaling afterwards
188182
var initialWindowDpi = NativeMethods.GetDpiForWindow(_appWin);
189183

190-
// Enable mixed-DPI hosting on this thread before SetParent so that
191-
// Windows routes DPI notifications to the cross-process child window.
192-
// SetThreadDpiHostingBehavior is available on Windows 10 1803+.
193-
var prevDpiHosting = NativeMethods.SetThreadDpiHostingBehavior(
194-
NativeMethods.DPI_HOSTING_BEHAVIOR.DPI_HOSTING_BEHAVIOR_MIXED);
195184
NativeMethods.SetParent(_appWin, WindowHost.Handle);
196-
NativeMethods.SetThreadDpiHostingBehavior(prevDpiHosting);
197185

198186
// Show window before set style and resize
199187
NativeMethods.ShowWindow(_appWin, NativeMethods.WindowShowStyle.Maximize);
@@ -205,19 +193,16 @@ private async Task Connect()
205193

206194
IsConnected = true;
207195

208-
// Resize embedded application & refresh
209-
// Requires a short delay because it's not applied immediately
196+
// Resize after short delay — not applied immediately
210197
await Task.Delay(250);
198+
211199
ResizeEmbeddedWindow();
212200

213-
// If conhost started at a different DPI than our monitor (e.g. it
214-
// spawned on a secondary monitor with a different scale factor),
215-
// correct the font now so WindowsFormsHost_DpiChanged always starts
216-
// from the right baseline for relative scaling.
201+
// Correct font if conhost started at a different DPI than our panel
217202
var currentPanelDpi = NativeMethods.GetDpiForWindow(WindowHost.Handle);
203+
218204
if (initialWindowDpi != currentPanelDpi)
219-
NativeMethods.TryRescaleConsoleFont((uint)_process.Id,
220-
(double)currentPanelDpi / initialWindowDpi);
205+
NativeMethods.TryRescaleConsoleFont((uint)_process.Id, (double)currentPanelDpi / initialWindowDpi);
221206
}
222207
}
223208
else

Source/NETworkManager/Controls/PuTTYControl.xaml.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ private void WindowGrid_SizeChanged(object sender, SizeChangedEventArgs e)
2222
ResizeEmbeddedWindow();
2323
}
2424

25-
private async void WindowsFormsHost_DpiChanged(object sender, DpiChangedEventArgs e)
25+
private void WindowsFormsHost_DpiChanged(object sender, DpiChangedEventArgs e)
2626
{
2727
ResizeEmbeddedWindow();
2828

29-
if (!IsConnected || _process == null || _process.HasExited)
29+
if (!IsConnected)
3030
return;
3131

32-
// PuTTY is a GUI application (not console-based), so the Console Font API
33-
// (AttachConsole/SetCurrentConsoleFontEx) does not apply. Instead, send
34-
// WM_DPICHANGED directly to the PuTTY window so it can rescale its fonts
35-
// and layout internally — bypassing the cross-process delivery limitation.
32+
// Send WM_DPICHANGED to the embedded window so it can rescale fonts and UI elements.
3633
NativeMethods.TrySendDpiChangedMessage(
3734
_appWin,
3835
e.OldDpi.PixelsPerInchX,
@@ -194,18 +191,10 @@ private async Task Connect()
194191

195192
if (!_process.HasExited)
196193
{
197-
// Capture PuTTY's window DPI before embedding. The process
198-
// might have started on a different monitor than ours, so its font
199-
// may be scaled for a different DPI. We correct this after embedding.
194+
// Capture DPI before embedding to correct font scaling afterwards
200195
var initialWindowDpi = NativeMethods.GetDpiForWindow(_appWin);
201196

202-
// Enable mixed-DPI hosting on this thread before SetParent so that
203-
// Windows routes DPI notifications to the cross-process child window.
204-
// SetThreadDpiHostingBehavior is available on Windows 10 1803+.
205-
var prevDpiHosting = NativeMethods.SetThreadDpiHostingBehavior(
206-
NativeMethods.DPI_HOSTING_BEHAVIOR.DPI_HOSTING_BEHAVIOR_MIXED);
207197
NativeMethods.SetParent(_appWin, WindowHost.Handle);
208-
NativeMethods.SetThreadDpiHostingBehavior(prevDpiHosting);
209198

210199
// Show window before set style and resize
211200
NativeMethods.ShowWindow(_appWin, NativeMethods.WindowShowStyle.Maximize);
@@ -217,19 +206,16 @@ private async Task Connect()
217206

218207
IsConnected = true;
219208

220-
// Resize embedded application & refresh
221-
// Requires a short delay because it's not applied immediately
209+
// Resize after short delay — not applied immediately
222210
await Task.Delay(250);
223211

224212
ResizeEmbeddedWindow();
225213

226-
// If PuTTY started at a different DPI than our panel (e.g. it
227-
// spawned on a secondary monitor with a different scale factor),
228-
// send WM_DPICHANGED so PuTTY rescales its fonts to match.
214+
// Correct DPI if PuTTY started at a different DPI than our panel
229215
var currentPanelDpi = NativeMethods.GetDpiForWindow(WindowHost.Handle);
216+
230217
if (initialWindowDpi != currentPanelDpi)
231-
NativeMethods.TrySendDpiChangedMessage(_appWin,
232-
initialWindowDpi, currentPanelDpi);
218+
NativeMethods.TrySendDpiChangedMessage(_appWin, initialWindowDpi, currentPanelDpi);
233219
}
234220
}
235221
}

0 commit comments

Comments
 (0)