Search code examples
windowsautohotkey

How to determine whether a Window is visible on the screen with AHK?


I am trying to run this script from here https://autohotkey.com/board/topic/25619-detect-on-screen/, but it does not work if the Window top border is not touching the screen top:

^F2::
If ( ID := WinExist("Calculator") ) 
 {
   WinGetPos , X, Y, , , ahk_id %ID%
   If  ( DllCall("WindowFromPoint", Int,X+5, Int,Y+5 ) = ID ) 
       MsgBox, Calculator is ON SCREEN
 }
Return

Solution

  • use det_winIsVisible := WinGetStyle("ahk_id " . hwnd) & WS_VISIBLE

    WS_VISIBLE                := 0x10000000
    
    ; Is a Window Visible on Screen - AutoHotkey Community
    ; https://www.autohotkey.com/boards/viewtopic.php?t=107797
    ;
    ; WinGetStyle / WinGetExStyle - Syntax & Usage | AutoHotkey v2
    ; https://www.autohotkey.com/docs/v2/lib/WinGetStyle.htm
    /**
     * 
     * @param hwnd - The window handle.
     * @returns {Boolean|""} - true if visible, false if hidden, null if non-existent.
     */
    detm_winIsVisible(hwnd) {
      DetectHiddenWindows_prev := DetectHiddenWindows(true)
      try {
        det_winIsVisible := WinGetStyle("ahk_id " . hwnd) & WS_VISIBLE
        return det_winIsVisible
      } catch TargetError as e {
        return ""
      } finally {
        DetectHiddenWindows(DetectHiddenWindows_prev)
      }
    }
    
    • ahk v2
    • not the best, maybe Dll can be better
    • basing on question title, may not fit op's specific case

    WinGetMinMax - Syntax & Usage | AutoHotkey v2
    https://www.autohotkey.com/docs/v2/lib/WinGetMinMax.htm
    Returns a non-zero number if the specified window is maximized or minimized.