As described in the What is the PowerBASIC COM Browser topic, the PowerBASIC COM Browser is a browser utility application that exposes the Interfaces, Methods, and Properties in a type-library. It is also used to generate PowerBASIC compatible source code to be used in your application.
We will walk through and example of using the PowerBASIC COM Browser to locate a registered type library, generate the PowerBASIC compatible source code, and then use this source code in a PowerBASIC For Windows application.
Start the PowerBASIC COM Browser
Locate the Microsoft Agent Control 2.0 type library. This will be listed under the "Registered Library" heading with the text of "Microsoft Agent Control 2.0" or under the "Filename" heading of "agentctl.dll". If you do not have this type library installed it can be downloaded for free from http://www.microsoft.com/msagent/downloads/user.aspx. After installing the Microsoft Agent Control 2.0 type library click the Reload button to update the list of registered type libraries.
Double-click on the Microsoft Agent Control 2.0 type library listed in the list of Registered type libraries, to generate the PowerBASIC compatible source code for this object.
Click the "Save As..." button and save it with the name of "agent.inc"
Close the PowerBASIC COM Browser
Start the PowerBASIC For Windows 9 IDE
Click the Create New File button in the IDE
Paste the following code into the new file created in the IDE
#COMPILE EXE #DIM ALL
%ID_START = 1000 %ID_STOP = 1001 %ID_EVENTLIST = 1003
GLOBAL hDlg AS LONG
#INCLUDE "agent.inc" #INCLUDE "Win32api.inc"
' Display an error message MACRO DisplayError(txt) IF ISTRUE(ISOBJECT(AgentEvents)) THEN ' Detach the events handler EVENTS END AgentEvents END IF
' Print the error and then exit the callback routine MSGBOX txt, %MB_OK OR %MB_ICONERROR, "MS Agent Error" EXIT FUNCTION END MACRO
CALLBACK FUNCTION DlgProc STATIC AgentCtrlEx AS IAgentCtlEx STATIC AgentEvents AS Int__AgentEvents LOCAL StartX AS LONG LOCAL StartY AS LONG LOCAL CharW AS LONG LOCAL CharH AS LONG
SELECT CASE AS LONG CBMSG CASE %WM_INITDIALOG ' Create the Agent Control Object AgentCtrlEx = newcom $PROGID_Agent2 IF ISFALSE(ISOBJECT(AgentCtrlEx)) THEN DisplayError("The Microsoft Agent Control 2.0 is not installed. This control can be " + _ "downloaded from http://www.microsoft.com/msagent/downloads/user.aspx") END IF
' Create the Events handler interface AgentEvents = CLASS "Class_Int__AgentEvents" IF ISFALSE(ISOBJECT(AgentEvents)) THEN DisplayError("Error creating the event interface.") END IF
' Attach the Events handler interface to the Agent Control Events FROM AgentCtrlEx CALL AgentEvents
'Enable the Start button CONTROL ENABLE CBHNDL, %ID_START
CASE %WM_COMMAND SELECT CASE AS LONG CBCTL CASE %ID_START IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN ' Load the Merlin agent AgentCtrlEx.Characters.Load("Merlin", "Merlin.acs") IF OBJRESULT <> %S_OK THEN DisplayError("The Microsoft Agent Control 2.0 Merlin Character is not installed. This character " + _ "can be downloaded from http://www.microsoft.com/msagent/downloads/user.aspx") END IF
' Show the Merlin agent on the screen AgentCtrlEx.Characters.Character("Merlin").Show
' Get the Width and Height of the Merlin agent CharW = AgentCtrlEx.Characters.Character("Merlin").Width CharH = AgentCtrlEx.Characters.Character("Merlin").Height
' Get the Width and Height of the Desktop DESKTOP GET CLIENT TO StartX, StartY
' Find the center of the desktop for Merlin agent StartX = (StartX - CharW)\2 StartY = (StartY - CharH)\2
' Move the Merlin agent to the center of the desktop AgentCtrlEx.Characters.Character("Merlin").MoveTo(StartX, StartY)
' Have the Merlin agent play the trumpet AgentCtrlEx.Characters.Character("Merlin").Play(UCODE$("Announce"))
' Make the Merlin agent speak AgentCtrlEx.Characters.Character("Merlin").Speak("With \map="+$DQ+"PowerBAYSIK"+$DQ+"="+$DQ+"PowerBASIC"+$DQ+"\ \Pau=300\ you can be a wizard too!")
' Disable the Start button and enable the Stop button CONTROL DISABLE CBHNDL, %ID_START CONTROL ENABLE CBHNDL, %ID_STOP END IF
CASE %ID_STOP IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN ' Stop all actions by the Merlin agent and unload it AgentCtrlEx.Characters.Character("Merlin").STOP
AgentCtrlEx.Characters.Unload("Merlin") ' Enable the Start button and disable the Stop button CONTROL ENABLE CBHNDL, %ID_START CONTROL DISABLE CBHNDL, %ID_STOP END IF
END SELECT
CASE %WM_DESTROY IF ISTRUE(ISOBJECT(AgentEvents)) THEN ' Detach the event handler interface Events END AgentEvents END IF END SELECT END FUNCTION
FUNCTION PBMAIN () AS LONG DIALOG NEW 0, "COM Browser Tutorial", 201, 122, 198, 115, %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR _ %WS_SYSMENU OR %WS_MINIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR _ %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _ %WS_EX_RIGHTSCROLLBAR, TO hDlg CONTROL ADD BUTTON, hDlg, %ID_START, "Start Agent", 5, 5, 50, 15, %WS_CHILD OR %WS_VISIBLE OR %WS_DISABLED OR _ %WS_TABSTOP OR %BS_TEXT OR %BS_PUSHBUTTON OR %BS_CENTER OR %BS_VCENTER, %WS_EX_LEFT OR %WS_EX_LTRREADING CONTROL ADD BUTTON, hDlg, %ID_STOP, "Stop Agent", 5, 25, 50, 15, %WS_CHILD OR %WS_VISIBLE OR %WS_DISABLED OR _ %WS_TABSTOP OR %BS_TEXT OR %BS_PUSHBUTTON OR %BS_CENTER OR %BS_VCENTER, %WS_EX_LEFT OR %WS_EX_LTRREADING CONTROL ADD LISTBOX, hDlg, %ID_EVENTLIST, , 70, 0, 125, 110, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %WS_VSCROLL, _ %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
DIALOG SHOW MODAL hDlg, CALL DlgProc END FUNCTION
Click the Save All button and save this file as "agent.bas" in the same directory that you save "agent.inc" to in step #4
Open the "agent.inc" file in the IDE
Search (CTRL+F) in the IDE for the text of "IAgentCtl event interface" (without the quotes). The methods of this interface are called when an event occurs in the Microsoft Agent Control. We will add code to these methods that will display the event that occurred in the dialogs listbox. Make the Int__AgentEvents, look like the following:
CLASS Class_Int__AgentEvents $CLSID_Event__AgentEvents AS EVENT INTERFACE Int__AgentEvents $IID_Int__AgentEvents INHERIT IDISPATCH
METHOD ActivateInput <1> (BYREF CharacterID AS STRING) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Input Activated" END METHOD
METHOD DeactivateInput <3> (BYREF CharacterID AS STRING) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Input Deactivated" END METHOD
METHOD Click <2> (BYVAL CharacterID AS STRING, BYVAL BUTTON AS INTEGER, BYVAL Param_Shift AS INTEGER, BYVAL x AS INTEGER, BYVAL y AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Click at ("+FORMAT$(x)+","+FORMAT$(y)+")" END METHOD
METHOD DblClick <4> (BYVAL CharacterID AS STRING, BYVAL BUTTON AS INTEGER, BYVAL Param_Shift AS INTEGER, BYVAL x AS INTEGER, BYVAL y AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Double Click at ("+FORMAT$(x)+","+FORMAT$(y)+")" END METHOD
METHOD DragStart <5> (BYVAL CharacterID AS STRING, BYVAL BUTTON AS INTEGER, BYVAL Param_Shift AS INTEGER, BYVAL x AS INTEGER, BYVAL y AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Drag Start at ("+FORMAT$(x)+","+FORMAT$(y)+")" END METHOD
METHOD DragComplete <6> (BYVAL CharacterID AS STRING, BYVAL BUTTON AS INTEGER, BYVAL Param_Shift AS INTEGER, BYVAL x AS INTEGER, BYVAL y AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Drag Complete to ("+FORMAT$(x)+","+FORMAT$(y)+")" END METHOD
METHOD SHOW <15> (BYVAL CharacterID AS STRING, BYVAL Cause AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Character is showing" END METHOD
METHOD Hide <7> (BYVAL CharacterID AS STRING, BYVAL Cause AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Character is hidden" END METHOD
METHOD RequestStart <9> (BYVAL Request AS IDISPATCH) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Request Start" END METHOD
METHOD RequestComplete <11> (BYVAL Request AS IDISPATCH) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Request Complete" END METHOD
METHOD Restart <21> () ' Insert your code here END METHOD
METHOD Shutdown <12> () ' Insert your code here END METHOD
METHOD Bookmark <16> (BYVAL BookmarkID AS LONG) ' Insert your code here END METHOD
METHOD Command <17> (BYVAL UserInput AS IDISPATCH) ' Insert your code here END METHOD
METHOD IdleStart <19> (BYVAL CharacterID AS STRING) ' Insert your code here END METHOD
METHOD IdleComplete <20> (BYVAL CharacterID AS STRING) ' Insert your code here END METHOD
METHOD Move <22> (BYVAL CharacterID AS STRING, BYVAL x AS INTEGER, BYVAL y AS INTEGER, BYVAL Cause AS INTEGER) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Move to ("+FORMAT$(x)+","+FORMAT$(y)+")" END METHOD
METHOD SIZE <23> (BYVAL CharacterID AS STRING, BYVAL Param_Width AS INTEGER, BYVAL Height AS INTEGER) ' Insert your code here END METHOD
METHOD BalloonShow <24> (BYVAL CharacterID AS STRING) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Showing balloon text" END METHOD
METHOD BalloonHide <25> (BYVAL CharacterID AS STRING) LISTBOX INSERT hDlg, %ID_EVENTLIST, 1, "Hiding balloon text" END METHOD
METHOD HelpComplete <26> (BYVAL CharacterID AS STRING, BYVAL Param_Name AS STRING, BYVAL Cause AS INTEGER) ' Insert your code here END METHOD
METHOD ListenStart <27> (BYVAL CharacterID AS STRING) ' Insert your code here END METHOD
METHOD ListenComplete <28> (BYVAL CharacterID AS STRING, BYVAL Cause AS INTEGER) ' Insert your code here END METHOD
METHOD DefaultCharacterChange <30> (BYREF Param_GUID AS STRING) ' Insert your code here END METHOD
METHOD AgentPropertyChange <31> () ' Insert your code here END METHOD
METHOD ActiveClientChange <32> (BYVAL CharacterID AS STRING, BYVAL Active AS INTEGER) ' Insert your code here END METHOD
END INTERFACE END CLASS
In the IDE, click the compile and run button. The application will be displayed as
Click the "Start Agent" button and the
Merlin character will display in the top left corner of the screen then
move to the center of the desktop and play a trumpet then speak. If you
wish to hear the text shown in the balloon when Merlin is speaking, you
will need to download and install the free SAPI 4.0 and a Text to Speech
Engine from http://www.microsoft.com/msagent/downloads/user.aspx.
You can click, double-click, drag and drop, hide
(right-click on Merlin and select Hide), or show (right-click on Merlin
in the systems tray and select Show) and see these events listed in the
listview control on the dialog box.
Click the Stop Agent button to stop and unload the Merlin character.
See Also
What is the PowerBASIC COM Browser
The PowerBASIC COM Browser User Interface