主题:介绍用VB调用系统的动画精灵实例
默认的动画精灵共有4个:
1.默林(Merlin):他是一个法术高强的魔法师,可能比刘谦还要厉害。
2.小黄(rover):就是那只小狗狗啦。
3.清丽佳人(courtney):天姿国色,清纯可亲,永远坐在一辆红色的跑车上。
4.冲浪小子(earl):冲浪技术可是一流的,据说获得过世界大赛的奖牌。
这4个动画精灵文件在系统中的路径如下:
---------------------------------------------------
默林: %SystemRoot%\msagent\chars\Merlin.acs
清丽佳人:%SystemRoot%\srchasst\chars\courtney.acs
冲浪小子:%SystemRoot%\srchasst\chars\earl.acs
小黄: %SystemRoot%\srchasst\chars\rover.acs
---------------------------------------------------
下面介绍的,就是如何用VB代码来调用这四个精灵。
先介绍常用的方法:
---------------------------------------------------------------
Show >让精灵显示在屏幕上
Hide >让精灵隐藏起来,但没有从内存中删除
MoveTo x, y >让精灵移动到屏幕的 x,y 位置,单位:像素
Speak "要说的话" >让精灵“说话”,要说的话显示在一个语言气球内
Think "思考内容" >让精灵“思考”,思考内容显示在一个思考气球内
Play "命令字符串" >让精灵动作
---------------------------------------------------------------
请注意 Play 方法的“命令字符串”,这是使精灵做出各种动作的关键。这些命令字符串是微软预先
设置好的,我们只能照本宣科地使用,无法自己创新。
新建一个窗体,添加四个按纽,分别取名为:默林、小黄、清丽佳人、冲浪小子。代码如下:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim AgentPath As String '动画精灵文件的路径
Dim AgentName As String '动画精灵名称
Private Sub 默林_Click()
Dim objAgent, objCharacter
AgentName = "Merlin"
AgentPath = Environ("SystemRoot") & "\msagent\chars\" & AgentName & ".acs"
Set objAgent = CreateObject("Agent.Control.2")
With objAgent
.Connected = True '启动与 Agent 的连接
.Characters.Load AgentName, AgentPath '导入精灵。参数1-精灵名称,参数2-精灵文件路径
Set objCharacter = .Characters.Character(AgentName)
End With
With objCharacter '显示精灵的各种动作
.Width = .Width * 2 '放大
.Height = .Height * 2
.MoveTo 420, 130 '移动精灵到指定的屏幕位置,以像素为单位
.Show '显示精灵
.Speak "哈罗,我是魔法师默林。现在的时间是:" & Time & "。下面开始表演"
.MoveTo 340, 250
.Play "Acknowledge" '打招呼,点头,帽子上有螺旋桨
.Play "Alert" '敬礼,待命
.Play "Announce" '宣告,吹喇叭
.Play "Blink" '眨眼
.Play "Confused" '困惑、抓头
.Play "Congratulate" '祝贺,变出一个大奖杯
.Play "Congratulate_2" '祝贺,鼓掌
.Play "Decline" '无奈,摊手,摇头
.Play "DoMagic1" '变出一顶帽子
.Play "DoMagic2" '变魔术2
.Play "DontRecognize" '不认识没听清
.Play "Explain" '辩解、摊手
.Play "GestureUp" '向上手势,天哪
.Play "GestureDown" '向下手势,请
.Play "GestureLeft" '伸出左手
.Play "GestureRight" '伸出右手
.Play "GetAttention" '敲一下门
.Play "GetAttentionContinued" '继续敲门
.Play "GetAttentionReturn" '不敲了
.Play "Greet" '问候、躹躬
.Play "Idle1_1" '闲散1-1
.Play "Idle1_2" '闲散1-2
.Play "Idle1_3" '闲散1-3
.Play "Idle1_4" '闲散1-4
.Play "Idle2_1" '闲散2-1,拿出魔术棒
.Play "Idle2_2" '闲散2-2,两手在腹前交叉
.Play "Idle3_1" '闲散3-1,打呵欠
.Play "LookUp" '上看
.Play "LookDown" '下看
.Play "LookLeft" '左看
.Play "LookRight" '右看
.Play "LookDownBlink" '俯视眨眼,帽子上有螺旋桨,戴眼镜
.Play "LookDownReturn" '点头眨眼,帽子上有螺旋桨,戴眼镜
.Play "MoveUp" '上移,帽子上有螺旋桨,戴眼镜
.Play "MoveDown" '下蹲,马步,帽子上有螺旋桨,戴眼镜
.Play "MoveLeft" '左移,帽子上有螺旋桨,戴眼镜
.Play "MoveRight" '右移,帽子上有螺旋桨,戴眼镜
.Play "Pleased" '满足,双手在腹前相握
.Play "Process" '加工,调配
.Play "Read" '读
.Play "ReadContinued" '接着读
.Play "ReadReturn" '不读了
.Play "Sad" '悲哀
.Play "Search" '查找,变出一个球
.Play "StartListening" '开始听
.Play "StopListening" '不听了,掩耳
.Play "Suggest" '变出一个灯泡
.Play "Surprised" '惊讶
.Play "Uncertain" '无可奉告,身体前倾
.Play "Write" '写
.Play "WriteContinued" '接着写
.Play "WriteReturn" '不写了
.Speak "拜拜..."
.Play "Wave" '挥手
Sleep 10000
.Hide
End With
Do While objCharacter.Visible
Sleep 100
Loop
Set objAgent = Nothing
Set objCharacter = Nothing
End Sub
Private Sub 小黄_Click()
Dim objAgent, objCharacter
AgentName = "rover"
AgentPath = Environ("SystemRoot") & "\srchasst\chars\" & AgentName & ".acs"
Set objAgent = CreateObject("Agent.Control.2")
With objAgent
.Connected = True
.Characters.Load AgentName, AgentPath
Set objCharacter = .Characters.Character(AgentName)
End With
With objCharacter
.Width = .Width * 1.5
.Height = .Height * 1.5
.MoveTo 400, 300
.Show
.Speak "汪汪..."
.Play "Acknowledge" '打招呼后坐下
.Play "Congratulate" '起身摇尾祝贺
.Play "GestureLeft" '向左看
.Play "LookUpLeft" '向左上方看
.Play "GetAttention" '抬左前腿
.Play "Greet" '甩头
.Play "LookUp" '向上看
.Play "Pleased" '抬头向上看
.Play "Surprised" '惊讶,起身,伸舌头
.Play "Writing" '举起双爪
.Speak "886..."
Sleep 10000
.Hide
End With
Do While objCharacter.Visible
Sleep 100
Loop
Set objAgent = Nothing
Set objCharacter = Nothing
End Sub
Private Sub 清丽佳人_Click()
Dim objAgent, objCharacter
AgentName = "courtney"
AgentPath = Environ("SystemRoot") & "\srchasst\chars\" & AgentName & ".acs"
Set objAgent = CreateObject("Agent.Control.2")
With objAgent
.Connected = True
.Characters.Load AgentName, AgentPath
Set objCharacter = .Characters.Character(AgentName)
End With
With objCharacter
.Width = .Width * 2
.Height = .Height * 2
.MoveTo 420, 230
.Show
.Speak "哈罗,我是清丽佳人"
.MoveTo 340, 250
.Play "Acknowledge" '打招呼
.Play "Announce" '宣告,拿出手提话筒
.Play "Congratulate" '祝贺,拿出一枝花
.Play "Explain" '辩解、摆一下右手
.Play "GestureLeft" '伸出左手
.Play "GetAttention" '按小车喇叭两声
.Play "Greet" '问候、挥右手
.Play "LookUp" '向上看
.Play "Pleased" '满足
.Play "Surprised" '惊讶
.Play "Writing" '掏出手机通话
.Speak "886..."
Sleep 5000
.Hide
DoEvents
End With
Do While objCharacter.Visible
Sleep 100
Loop
Set objAgent = Nothing
Set objCharacter = Nothing
End Sub
Private Sub 冲浪小子_Click()
Dim objAgent, objCharacter
AgentName = "earl"
AgentPath = Environ("SystemRoot") & "\srchasst\chars\" & AgentName & ".acs"
Set objAgent = CreateObject("Agent.Control.2")
With objAgent
.Connected = True
.Characters.Load AgentName, AgentPath
Set objCharacter = .Characters.Character(AgentName)
End With
With objCharacter
.Width = .Width * 2
.Height = .Height * 2
.MoveTo 420, 130
.Show
.Think "我是冲浪高手"
.MoveTo 340, 250
.Play "Acknowledge" '打招呼,点头
.Play "Announce" '宣告,冲浪一次
.Play "Congratulate" '祝贺,身体前倾冲浪一次
.Play "Explain" '辩解、冲浪一次
.Play "GestureLeft" '向左转
.Play "Greet" '冲浪一次
.Play "LookUp" '向上看
.Play "LookUpLeft" '向左上方看
.Play "Pleased" '满足,双手抱于胸前
.Play "StopListening" '不听了,双手掩耳
.Play "Surprised" '惊讶、冲浪两次
.Play "Uncertain" '无可奉告,摊手
.Speak "886..."
Sleep 5000
.Hide
End With
Do While objCharacter.Visible
Sleep 100
Loop
Set objAgent = Nothing
Set objCharacter = Nothing
End Sub
以上代码中,我已将精灵们常用的命令字符串罗列出来,你可以只用其中的一个或若干个。另外还有几个命令字符串,如果使用了,动画就似乎陷入了无限循环一般,难以自拔,我还没有研究出来使用方法(哪位兄弟知道,请告知),这些字符串是:
Searching '不停地查找
Thinking '不停地思考
Writing '不停地写
Uncertain '清丽佳人用的,无可奉告,耸肩摊手
另外,还有一种使用 Agent 控件来调用动画精灵的办法,下面也介绍一下。
新建一个窗体,添加一个按纽,取名“调用精灵控件”,再在“工程->部件”的下拉框中勾选:
Microsoft Agent control2.0
于是工具箱上就有了一个戴礼帽的壮汉图标,这就是 Agent 控件,把这个控件添加到窗体,接着输入代码:
Option Explicit
Dim AgentPath As String '动画精灵文件的路径
Dim AgentName As String '动画精灵名称
Private Sub 调用精灵控件_Click()
Dim objCharacter As IAgentCtlCharacterEx '动画精灵对象名
AgentName = "Merlin"
AgentPath = Environ("SystemRoot") & "\msagent\chars\" & AgentName & ".acs"
Agent1.Characters.Load AgentName, AgentPath
Set objCharacter = Agent1.Characters.Character(AgentName)
With objCharacter
.Show
.Think "演奏什么乐器呢?"
.Play "Announce" '吹喇叭
.Hide
End With
Set objCharacter = Nothing
End Sub
代码就介绍到这儿。其实 Agent 控件还具有语音合成功能和语音识别能力,用户可以向动画人物发出语音命令(同时按住Scroll Lock键),不过,他们只听得懂英文(崇洋迷外的典型哦),哪位有兴趣可以自行研究。