Microsoft KB Archive/136308

= Microsoft Knowledge Base =

How to "Click" Bitmap Toolbar "Buttons" in Visual Test
Last reviewed: July 25, 1996

Article ID: Q136308

The information in this article applies to:


 * Microsoft Visual Test for Windows 95, version 4.0

SUMMARY
Some applications have toolbars consisting of bitmaps that look like buttons but are not true buttons. They are bitmaps drawn within a single window. Because they are not true buttons, you can't use the WButtonClk function to trigger their click event. But you can use the Play and WClkWnd functions to click a particular location. This article shows how.

MORE INFORMATION
Windows 95 supports standard toolbar controls that have a class name of ToolBarWindow32, and Visual Test version 4.0 has WToolBarxxxx functions to interact with them. However some applications may not use this standard toolbar control. For example, applications written using MFC (Microsoft Fountation Class) libraries can have toolbars made up of bitmaps drawn to look like buttons in a single Window. This type of control usually has a class name of AfxControlBar. The following code sample demonstrates how you can use Visual Test to interact with these nonstandard toolbar buttons.

The example uses the Windows 95 WordPad program. The width of the bitmap buttons on the toolbar and the spaces contained in the toolbar can be found by using the Screen Utility (click Screen Utility on the Test menu). Using the Screen Utiltiy, you can use the Capture Region option to select any one of the buttons or spaces on the toolbar. Using the Capture Region dialog box, you can determine the width of the button and spaces.

The example program accounts only for a toolbar that consists of equally sized buttons and equally sized spaces. It brings up WordPad.exe and loads a sample .wri file. Then it fires the Find button's click event to bring up a Find dialog box, pauses for five seconds, and then closes the Find dialog box and the WordPad program.

Code Sample
' $include 'declares.inc'

Declare Function ToolButtonclk (ClassName as String, Ordinal as long, _                     s as STRING * 1, BtntoClk as integer)as integer

' The space or button will constitute one element

const MAX_ELEM= 17 ' The number of buttons on the toolbar + spaces

' You can use the Screen Utilty to get at the Width as explained above

const BTN_Width = 22  ' The width of the button in pixels const SP_Width = 7    ' The width of the space in pixels

GLOBAL Elements(MAX_ELEM) as string * 1

' Initialize the space information ' Space or button is counted as an element

Elements(1)="Y" ' This means that there is a space in the first ' position instead of a button

' Spaces occupy the 5, 8, 10, 15, 17 positions as elements on the toolbar

Elements(5)="Y" Elements(8)="Y" Elements(10)="Y" Elements(15)="Y" Elements(17)="Y"

' Start the WordPad Program on Windows 95 - Modify the Path if needed

Run "c:\Program files\accessories\wordpad.exe _ C:\msdev\readvt4.wri",Nowait dim currentwindow as long

' Store the ordinal value of the toolbar window

CurrentWindow&   = WFndWndWaitC("Document - WordPad", "WordPadClass", _                            FW_FOCUS, 10) Dim Ordinalval as long

' For WordPad this value is 4

Ordinalval = 4 ' Use Tools-Winfo to find the ordinal value

' Store the button number that you want to click

Dim BtntoClk as integer Dim ret as integer

' Try to click the 6th Button, which is a Find button ' on WordPad's toolbar

BtntoClk = 6 Ret% = toolbuttonclk("AfxControlBar",Ordinalval,Elements,BtntoClk)

' Close the dialog box after pausing for five seconds

sleep 5 WButtonClick("Cancel")

' Dim currentwindow as long ' Close the Wordpad Program

CurrentWindow& = WFndWndWaitC("Document - WordPad", "WordPadClass", _                                FW_FOCUS, 10) WSysMenu(CurrentWindow&) WMenuSelect("&Close") end

Function ToolButtonclk _

(ClassName as String, Ordinalvalue as long, s as string * 1, _  BtntoClk as integer) as integer

dim wndinfo as info dim ybasecoord as integer dim xbasecoord as integer

' Check if the classname is empty or no button number to click on

if (len(ClassName)=0 or BtntoClk = 0 ) then ToolButtonClk = 0 else dim hwind as long

' Find the Handle of the toolbar window

hwind = WfndWndWaitc(_ord(Ordinalvalue),ClassName,FW_DEFAULT,10)

if hwind > 0 then ' Get information about the toolbar's width, height, and so on        wgetinfo(hwind,wndinfo)

' Find the starting position of the toolbar

ybasecoord = Wndinfo.atop + wndinfo.wheight/2 xbasecoord = Wndinfo.aleft

' Declare varibales needed to track the x coordinate delta

dim button as integer dim spacedelta as integer dim count as integer dim xc as string

Button% = 0 spacedelta% = 0

count% = 1

' Check the array and calculate the value needed to add to base ' coordinate value to click the required button

while (button < BtntoClk )

' If element happens to be the space add only the space width if Elements(count) ="Y" then spacedelta= spacedelta + SP_Width else spacedelta= spacedelta + (BTN_Width ) Button = Button + 1 endif

' Increment count to look at the next array element count = count + 1

endwh

' Adjust the x coordinate value xc$ = str$(xbasecoord + spacedelta-(BTN_Width/2))

' Click the button Play "{CLICK" + xc + "," + str$(ybasecoord)+"}"

endif ToolButtonClk = 1

endif end function