[1.14] ShowWindow(SW_MINIMIZE) doesn't work anymore #17438

Closed
opened 2026-01-31 05:42:29 +00:00 by claunia · 5 comments
Owner

Originally created by @zadjii-msft on GitHub (May 9, 2022).

From bug bash.

  1. put in file:
$Source=@"
using System;
using System.Runtime.InteropServices;
public class Native {
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetConsoleWindow();
}
"@
Add-Type -TypeDefinition $Source
[Native]::ShowWindow([Native]::GetConsoleWindow(), 6)
  1. Run .\hide.ps1

expect:

  • See a dterm sequence for hiding the window

actual:

  • Terminal still visible.
Originally created by @zadjii-msft on GitHub (May 9, 2022). From bug bash. 1. put in file: ```pwsh $Source=@" using System; using System.Runtime.InteropServices; public class Native { [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetConsoleWindow(); } "@ Add-Type -TypeDefinition $Source [Native]::ShowWindow([Native]::GetConsoleWindow(), 6) ``` 2. Run `.\hide.ps1` expect: * See a dterm sequence for hiding the window actual: * Terminal still visible.
Author
Owner

@zadjii-msft commented on GitHub (May 10, 2022):

I was the murderer

image

69b77cae83

That's a 1 line fix, but fixing that causes the window to flicker uncontrollably. Somewhere there's a mismatch between the Terminal HWND visibility and the ConPTY one, some flipped boolean somewhere.

