Home > Language Reference > Windows Management Functions > GetWindowHandle

GetWindowHandle

The GetWindowHandle function returns the handle of the specified window.

Usage

Used to retrieve a window handle which can then be used in a UseWindowHandle statement.  

Syntax

handle=GetWindowHandle(<window_name>)

Parameters

<window_name>, string, window name of the window to retrieve the handle.

Return value

handle, integer, handle of the specified window. If <window_name> is not found, no error is reported but handle is set to 0. If #UseExact is at 0 (its default value), a partial match is processed if no window with the exact name is found.

See also

GetFocusWindowHandle
GetTopWindowHandle
GetWindowName$
UseWindowHandle

Examples

shell("notepad.exe")
a=GetWindowhandle("NOTEPAD.EXE|Notepad|Untitled")
msgbox(a)
'a returns the handle of the notepad parent window.

shell("notepad.exe")
a=GetWindowhandle("NOTEPAD.EXE|Edit|Untitled")
msgbox(a)
'a returns the handle of the notepad Edit child window

shell("notepad.exe")
a=GetWindowhandle("NOTEPAD.EXE|Notepad|Doc")
msgbox(a)
'a returns the handle of the notepad parent window using partial match.

#UseExact=1
shell("notepad.exe")
a=GetWindowhandle("NOTEPAD.EXE|Notepad|Doc")
msgbox(a)
'a returns 0 as the notepad window title contains Untitled whereas GetWindowhandle's argument contains Doc and system variable #UseExact is set to 1
.


'The script below shows how to use UseWindowHandle instead of UseWindow when window names are not the same when you record and when you replay.
'The example installs Acrobat Reader
'Two functions are defined, one testing if a button has the focus, one testing if a button is on top.
'GetWindowHandle and UseWindowHandle are used (instead of UseWindow) as window names varies from one installation to another.

'Function waits for object objet$ to have focus for Tmax seconds
function wait_focus(objet$,Tmax)
local i, exitr, a$
i=0
exitr=0
repeat
    a$=focus$()
    if instr(a$,objet$)=0 then
        pause 1
        i=i+1
        if i > Tmax then
        msgbox("wait too long for "+objet$)
        stop
        endif
    else
        exitr=1
    endif
until exitr=1
endfunction
'--------------------------------------------
'Function waits for object objet$ to be on top for Tmax seconds
function wait_top(objet$,Tmax)
local i, exitr, a$
i=0
exitr=0
repeat
    a$=top$()
    if instr(a$,objet$)=0 then
        pause 1
        i=i+1
        if i > Tmax then
            msgbox("wait too long for "+objet$)
            stop
        endif
    else
        exitr=1
    endif
until exitr=1
endfunction
'-----------------------------------------------

#ActionTimeout=10

'Start Acrobat setup
Shell("f:\Acrobat\Setup.exe",1)

'License screen - wait until License has the focus
wait_focus("License",10)
'Get the window handle when License is there
a=getwindowhandle(top$())
usewindowhandle(a)
Click(Button,"&Accept")

'Installation choice
wait_focus("Button|Typical",10)
a=getwindowhandle(top$())
usewindowhandle(a)
Click(Button,"&Next >")

'User information window
wait_top("User information",10)
a=getwindowhandle(top$())
usewindowhandle(a)
Click(Button,"&Next >")

'Confirmation window
wait_focus("Button|&Yes",10)
a=getwindowhandle(top$())
usewindowhandle(a)
Click(Button,"&Yes")

'Last validation
wait_focus("Button|&Next",10)
a=getwindowhandle(top$())
usewindowhandle(a)
Click(Button,"&Next >")