Trouble disabling SC_CLOSE for terminal's window #20225

Open
opened 2026-01-31 07:07:22 +00:00 by claunia · 0 comments
Owner

Originally created by @bzm3r on GitHub (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.

Originally created by @bzm3r on GitHub (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](https://stackoverflow.com/questions/73746912/disable-the-close-x-button-in-powershell) and [this blog post](https://www.makak.ch/disable-close-button-in-powershell-window/)): ```powershell 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.
claunia added the Issue-BugResolution-External labels 2026-01-31 07:07:22 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20225