@zadjii-msft commented on GitHub (May 10, 2022): I was the murderer ![image](https://user-images.githubusercontent.com/18356694/167620964-4a614678-d3b1-48a5-9130-628b9127f235.png) 69b77cae83f8b73dfcc71f99eb75c3db8a1572a9 That's a 1 line fix, but fixing that causes the window to flicker uncontrollably. Somewhere there's a mismatch between the Terminal HWND visibility and the ConPTY one, some flipped boolean somewhere.
Author
Owner

@zadjii-msft commented on GitHub (May 16, 2022):

  • ✔️ 22fb15f0a - definitely works, no flashing.
  • 90d6fe792b
  • 419551e39c
  • 5b59cf8723
  • ✔️ 6bdc8c9285
  • ⚠️ 69b77cae83 - the bad merge. Presumably, anything after here doesn't work, but maybe with the merge fixed they would?
    • Even with the fix to the merge, this causes the flashing. Everything after here would as well.
    • This was a merge with 26d67d9c0a, which was #12526.
    • ✔️ 479c6c9f08 was my branch where I thought I merged these all and it worked. That's basically a merge of (5923cf3, 23b6fee1f0, and 5923cf3, the last of which was NOT the whole focus/foreground implementation, merely a prototype. Shiz. )
      • This commit DOES work. So somewhere between there and 69b77cae83 broke. Interesting.
  • d54e6d13ce
  • 9aa987f6d5
  • f70c53db84
  • 46b88e189b
  • 3b9710a4a - didn't work, but neither does the test script so clearly I never tested that build. That's after the bad merge, so yea that doesn't work,
  • main
@zadjii-msft commented on GitHub (May 16, 2022): * ✔️ 22fb15f0a - definitely works, no flashing. * ❔ 90d6fe792b969ad1913088087a1fda555ef0bb9d * ❔ 419551e39cb943eb0d067bfcc047b7ba8521eeee * ❔ 5b59cf87232495312f829015631ae509f5268052 * ✔️ 6bdc8c928524111809798d9ef155b6cb87060f33 * ⚠️ 69b77cae83f8b73dfcc71f99eb75c3db8a1572a9 - the bad merge. Presumably, anything after here doesn't work, but maybe with the merge fixed they would? * Even with the fix to the merge, this causes the flashing. Everything after here would as well. * This was a merge with 26d67d9c0af0a05b0e65aa76537006862d39535b, which was #12526. * ✔️ 479c6c9f08cf273768e64f9f7fbc74a2c231b815 was my branch where I thought I merged these all and it worked. That's basically a merge of (5923cf3, 23b6fee1f0b1722a9f081e52b8f47eea45a0f709, and 5923cf3, the last of which was NOT the whole focus/foreground implementation, merely a prototype. Shiz. ) * This commit DOES work. So somewhere between there and 69b77cae83f8b73dfcc71f99eb75c3db8a1572a9 broke. Interesting. * ❔ d54e6d13ce606e42c03d1b4a485066b22eb33aa8 * ❔ 9aa987f6d581c7af7e7c19013310fca478abd34e * ❔ f70c53db84664d911aeace245aa34d63b4619b38 * ❔ 46b88e189bc0b39c95556f9c1335fd7c4310a52b * ❌ 3b9710a4a - didn't work, but neither does the test script so clearly I never tested that build. That's after the bad merge, so yea that doesn't work, * ❌ `main`
Author
Owner

@zadjii-msft commented on GitHub (May 17, 2022):

Hmm. It's the combo of 22fb15f0a6 with #12526 that causes this. That initial show/hide to sync the terminal with the conpty, that's what's busted somehow.

Merely commenting those lines fixes this. (see bbae2355c) HOWEVER, it breaks doing a [Native]::ShowWindow([Native]::GetConsoleWindow(), 0) (0==SW_HIDE) immediately in a fresh window. Trick here: the Terminal starts with WS_VISIBLE | !WS_MINIMIZE. The Conpty starts with !WS_VISIBLE | !WS_MINIMIZE. Somewhere early in startup, we get a WM_SHOWWINDOW(false, 0), and that then bounces with the terminal, because that results in the pty telling the terminal to hide.

  • I need to validate that [Native]::ShowWindow([Native]::GetConsoleWindow(), 0) did work between 2fb15f and 69b77ca
  • ✔️ Does removing the ConptyReparentPseudoConsole call instead fix this? Is the window getting hidden as a result of being reparented? I've determined that the window is 100% of the time created by ::ConsoleInputThreadProcWin32 in windowio.cpp, and that creates it without the owner hwnd.
    • It did, but the plot thickens.
@zadjii-msft commented on GitHub (May 17, 2022): Hmm. It's the combo of 22fb15f0a69f70a1b9115567a41b984273835f74 with #12526 that causes this. That initial show/hide to sync the terminal with the conpty, that's what's busted somehow. Merely commenting those lines fixes this. (see bbae2355c) HOWEVER, it breaks doing a `[Native]::ShowWindow([Native]::GetConsoleWindow(), 0)` (0==`SW_HIDE`) immediately in a fresh window. Trick here: the Terminal starts with `WS_VISIBLE | !WS_MINIMIZE`. The Conpty starts with `!WS_VISIBLE | !WS_MINIMIZE`. Somewhere early in startup, we get a [`WM_SHOWWINDOW(false, 0)`](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-showwindow), and that then bounces with the terminal, because that results in the pty telling the terminal to hide. * ❔ I need to validate that `[Native]::ShowWindow([Native]::GetConsoleWindow(), 0)` did work between 2fb15f and 69b77ca * ✔️ Does removing the `ConptyReparentPseudoConsole` call instead fix this? Is the window getting hidden as a result of being reparented? I've determined that the window is 100% of the time created by `::ConsoleInputThreadProcWin32` in `windowio.cpp`, and that creates it without the owner hwnd. * It did, but the plot thickens.
Author
Owner

@zadjii-msft commented on GitHub (May 17, 2022):

The plot thickens. Remove the call to reparent, but leave the initial state sync in. Things seem to work. HOWEVER, if you minimize the Terminal, then restore it, the terminal doesn't get focus. I bet that the pty window is becoming WS_VISIBLE, which means it's now a valid target to become the foreground window, even though its dimensions are 0x0 and not drawn anywhere. Interesting.

  • try WS_EX_NOACTIVATE
  • try the cloaking thing again.

lmao

However, you can display the system menu by right-clicking or by typing ALT+SPACE.

Yep you can sure do that.

@zadjii-msft commented on GitHub (May 17, 2022): The plot thickens. Remove the call to reparent, but leave the initial state sync in. Things seem to work. HOWEVER, if you minimize the Terminal, then restore it, the terminal doesn't get focus. I _bet_ that the pty window is becoming `WS_VISIBLE`, which means it's now a valid target to become the foreground window, even though its dimensions are 0x0 and not drawn anywhere. Interesting. * [x] ❌ try `WS_EX_NOACTIVATE` * [ ] try the cloaking thing again. lmao > However, you can display the system menu by right-clicking or by typing ALT+SPACE. Yep you can sure do that.
Author
Owner

@zadjii-msft commented on GitHub (May 19, 2022):

This doesn't work with DefTerm until the window is minimized and restored once. :|

@zadjii-msft commented on GitHub (May 19, 2022): > This doesn't work with DefTerm until the window is minimized and restored once. :|
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#17438