@@ -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
0 commit comments