Microsoft Agent 学习笔记 (一)

    技术2022-05-11  132

     

                  Microsoft Agent 学习笔记 (一)

        Microsoft Agnet是一组可编程的软件服务, 用来管理一些角色(Character),这些角色通过动画、声音

    来同计算机用户交流,使用最新的技术,他们甚至可以听懂人的话,也就是说,用户可以用声音控制他们,

    这使得应用程序的操作方式更符合人的行为方式。

      虽然Microsoft对于Agent最成功的使用是Office Asistant, 它用来提供上下文帮助和使用向导,但是可以把

    Agent使用在任何地方,譬如一般的应用程序和游戏中。在本文和后面的文章中,我将简单的介绍一下它的使用。

       MS Agent组件包含了一组对象,它们分别是Request, Agent(control), Characters (collection), Character,

    Commands(collection),Command,Balloon,AnimationNames(collection),SpeechInput,AudioOutput,CommandsWindow,

    PropertySheet。本篇先介绍Agent(control), Characters(collection), Request三个对象,其他对象以后再作介绍。

       MS Agent组件包含了一组对象,它们分别是Request, Agent(control), Characters (collection), Character,

    Commands(collection),Command,Balloon,AnimationNames(collection),SpeechInput,AudioOutput,CommandsWindow,

    PropertySheet。本篇先介绍Agent(control), Characters(collection), Request三个对象,其他对象以后再作介绍。

      Agent(control)--通过它,我们可以访问到Microsoft Agent支持的大部分对象和事件。它有几个重要的属性和方法,

    包括:Connected属性, Characters属性。

     

    unit AgentDemo;

    uses

      AgentObjects_TLB; //引用Delphi导入的TypeInfo unit

     

    const

    RequestSuccessfully = 0

    RequestFailed = 1;

      RequestPending = 2;

      RequestInterrupted = 3;

      RequestInProgress = 4;

     

    var

      Agent1: TAgent; //声明TAgent类型的变量Agent1

      IAgnt: IAgentCtlEx;

      Merlin: IAgentCtlCharacterEx;

      IReq: IAgentCtlRequest;

     

    begin

      Agent1 := TAgent.Create; 

      IAgnt := Agent1.ControlInterface;             //取得IAgentCtlEx接口

      IAgnt.Connected := True;                      //连接到Microsoft Agent Server

      IReq := IAgnt.Characters.Load('Merlin', 'Merlin.acs');        //加载Merlin角色

      Merlin := IAgnt.Characters.Character('Merlin');  //取得Merlin的Character接口

      Merlin.Show(False);                       //显示Merlin

      IReq := Merlin.Play('Congratulate_2');         //让Merlin鼓掌

      if IReq.Status <> RequestSuccessfully then                 //判断Merlin当前的状态,如果鼓掌动画//还没有完成就停掉它

      Merlin.Stop;

      Merlin.Speak('Hello, guys', '');     //让Merlin说话

      Merlin.Hide;                         //隐藏Merlin

      IAgnt.Characters.Unload('Merlin');  //卸载Merlin

      IAgnt.Connected := False;  //与Microsoft Agent Server断开连接

      Agent1.Free;

    end;

     

     

    说明:Request Object用来同步对Character的访问,对Character的访问,如Load, Get, Play, Speak都会产生一个Request,

    IAgentCtlRequest的Status属性有五个值,分别是

      0:请求已经完成

      1:请求失败

      2:请求已经提交,但是还没有完成

      3:请求被中断

      4:请求正在处理

    Request对象在上面的程序中的作用就是强制Merlin停止鼓掌说话,你也可以用在多个Character的代码中,例如:让多个Character

    聊天,

     

    procedure WaitFor(AReq: IAgentCtlRequest);

      var

      Status: LongInt;

    begin

      if AReq <> nil then

    repeat

            Application.ProcessMessages;

            Status := AReq.Get_Status;

          until (Status <> RequestPending) and (Status<>RequestInProgress)

      end;

      end;

      Req := Merlin.Speak('Hello, Monkey King','');

      WaitFor(Req);

      Req := MonkeyKing.Speak('Hello, Merlin','');

      WaitFor(Req);

      Req := Merlin.Speak('Byebye, Monkey King', '');

      WaitFor(Req);

      Req := MonkeyKing.Speak('bye', '');

     

     

    上面的程序使用的是early binding, 需要import MS Agent 2.0的TypeInfo, 如果使用late binding,则可以这样

    uses

      ComObj;

    var

      vAgent: OleVariant;

      Merlin: OleVariant;

      Req: OleVariant;

    begin

      vAgent := CreateOleObject('Agent.Control.2');

      vAgent.Connected := True;

      Req := vAgent.Characters.Load('Merlin', 'Merlin.acs');

      Merlin := vAgent.Characters.Character('Merlin');

      Req := Merlin.Show(False);                      

      WaitFor(Req):

      Req := Merlin.Play('Congratulate_2');        

      WaitFor(Req);

      Merlin.Speak('Hello, guys', '');    

      Merlin.Hide;                        

      vAgent.Characters.Unload('Merlin');  

      vAgent.Connected := False; 

    end. 

     

    不过速度上面会慢,因为涉及到接口的多次查询和转换

     

    以上代码只是简单的演示和说明,只能作为参考。

    以上技术资料可以参考    <<Delphi COM Programming>>  Eric Harmon

                            MSDN---->Platform SDK-->User Interface Services--> Microsoft Agent

     

     

        必要的准备:因众多语言中,本人只略懂Delphi, 所以所有的思路及程序代码都以Object Pascal描述,因此

    读者应具备一定的Object Pascal基础和Delphi使用经验。另外由于MS Agent包含有一组ActiveX组件,要运行程序,

    也必须先在本地计算机上面安装Microsoft Agent 2.0。Microsoft Agent 2.0和角色文件请至www.Microsoft.com下载。本文

    所有的源程序可以发信至lukejee@263.net向我索取。

     

                                                                                 Luke Jee

                                                                                 2001.11.22


    最新回复(0)