Jump to content

function to find if window is visible

Recommended Posts


i sometimes have to deal with multimonitor setups. I searched the forums and found quite a lot of threads about this. Most fo them seem to complicated for what i have in mind.

When i start a program and drag it to a different monitor, and the close the program, i save the x/y coordinates of the window to start it were it was closed.

this works fine as long as i start it on the same PC setup. But when suddenly i use a pc with only one monitor i will end up with a window placed outside the visible area.

Is there an easy way to detect that? If yes, i would simply move the window to the center of the screen if it is not visible.

I tried windowgetstate, but it seems it can only detect if the state is set to hide and not if the window is off screen...

Ideas are welcome:-)



Link to comment
Share on other sites

  • Moderators


This thread should allow you to determine the setup with which you are working - then use WinGetPos to get the position of the GUI and then check to see if it is within the available bounds.


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Link to comment
Share on other sites

The script allows you to retrieve the desktop size.

Can you try with this function : just give the window title in the first parameter :

; Returns 0 = Not visible
; Returns 1 = Fully viewable
; Returns 2 = Partially viewable
; @error = Window does not exist
Func _WinIsOnScreen($vTitle, $sText = "")
    Local $iDesktopLeft, $iDesktopTop, $iDesktopRight, $iDesktopBottom
    Local $aDesktopPos[4], $i = 0
    Local $hWnd = WinGetHandle($vTitle, $sText)
    If @error Then Return SetError(@error, 0, 0)
    If BitAND( WinGetState($hWnd), 32) Then Return 1 ; Window is maximized

    Local $aPos = WinGetPos($hWnd)
    If @error Then Return SetError(@error, 0, 0)

    Local $iWinLeft = $aPos[0]
    Local $iWinTop = $aPos[1]
    Local $iWinRight = $iWinLeft + $aPos[2]
    Local $iWinBottom = $iWinTop + $aPos[3]

    Local $iNbMonitors = _WinAPI_GetSystemMetrics(80)
    If $iNbMonitors = 1 Then
        $iDesktopLeft = 0
        $iDesktopTop = 0
        $iDesktopRight = _WinAPI_GetSystemMetrics(0)
        $iDesktopBottom = _WinAPI_GetSystemMetrics(1)
        $iDesktopLeft = _WinAPI_GetSystemMetrics (76)
        $iDesktopTop = _WinAPI_GetSystemMetrics (77)
        $iDesktopRight = $iDesktopLeft + _WinAPI_GetSystemMetrics (78)
        $iDesktopBottom = $iDesktopTop + _WinAPI_GetSystemMetrics (79)
    If $iWinLeft < $iDesktopLeft Or $iWinTop < $iDesktopTop Or $iWinRight > $iDesktopRight Or $iWinBottom > $iDesktopBottom Then
        If $iWinRight < $iDesktopLeft Or $iWinBottom < $iDesktopTop Or $iWinLeft > $iDesktopRight Or $iWinTop > $iDesktopBottom Then Return 0
        Return 2
    Return 1
EndFunc   ;==> _WinIsOnScreen
Edited by jguinch
Link to comment
Share on other sites

can you explain this line?

If BitAND(WinGetState($hWnd), @SW_MAXIMIZE) Then Return 1

is it normal that a minimized window is visible reported as visible? Its no problem, i just have to know:-)

Edit: the function does not work.When i move the window outside the screen

WinMove("mywindow", "", 2000, 2000, Default, Default, 1)

it is still reported as visible

Edited by Allow2010
Link to comment
Share on other sites

Sorry, I didn't try with a single monitor. I edited my previous post. Can you try again ?

The line BitAND(WinGetState($hWnd), @SW_MAXIMIZE) was wrong, it's replaced by a correct line witch test of the window is maximized (returns 1 if it is)

A minimized window is not visible, so the function just return 0.

Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...