Search code examples
autohotkey

Improved version of an old script does not work as intended


For a long time I have been using this scrip I found on a forum, cant remember who made it, it will simply hide any newly created windows title bar:

#Persistent
#SingleInstance Force

#SingleInstance, force
Gui +LastFound
OnExit("DeregisterShellHook")
DllCall("RegisterShellHookWindow", "ptr", WinExist())
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), Func("RegisterWindowMessage"))
Return

RegisterWindowMessage(wParam, hwnd)
{
    ; 1 means a new window gets created
    If (wParam = 1)
        ; WinSet, Style, -0xC00000, ahk_id %hwnd%       ;Small crop effect  
        ; WinSet, Style, -0x800000, ahk_id %hwnd%       ;Midium crop effect 
        WinSet, Style, -0xc40000, ahk_id %hwnd%     ;Greatest crop effect
}

DeregisterShellHook()
{
    DllCall("DeregisterShellHookWindow", "ptr", WinExist())
}

The problem I have been having with it is that, it will do this for all windows, I have two programs, that if their title bar is removed, they will break.

If I simply want to the script to exclude these two programs, do nothing if the window belongs to one of the programs, Some time ago I sought help and came up with this:

#Persistent
#SingleInstance Force

DllCall("RegisterShellHookWindow", "UInt",A_ScriptHwnd)
OnMessage(0xC028, "ShellEvent") ; SHELLHOOK

return ; End of auto-execute

ShellEvent(wParam, lParam) {
    if (wParam != 1) ; HSHELL_WINDOWCREATED
        return
    WinGet exe, ProcessName, % "ahk_id" lParam
    if (exe = "ahk_exe Notepad.exe")
        return
    WinSet Style, -0xC40000, % "ahk_id" lParam
}

But when I am running script, nothing happens to new windows. I tried running it both elevated and unelevated, it just does not work.

I would be greatly appreciate, if some could tell me what's wrong with the second script or just show me an example of first script, of how I can exclude a window belonging to ahk_exe Notepad.exe

Thank you.


EDIT 1:

@ user3419297

#Persistent
#SingleInstance Force

DllCall("RegisterShellHookWindow", "UInt",A_ScriptHwnd)
OnMessage(0xC028, "ShellEvent") ; SHELLHOOK

return ; End of auto-execute

ShellEvent(wParam, lParam) {
    if (wParam != 1) ; HSHELL_WINDOWCREATED
        return
    WinGet exe, ProcessName, % "ahk_id" lParam
        msgbox  %exe%
    if (exe = "Notepad.exe")
        ; msgbox  %exe%
        return
    WinSet Style, -0xC40000, % "ahk_id" lParam
}
    ; msgbox  % "ahk_id" lParam


Solution

  • Try

    ; How to] Hook on to Shell to receive its messages? 
    ; https://www.autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488
    
    #SingleInstance Force
    
    Gui +LastFound
    hWnd := WinExist()
    DllCall( "RegisterShellHookWindow", UInt,hWnd )
    MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
    OnMessage( MsgNum, "ShellMessage" )
    Return ;  // End of Auto-Execute Section //
    
    ShellMessage( wParam,lParam ) {
        ; Execute a command based on wParam and lParam
        WinGet, pname, ProcessName, % "ahk_id" lParam
        If ( wParam = 1 ) ;  HSHELL_WINDOWCREATED
        {
            msgbox  %pname%
            if (pname = "Notepad.exe")
                return
            WinSet Style, -0xC40000, % "ahk_id" lParam
        }
    }