GetWindowHandle

 

Windows management function.

The GetWindowHandle function returns the handle of specified window ; this handle can then be used in Windows API functions or with UseWindowHandle (gives title independence).

Syntax

handle=GetWindowHandle(<window_name> [,<instance>])

Parameters

<window_name>, string. window name of the window we want to find the handle. You can use the SPY tool to manually determine the caption of a window or use Recording mode.

<instance>, optional parameter, instance number of the window we want to find the handle.

Return value

handle : integer, handle of the window <window_name>. If window <window_name> is not found, there are no errors but handle is set to 0. If #UseExact is at 0 (its default value), a window with nearly the same name will return the handle of that window.

See also

#UseExact, UseWindowHandle, GetWindowName$

Examples

 

shell("wordpad.exe")

a=GetWindowhandle("WORDPAD.EXE|RichEdit20A|Document - WordPad|1")

msgbox(a)

a returns WordPad window's handle.

 

shell("wordpad.exe")

a=GetWindowhandle("WORDPAD.EXE|RichEdit20A|Doc - WordPad|1")

msgbox(a)

a returns WordPad window's handle as a window name with nearly the same name is found (Document instead of Doc).

 

#UseExact=1

shell("wordpad.exe")

a=GetWindowhandle("WORDPAD.EXE|RichEdit20A|Doc - WordPad|1")

msgbox(a)

a returns 0 as the WordPad window contains Document whereas GetWindowhandle's argument contains Doc and system variable #UseExact is set to 1.

Example code

The script below installs Acrobat ; two functions are defined for testing if the button we want to click is ready for being clicked ; 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 from CDROM

Shell("f:\Acrobat4\Setup.exe",1)

 

'License window

wait_focus("License",10)

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 >")