Part 6- Mouse clicks

Part 6 - Activating an agent with a mouse click.

Start a new project, place an agent control on the form then add one drop down lists and two command buttons to the form, you may also want to add the Agents label but it is not necessary. Name the dropdown list cmbAgent and set it's Style property to Dropdown List (2). Double click on the form to replace it's content with the following code.

Option Explicit
Dim miFormLeft%, miFormTop%
Dim msLocalPath$
Dim msAgentName$
Dim theAgent As IAgentCtlCharacter
Dim reqHB

Private Sub Form_Load()
    On Error Resume Next
    CenterForm
    LoadAgent "merlin"
    FillAgentsList
End Sub

Sub CenterForm()
    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    miFormLeft = Me.Left \ Screen.TwipsPerPixelX
    miFormTop = Me.Top \ Screen.TwipsPerPixelY
    Show
End Sub

Sub LoadAgent(sAgentName)
    msLocalPath = "c:\program files\microsoft agent\characters\"
    msAgentName = sAgentName
    Agent1.Characters.Load sAgentName, msLocalPath & sAgentName & ".acs"
    If Err = 0 Then
        Set theAgent = Agent1.Characters(sAgentName)
        Dim x%, y%
        Randomize
        x = Int((Screen.Width \ Screen.TwipsPerPixelX - theAgent.Width) * Rnd)
        y = Int((Screen.Height \ Screen.TwipsPerPixelY - theAgent.Height) * Rnd)
        theAgent.MoveTo x, y
        theAgent.Show
    Else
        MsgBox "Cannot load " & sAgentName
        End
    End If
End Sub

Sub FillAgentsList()
    On Error Resume Next
    cmbAgents.AddItem "genie"
    cmbAgents.AddItem "robby"
    Set reqHB = theAgent.Speak("Select another agent from the dropdown list.")
End Sub

Private Sub cmbAgents_Click()
    On Error Resume Next
    LoadAgent (cmbAgents.Text)
End Sub

Private Sub Agent1_RequestComplete(ByVal Request As Object)
    Select Case Request
        Case reqHB
            theAgent.Balloon.Visible = False
    End Select
End Sub

Private Sub Agent1_Click(ByVal CharacterID As String, ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer)
    Set theAgent = Agent1.Characters(CharacterID)
    Set reqHB = theAgent.Speak("I'm now the active agent.")
End Sub

Private Sub Command1_Click()
    Set reqHB = theAgent.Speak("My name is " & theAgent.Name & ".")
End Sub

Private Sub Command2_Click()
    Set reqHB = theAgent.Speak("I'll do anything you want. Well... almost!")
End Sub




Since you do not want the agents to appear one over the other, Randomise and Rnd are used in LoadAgent() to place the agents randomly on the screen. When using a character variable (theAgent) in your code, you just need to Set theAgent = Agent1.Characters(CharacterID) in the Agent1_Click() event.