Sign in to follow this  
Followers 0
OldCoder

IsVisible()

1 post in this topic

#1 ·  Posted (edited)

Just something I've needed on various occasions. Cheers. :)

'IsVisible()'

#cs
    IsVisible(WinHandle [, Offset [,Control]])

    Check if the middle and all four corners of a window or control are currently visible.

        $wiv_hWnd = The handle to the window to check, (if blank, the active window will be used).

        $wiv_offx = [optional] How far inside the window/control you want to check. 5 is a good value for windows since some corners of windows are rounded, especially in the titlebar. More may be needed.

        $wiv_ctrl = [optional] The control you want to check.  Blank if entire window.
    
    Returns:
        = 1 if window/control is visible
        =-1 if the window does not exist
        =-2 if the window is hidden
        =-3 if the window is minimized
        = 0 or Window Handle/Control Text of the window/control covering the defined window/control
#ce

Func IsVisible($wiv_hWnd="", $wiv_off=5, $wiv_ctrl="")
    If NOT IsHWnd($wiv_hWnd) Then $wiv_hWnd=WinGetHandle(WinActive(""))
    Local $wiv_state=WinGetState($wiv_hWnd)
    If NOT BitAND($wiv_state, 1) Then Return -1
    If NOT BitAND($wiv_state, 2) Then Return -2 
    If BitAND($wiv_state, 16) Then Return -3
    If $wiv_ctrl="" Then
        Local $wiv_pos=WinGetPos($wiv_hWnd)
    Else
        Local $wiv_pos=ControlGetGlobalPos($wiv_hWnd, $wiv_ctrl)
    EndIf
    Local $left=$wiv_pos[0]+$wiv_off, $rite=($wiv_pos[0]+$wiv_off)+($wiv_pos[2]-($wiv_off*2)), $top=$wiv_pos[1]+$wiv_off, $botm=($wiv_pos[1]+$wiv_off)+($wiv_pos[3]-($wiv_off*2)), $cenx=$left+Int($wiv_pos[2]/2), $ceny=$top+Int($wiv_pos[3]/2), $lt, $lb, $rt, $rb, $xy
    $xy=WinFromPoint($cenx, $ceny)
    If $xy<>$wiv_hWnd AND ChildGetParent($xy)<>$wiv_hWnd Then Return $xy
    $lt=WinFromPoint($left, $top)
    If $lt<>$wiv_hWnd AND ChildGetParent($lt)<>$wiv_hWnd Then Return $lt
    $rb=WinFromPoint($rite, $botm)
    If $rb<>$wiv_hWnd AND ChildGetParent($rb)<>$wiv_hWnd Then Return $rb
    $rt=WinFromPoint($rite, $top)
    If $rt<>$wiv_hWnd AND ChildGetParent($rt)<>$wiv_hWnd Then Return $rt
    $lb=WinFromPoint($left,$botm)
    If $lb<>$wiv_hWnd AND ChildGetParent($lb)<>$wiv_hWnd Then Return $lb
    Return 1
EndFunc


Func WinFromPoint($wfp_x, $wfp_y)
    Local $wfp_pt=DllStructCreate("int X;int Y"),$wfp_1=DllStructSetData($wfp_pt,"x",$wfp_x),$wfp_2=DllStructSetData($wfp_pt,"y",$wfp_y),$wfp_ptr=DllStructCreate("int64",DllStructGetPtr($wfp_pt)),$wfp_ret=DllCall("user32.dll","hwnd","WindowFromPoint","int64",DllStructGetData($wfp_ptr,1))
    Return $wfp_ret[0]
EndFunc


Func ChildGetParent($child_hWnd)
    Local $child_h=$child_hWnd, $child_ret
    If NOT IsHWnd($child_hWnd) Then $child_h=GUICtrlGetHandle($child_hWnd)
    If NOT IsHWnd($child_h) Then $child_h=WinGetHandle($child_hWnd)
    $child_ret=DllCall("user32.dll", "hwnd", "GetParent", "hwnd", $child_hWnd)
    Return $child_ret[0]
EndFunc


Func ControlGetGlobalPos($cts_hWnd, $cts_ctrl)
    If NOT IsHWnd($cts_hWnd) Then $cts_hWnd=WinGetHandle($cts_hWnd)
    If NOT IsHWnd($cts_ctrl) Then $cts_ctrl=GuiCtrlGetHandle($cts_ctrl)
    Local $cts_tmp=ControlGetPos($cts_hWnd,"",$cts_ctrl), $cts_tp=DllStructCreate("int X;int Y"), $cts_ret, $cts_1=DllStructSetData($cts_tp, 1, $cts_tmp[0]), $cts_2=DllStructSetData($cts_tp, 2, $cts_tmp[1]), $cts_3=DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $cts_hWnd, "ptr", DllStructGetPtr($cts_tp))
    Return StringSplit(DllStructGetData($cts_tp, 1)&"|"&DllStructGetData($cts_tp, 2)&"|"&$cts_tmp[2]&"|"&$cts_tmp[3], "|", 2)
EndFunc
Edited by OldCoder

"Intelligence is the ability to adapt to change."                                      - Stephen Hawking                                        "...not the ability to exploit others."                                                  - OldCoder

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0