use MSAgent in C#

类别:.NET开发 点击:0 评论:0 推荐:

查资料的时候突然看到MSAgent编程,找了些资料动手写了几行代码。

如果没有MSAgent runtime或者SDK的话,可以去www.microsoft.com下载,如果用VC++,则需要下载两个头文件。

创建.net项目以后,添加一个引用,选择COM页,然后选择Microsoft Agent Server 2.0。并在需要使用Agent的地方引用AgentServerObject的名字空间。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using AgentServerObjects;

using System.Threading;

namespace HelloAgent

{

     /// <summary>

     /// Form1 的摘要说明。

     /// </summary>

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button btnDelegate;

 

         private AgentServer agentServer = null;

         private IAgentEx agentEx = null;

         private IAgentCharacterEx characterEx = null;

         private IAgentNotifySink sink = null;

 

         private int dwCharID = 0;

         private int dwReqID = 0;

         private int dwSinkID = 0;

         private string strCharName = "C:\\WINDOWS\\msagent\\chars\\merlin.acs";

         /// <summary>

         /// 必需的设计器变量。

         /// </summary>

         private System.ComponentModel.Container components = null;

 

         public Form1()

         {

              //

              // Windows 窗体设计器支持所必需的

              //

              InitializeComponent();

 

              //

              // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

              //

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if (components != null)

                   {

                       components.Dispose();

                   }

              }

              base.Dispose( disposing );

         }

 

         #region Windows 窗体设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {

              this.btnDelegate = new System.Windows.Forms.Button();

              this.SuspendLayout();

              //

              // btnDelegate

              //

              this.btnDelegate.Location = new System.Drawing.Point(96, 32);

              this.btnDelegate.Name = "btnDelegate";

              this.btnDelegate.TabIndex = 0;

              this.btnDelegate.Text = "delegate";

              this.btnDelegate.Click += new System.EventHandler(this.btnDelegate_Click);

              //

              // Form1

              //

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(292, 273);

              this.Controls.Add(this.btnDelegate);

              this.Name = "Form1";

              this.Text = "Form1";

              this.Load += new System.EventHandler(this.Form1_Load);

              this.ResumeLayout(false);

 

         }

         #endregion

 

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

         [STAThread]

         static void Main()

         {

              Application.Run(new Form1());

         }

 

         private void InitAgent()

         {

              agentServer = new AgentServerObjects.AgentServerClass();

              if( agentServer == null )

              {

                   MessageBox.Show( "Agent初始化失败" );

              }

 

              agentEx = (IAgentEx)agentServer;

              agentEx.Load(strCharName, out dwCharID, out dwReqID);

              agentEx.GetCharacterEx(dwCharID, out characterEx);

 

              characterEx.Show(100, out dwReqID);

              characterEx.Speak("Hello", null, out dwReqID);

 

              string strName = "";

              characterEx.GetName(out strName);

              characterEx.Speak("My name is " + strName, null, out dwReqID);

 

              sink = new AgentEvent( new SpeakDelegate(SpeakSomething) );

              agentEx.Register(sink, out dwSinkID);

         }

 

         private void Form1_Load(object sender, System.EventArgs e)

         {

              InitAgent();

         }

 

         public delegate void SpeakDelegate(string str);

         public void SpeakSomething(string str)

         {

              if(InvokeRequired)

              {

                   BeginInvoke( new SpeakDelegate( SpeakSomething ), new object[]{str});

                   return;

              }

 

              characterEx.Wait(1000, out dwReqID);

              Thread.Sleep(1000);

              characterEx.Speak(str, null, out dwReqID);

         }

 

         private void ThreadProc()

         {

              SpeakSomething("Hello,this is use delegate.");

         }

 

         private void btnDelegate_Click(object sender, System.EventArgs e)

         {

              Thread thread = new Thread( new ThreadStart(ThreadProc) );

              thread.IsBackground = true;

              thread.Start();

         }

     }

}

代码没有加注释,因为我找不到需要注释的地方,请多多包涵。

Agent的事件需要继承AgengServerObject名字空间下的一些接口。

using System;

using AgentServerObjects;

namespace HelloAgent

{

     /// <summary>

     ///

     /// </summary>

     public class AgentEvent :IAgentNotifySink

     {

         private Form1.SpeakDelegate delg = null;

 

         public AgentEvent(Form1.SpeakDelegate delg)

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

              this.delg = delg;

         }

         #region IAgentNotifySink 成员

 

         public void Command(int dwCommandID, object punkUserInput)

         {

              // TODO:  添加 AgentEvent.Command 实现

              delg("Command");

         }

 

         public void DragStart(int dwCharID, short fwKeys, int x, int y)

         {

              // TODO:  添加 AgentEvent.DragStart 实现

              delg("DragStart");

         }

 

         public void RequestStart(int dwRequestID)

         {

              // TODO:  添加 AgentEvent.RequestStart 实现

              //delg("RequestStart");

         }

 

         public void VisibleState(int dwCharID, int bVisible, int dwCause)

         {

              // TODO:  添加 AgentEvent.VisibleState 实现

              delg("VisibleState");

         }

 

         public void BalloonVisibleState(int dwCharID, int bVisible)

         {

              // TODO:  添加 AgentEvent.BalloonVisibleState 实现

              //delg("BalloonVisibleState");

         }

 

         public void DragComplete(int dwCharID, short fwKeys, int x, int y)

         {

              // TODO:  添加 AgentEvent.DragComplete 实现

              delg("DragComplete");

         }

 

         public void Click(int dwCharID, short fwKeys, int x, int y)

         {

              // TODO:  添加 AgentEvent.Click 实现

              delg("Click");

         }

 

         public void DblClick(int dwCharID, short fwKeys, int x, int y)

         {

              // TODO:  添加 AgentEvent.DblClick 实现

              delg("DblClick");

         }

 

         public void Shutdown()

         {

              // TODO:  添加 AgentEvent.Shutdown 实现

              delg("Shutdown");

         }

 

         public void Size(int dwCharID, int lWidth, int lHeight)

         {

              // TODO:  添加 AgentEvent.Size 实现

              delg("Size");

         }

 

         public void Restart()

         {

              // TODO:  添加 AgentEvent.Restart 实现

              delg("Restart");

         }

 

         public void BookMark(int dwBookMarkID)

         {

              // TODO:  添加 AgentEvent.BookMark 实现

              delg("BookMark");

         }

 

         public void RequestComplete(int dwRequestID, int hrStatus)

         {

              // TODO:  添加 AgentEvent.RequestComplete 实现

              //delg("RequestComplete");

         }

 

         public void Move(int dwCharID, int x, int y, int dwCause)

         {

              // TODO:  添加 AgentEvent.Move 实现

              delg("Move");

         }

 

         public void Idle(int dwCharID, int bStart)

         {

              // TODO:  添加 AgentEvent.Idle 实现

              //delg("Idle");

         }

 

         public void ActivateInputState(int dwCharID, int bActivated)

         {

              // TODO:  添加 AgentEvent.ActivateInputState 实现

              delg("ActivateInputState");

         }

 

         #endregion

 

     }

}

在触发事件的时候调用了一个Form1里面的一个delegate,该delegate在事件类构造的时候初始化。详细请参考:…

本文地址:http://com.8s8s.com/it/it42604.htm