Mega Code Archive

 
Categories / Delphi / Examples
 

Programming with Microsoft Agent

Title: Programming with Microsoft Agent Question: How do you get the agent (Paperclip) like in Office to pop up in Delphi ? Answer: This tutorial was originally done by Maarten van Gompel (Proycon). I must say thanks to him cause it taught me how to do it. Link to his tutorial is : http://www.geocities.com/SiliconValley/Haven/2118/thttagent4Delphi.htm The next short part comes from Maarten van Gompel's tutorial. What is Microsoft Agent ? Microsoft Agent is an ActiveX Control (OCX) that makes you able to control little dudes that can do all sorts of things and can even speak. The Microsoft Agent looks very similar to the "Office Assistant" included with Microsoft Office 97 and above. You have several characters you can use with Microsoft Agent, you can even make your own Agent character with a special program from Microsoft! We're very lucky Microsoft made everything free! The most famous Agent character is Merlin the wizard I believe. Links : Microsoft Agent (required) (395kb) : http://download.cnet.com/downloads/0-14480-100-897538.html?st.dl.redir.txt.tdtl TruVoice Text2Speech Engine (837 kb) : http://download.cnet.com/downloads/0-14480-100-874123.html?st.dl.redir.txt.tdtl Without this you won't hear your Agent talking but you don't need it. Microsoft Agent Character Editor (981 kb) : http://download.cnet.com/downloads/0-14485-100-897540.html?st.dl.redir.txt.tdtl This is the program, which let you make your own character. Not needed but nice to have. Microsoft Agent Documentation : http://download.cnet.com/downloads/0-14485-100-897544.html?st.dl.redir.txt.tdtl The official documentation, it describes every aspect of the Agent Control. Nice to have. On with the tutorial. After you have installed the Microsoft Agent you need to import the Active X Control into Delphi. In Delphi 5 it's under the Component Menu item. There is a menu item called Import Active X Control. Move down in the list box until you reach an item called Microsoft Agent Control 2.0 (Version 2.0). Select this item and click on install. Now you should have the component under your Active X Components on your component palette. Drop this component on your form. First we have to declare an Instance variable. So put the next line in your private section of your from object. MyAgent : IagentCtlCharacterEx; Next we need to load the agent file (.acs) into memory. This we do in the form's FormCreate method. For the example I'm using the Merlin character cause I thinks his the coolest. The code below assumes that you have the acs file in the same directory as your source code. Agent1.Characters.Load('merlin', '.\merlin.acs'); MyAgent := Agent1.Characters.Character('merlin'); After the agent is loaded you can make him visible by using the show command. MyAgent.Show(0); If you want your agent to show at a specific spot on the screen use the following 2 command - Set_Left and Set_Top. The Get_Width command gives you the width of your agent. Below I set the Agent to start in the right, top corner of my form. MyAgent.Set_Left((Application.MainForm.Left+Form1.Width)-MyAgent.Get_Width); MyAgent.Set_Top(Application.MainForm.Top); To hide him again use the Hide command. MyAgent.Hide(0); To let your agent say something you use the Speak command. When you have the text to speech engine installed you will hear him saying what you typed in. MyAgent.Speak('Hello my name is Merlin',''); To move the agent you use the move command. NOTE : The coordinates are screen coordinates and not your form's coordinates. The 10 is the speed at which it must travel to 320x240. Zero is at light speed. MyAgent.MoveTo(320,240,10); You can let your agent think as well, instead of speaking. When you use this the agent will have a thinking bubble above it's head and you would not hear him say it. MyAgent.Think('Thinking of a new spell ... hmm ... '); Every agent also have some animation it can perform. In the Agent documention there is list of all the animation each character can perform. Those made by Microsoft. To animate your character use the Play command. MyAgent.Play('animationname'); Example : MyAgent.Play('Wave'); Some of the animation are looping animations and must be stopped by using the stop command. To use the stop command you need to store the variable returned by the Play command otherwise it does not know which animation it must stop. AgentRequests : iAgentCtlRequest; AgentRequests := MyAgent.Play('Processing'); MyAgent.Stop(AgentRequests); Below is the source code for the whole example. Enjoy!!! If all the source code below is to much for you, you can download the source code from here : There are 2 files. One with the merlin.acs file included , is 2megs big and one without it is 190kb. The one with the .acs file : http://www.geocities.com/SiliconValley/Haven/1884/tut-big.zip The small on : http://www.geocities.com/SiliconValley/Haven/1884/tut-small.zip This is the pas file's source code : unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, AgentObjects_TLB; type TForm1 = class(TForm) Agent1: TAgent; GroupBox1: TGroupBox; Edit1: TEdit; Button1: TButton; Button2: TButton; GroupBox2: TGroupBox; Button3: TButton; Button4: TButton; GroupBox3: TGroupBox; Button5: TButton; edtX: TEdit; edtY: TEdit; Label1: TLabel; Label2: TLabel; GroupBox4: TGroupBox; Button6: TButton; Edit2: TEdit; Button7: TButton; procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); private { Private declarations } MyAgent : IagentCtlCharacterEx; AgentRequests : iAgentCtlRequest; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin Agent1.Characters.Load('merlin', '.\merlin.acs'); MyAgent := Agent1.Characters.Character('merlin'); end; procedure TForm1.Button3Click(Sender: TObject); begin MyAgent.Set_Left((Application.MainForm.Left+Form1.Width)-MyAgent.Get_Width); MyAgent.Set_Top(Application.MainForm.Top); MyAgent.Show(0); end; procedure TForm1.Button4Click(Sender: TObject); begin MyAgent.Hide(0); end; procedure TForm1.Button1Click(Sender: TObject); begin MyAgent.Speak(Edit1.Text,''); end; procedure TForm1.Button2Click(Sender: TObject); begin MyAgent.Think(Edit1.Text); end; procedure TForm1.Button5Click(Sender: TObject); begin MyAgent.MoveTo(StrToInt(edtX.Text),StrToInt(edtY.Text),2000); end; procedure TForm1.Button6Click(Sender: TObject); begin AgentRequests := MyAgent.Play(Edit2.Text); end; procedure TForm1.Button7Click(Sender: TObject); begin MyAgent.Stop(AgentRequests); end; end. You also need the form's source code, so here it is as well : object Form1: TForm1 Left = 236 Top = 103 Width = 259 Height = 348 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object GroupBox2: TGroupBox Left = 8 Top = 8 Width = 233 Height = 65 Caption = 'Show or Hide' TabOrder = 2 object Button3: TButton Left = 16 Top = 24 Width = 75 Height = 25 Caption = 'Show' TabOrder = 0 OnClick = Button3Click end object Button4: TButton Left = 144 Top = 24 Width = 75 Height = 25 Caption = 'Hide' TabOrder = 1 OnClick = Button4Click end end object Agent1: TAgent Left = 216 Top = 8 Width = 32 Height = 32 ControlData = {020200004F0300004F030000} end object GroupBox1: TGroupBox Left = 8 Top = 80 Width = 233 Height = 81 Caption = 'Speak or Think' TabOrder = 0 object Edit1: TEdit Left = 8 Top = 18 Width = 209 Height = 21 TabOrder = 0 Text = 'Hello my name is Merlin' end object Button1: TButton Left = 8 Top = 46 Width = 75 Height = 25 Caption = 'Speak' TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 141 Top = 48 Width = 75 Height = 25 Caption = 'Think' TabOrder = 2 OnClick = Button2Click end end object GroupBox3: TGroupBox Left = 8 Top = 168 Width = 233 Height = 57 Caption = 'Move Agent' TabOrder = 3 object Label1: TLabel Left = 13 Top = 24 Width = 13 Height = 13 Caption = 'X :' end object Label2: TLabel Left = 69 Top = 24 Width = 13 Height = 13 Caption = 'Y :' end object Button5: TButton Left = 149 Top = 20 Width = 75 Height = 24 Caption = 'Move' TabOrder = 0 OnClick = Button5Click end object edtX: TEdit Left = 32 Top = 21 Width = 32 Height = 21 TabOrder = 1 Text = '320' end object edtY: TEdit Left = 85 Top = 21 Width = 28 Height = 21 TabOrder = 2 Text = '240' end end object GroupBox4: TGroupBox Left = 8 Top = 232 Width = 230 Height = 81 Caption = 'Animate' TabOrder = 4 object Button6: TButton Left = 16 Top = 46 Width = 75 Height = 25 Caption = 'Start' TabOrder = 0 OnClick = Button6Click end object Edit2: TEdit Left = 16 Top = 16 Width = 121 Height = 21 TabOrder = 1 Text = 'Wave' end object Button7: TButton Left = 136 Top = 47 Width = 75 Height = 25 Caption = 'Stop' TabOrder = 2 OnClick = Button7Click end end end