查資料的時(shí)候突然看到msagent編程,找了些資料動(dòng)手寫(xiě)了幾行代碼。
如果沒(méi)有msagent runtime或者sdk的話,可以去www.microsoft.com下載,如果用vc++,則需要下載兩個(gè)頭文件。
創(chuàng)建.net項(xiàng)目以后,添加一個(gè)引用,選擇com頁(yè),然后選擇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 的摘要說(shuō)明。
/// </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>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設(shè)計(jì)器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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();
}
}
}
代碼沒(méi)有加注釋?zhuān)驗(yàn)槲艺也坏叫枰⑨尩牡胤剑?qǐng)多多包涵。
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: 在此處添加構(gòu)造函數(shù)邏輯
//
this.delg = delg;
}
#region iagentnotifysink 成員
public void command(int dwcommandid, object punkuserinput)
{
// todo: 添加 agentevent.command 實(shí)現(xiàn)
delg("command");
}
public void dragstart(int dwcharid, short fwkeys, int x, int y)
{
// todo: 添加 agentevent.dragstart 實(shí)現(xiàn)
delg("dragstart");
}
public void requeststart(int dwrequestid)
{
// todo: 添加 agentevent.requeststart 實(shí)現(xiàn)
//delg("requeststart");
}
public void visiblestate(int dwcharid, int bvisible, int dwcause)
{
// todo: 添加 agentevent.visiblestate 實(shí)現(xiàn)
delg("visiblestate");
}
public void balloonvisiblestate(int dwcharid, int bvisible)
{
// todo: 添加 agentevent.balloonvisiblestate 實(shí)現(xiàn)
//delg("balloonvisiblestate");
}
public void dragcomplete(int dwcharid, short fwkeys, int x, int y)
{
// todo: 添加 agentevent.dragcomplete 實(shí)現(xiàn)
delg("dragcomplete");
}
public void click(int dwcharid, short fwkeys, int x, int y)
{
// todo: 添加 agentevent.click 實(shí)現(xiàn)
delg("click");
}
public void dblclick(int dwcharid, short fwkeys, int x, int y)
{
// todo: 添加 agentevent.dblclick 實(shí)現(xiàn)
delg("dblclick");
}
public void shutdown()
{
// todo: 添加 agentevent.shutdown 實(shí)現(xiàn)
delg("shutdown");
}
public void size(int dwcharid, int lwidth, int lheight)
{
// todo: 添加 agentevent.size 實(shí)現(xiàn)
delg("size");
}
public void restart()
{
// todo: 添加 agentevent.restart 實(shí)現(xiàn)
delg("restart");
}
public void bookmark(int dwbookmarkid)
{
// todo: 添加 agentevent.bookmark 實(shí)現(xiàn)
delg("bookmark");
}
public void requestcomplete(int dwrequestid, int hrstatus)
{
// todo: 添加 agentevent.requestcomplete 實(shí)現(xiàn)
//delg("requestcomplete");
}
public void move(int dwcharid, int x, int y, int dwcause)
{
// todo: 添加 agentevent.move 實(shí)現(xiàn)
delg("move");
}
public void idle(int dwcharid, int bstart)
{
// todo: 添加 agentevent.idle 實(shí)現(xiàn)
//delg("idle");
}
public void activateinputstate(int dwcharid, int bactivated)
{
// todo: 添加 agentevent.activateinputstate 實(shí)現(xiàn)
delg("activateinputstate");
}
#endregion
}
}
在觸發(fā)事件的時(shí)候調(diào)用了一個(gè)form1里面的一個(gè)delegate,該delegate在事件類(lèi)構(gòu)造的時(shí)候初始化。詳細(xì)請(qǐng)參考:…