Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble disabling SC_CLOSE for terminal's window #15702

Closed
bzm3r opened this issue Jul 13, 2023 · 5 comments
Closed

Trouble disabling SC_CLOSE for terminal's window #15702

bzm3r opened this issue Jul 13, 2023 · 5 comments
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Resolution-External For issues that are outside this codebase

Comments

@bzm3r
Copy link

bzm3r commented Jul 13, 2023

Windows Terminal version

1.17.11461.0

Windows build number

10.0.19044.0

Other Software

PowerShell 7+

Steps to reproduce

I would like to disable the close (from the ALT+Space floating menu and the top-left window's "X") button for the terminal that my PowerShell session is loaded in. So, I run the following function (based on this SO answer and this blog post):

function Disable-ThisTerminalClose {
    #Calling user32.dll methods for Windows and Menus
    $MethodsCall = '
[DllImport("user32.dll")] public static extern long GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")] public static extern bool EnableMenuItem(long hMenuItem, long wIDEnableItem, long wEnable);
[DllImport("user32.dll")] public static extern long SetWindowLongPtr(long hWnd, long nIndex, long dwNewLong);
[DllImport("user32.dll")] public static extern bool EnableWindow(long hWnd, int bEnable);
'
 
    #Create a new namespace for the Methods to be able to call them
    Add-Type -MemberDefinition $MethodsCall -name NativeMethods -namespace Win32
 
    Write-Host "Added NativeMethods type..."

    #WM_SYSCOMMAND Message
    $MF_GRAYED = 0x00000001L    #Indicates that the menu item is disabled and grayed so that it cannot be selected.    
    $MF_DISABLED = 0x00000002L #Indicates that the menu item is disabled, but not grayed, so it cannot be selected.
    $MF_ENABLED = 0x00000000L #Indicates that the menu item is enabled and restored from a grayed state so that it can be selected.
    #... http://msdn.microsoft.com/en-us/library/windows/desktop/ms647636(v=vs.85).aspx
 
    $SC_CLOSE = 0xF060
    #... http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx

 
    #Get window handle of this Powershell process (should be the Windows Terminal?)
    $PSWindow = Get-Process | Where-Object { $_.Name -like "*Terminal*" }
    Write-Host ("Found PSWindow: " + $PSWindow)
    $hwnd = $PSWindow.MainWindowHandle
    Write-Host ("hwnd: " + $hwnd)
 
    #Get System menu of windows handled
    $hMenu = [Win32.NativeMethods]::GetSystemMenu($hwnd, 0)
 
    # #Window Style : TOOLWINDOW
    # [Win32.NativeMethods]::SetWindowLongPtr($hwnd, $GWL_EXSTYLE, $WS_EX_TOOLWINDOW) | Out-Null
 
    #Disable X Button and window itself
    Write-Host "About to disable close functionality..."
    [Win32.NativeMethods]::EnableMenuItem($hMenu, $SC_CLOSE, ($MF_GRAYED + $MF_DISABLED))
 
    Write-Host "Disabled!" -ForegroundColor Red
}

Run this function (or, paste it's body into a PowerShell 7+ session.

Expected Behavior

I was hoping that the close button (both X and in floating menu) would be disabled.

Actual Behavior

Close/X are not disabled.

@bzm3r bzm3r added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Jul 13, 2023
@lhecker
Copy link
Member

lhecker commented Jul 13, 2023

Why would you like to disable the close button?

@zadjii-msft
Copy link
Member

zadjii-msft commented Jul 13, 2023

Yea, messing with Terminal's HWND is 100% not supported.

Focus mode might be something that interests you, though, that'll entirely hide the titlebar (including the tabs).

We could theoretically make this a window theme setting. You can already hide the close button on the tabs, so there's probably room for configuring the close button too. Something ala: #12632 (comment)

@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jul 13, 2023
@bzm3r
Copy link
Author

bzm3r commented Jul 13, 2023

Why would you like to disable the close button?

I need to trigger some cleanup scripts on exit, but PowerShell.Exiting still does not trigger correctly for when the user presses the X button. At this point, the issue has been open for so long, that I am not sure what is going on there: PowerShell/PowerShell#8000

So, I thought I'd just try to disable the X button.

We could theoretically make this a window theme setting. You can already hide the close button on the tabs, so there's probably room for configuring the close button too. Something ala: #12632 (comment)

@zadjii-msft That would be super helpful, actually. I realize that it wouldn't disable Close in the alt+Space menu, but that would still be a major improvement. Of course, the best solution is if PowerShell.Exiting just finally worked correctly...

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jul 13, 2023
@zadjii-msft
Copy link
Member

Hokay, so we talked a bit with @SteveL-MSFT about this. He's gonna take another shot at PowerShell/PowerShell#8000. Because honestly, that's the right solution here. Just fix that. Not disable the close button for the Terminal. That seems like it's an unnecessarily big hammer for a weird edge case in the shell scripting engine 😅

/dup PowerShell/PowerShell#8000

@microsoft-github-policy-service
Copy link
Contributor

Hi! We've identified this issue as a duplicate of one that exists on somebody else's Issue Tracker. Please make sure you subscribe to the referenced external issue for future updates. Thanks for your report!

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-External For issues that are outside this codebase and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Attention The core contributors need to come back around and look at this ASAP. labels Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Resolution-External For issues that are outside this codebase
Projects
None yet
Development

No branches or pull requests

3 participants