首页 > 开发 > 综合 > 正文

use MSAgent in C#

2024-07-21 02:18:17
字体:
来源:转载
供稿:网友
查资料的时候突然看到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在事件类构造的时候初始化。详细请参考:…



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表