Search code examples
autohotkey

is there a way to determine if a window is a 'conventional' window, rather than a say control?


Recently I was asking around how to detect when a new window is created, and a user on Reddit (plankoe) gave me a solution I was very happy with. I have been using their solution for a few days now, and it has just one slight issue with it.

onNewWin(){                                  ;the callback function             
    WinSet, Style, -0xC40000, % "ahk_id" hwnd       ;removes the title bar/ caption on any newly created window
}

The call back onNewWin will apply WinSet to any window, regardless if its a control, a right click context menu, or windows 11 HUD windows, like the volume controls window. This is leading to broken functionality.

I would like to only apply the style to windows that have the standard windows title bar/caption (ones with minus, square and X, or just an X), but identifying this kind of window is proving to be difficult. I have tried using the winGet command to figure a common style between these kinds of windows, but every window returns a different style number. I also looked at the style table and found nothing applicable.

I should be clear, the call back stuff I mentioned above is to just give context for what I am trying to do, and my sole concern is trying to identify windows that have a title bar/caption (ones with minus, square and X, or just an X), so the problem can be reduced to:

x::
    if (<standard/conventional widnow>)                ;<----- figuring out this 
        WinSet, Style, -0xC40000, % "ahk_id" hwnd       ;remove the title bar/ caption
    return

Solution

  • ; Press F1  to apply that style 
    ; to any standard/conventional widnow under the mouse pointer:
    
    F1::
        MouseGetPos,,, WinUnderMouse
        If NOT IsWindow(WinExist("ahk_id" . WinUnderMouse))
            return
        WinSet, Style, -0xC40000, % "ahk_id" WinUnderMouse 
    return
    
    ;------------------------------------------------------------------------
    ; Check whether the target window is a standard/conventional widnow
    ;------------------------------------------------------------------------
    ;;; https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
    IsWindow(hWnd){
        WinGet, dwStyle, Style, ahk_id %hWnd%
        if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {    ;; Window with a style that doesn't activate (WS_EX_NOACTIVATE 0x08000000L), or not visible (WS_VISIBLE 0x10000000 )
        WinGetClass, szClass, ahk_id %hWnd% ; this is an exception for TApplication Classes
        if (szClass != "TApplication")
            return false
        }
        WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
        if (dwExStyle & 0x00000080) {   ; The window is a floating toolbar (WS_EX_TOOLWINDOW 0x00000080)
            return false
        }
        WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
        if (dwExStyle & 0x00000008) {   ; to exclude Always-On-top windows (WS_EX_TOPMOST 0x00000008)
            return false
        }
        WinGetClass, szClass, ahk_id %hWnd% ; this is an exception for TApplication Classes
        if (szClass = "TApplication") {
            return false
        }
        if IsWindowCloaked(hwnd){   ; exclude "Cloaked" windows
            return false
        }
        return true
    }
    
    ; Cloaked windows 
    IsWindowCloaked(hwnd) {
        return DllCall("dwmapi\DwmGetWindowAttribute", "ptr", hwnd, "int", 14, "int*", cloaked, "int", 4) >= 0
            && cloaked
    return
    }
    

    You can remove or add more exceptions (styles or classes) in the above function.