走近VB.Net(九) AxAgent动画窗体的实现
前几天看到一些网友讨论msagent的贴子,其实在VB.Net中实现也很容易却总是没有人肯动手一试。现在我就介绍一下这个很简单的方法,希望激励更多的同道加入我们的队伍,以能相互间促进(这次最好比“洋人”早一步)。
首先添加控件(不知道谁说VB.Net不使用控件,真是越传越玄)
第二步将控件在窗体上画出来(这也是运行时不可见的,不过他会出现在窗体上,而不是象timer控件出现在下面。
源代码如下:
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Public Class Form1
Inherits System.WinForms.Form
'指定要使用的动画数据,这里的merlin.acs是一个圣诞老人的造型
Dim Genie As AgentObjects.IAgentCtlCharacterEx
Const DATAPATH As String = "MERLIN.ACS"
'与VB6不同的是你不需要下载每一个都有2M多的动画数据,在控件中已内置了所有的动画文件
Public Sub New()
MyBase.New() '调用父类的new过程
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'设置窗体没有标题栏
Me.BorderStyle = FormBorderStyle.None
'设置窗体的色彩
Me.BackColor = Color.Beige
'TODO: Add any initialization after the InitializeComponent() call
Me.Height = 100 : button2.Top = 20 : button1.Top = 20 : button2.Text = "退出程序" : button1.Text = "少儿不宜"
'装载动画
axAgent1.Characters.Load("MERLIN", DATAPATH)
Genie = axAgent1.Characters("MERLIN")
Genie.LanguageID = &H409S
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose() '调用父类的dispose过程
genie = Nothing '释放资源
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private WithEvents Button2 As System.WinForms.Button
Private WithEvents Button1 As System.WinForms.Button
Private WithEvents AxAgent1 As AxAgentObjects.AxAgent
Dim WithEvents Form1 As System.WinForms.Form
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.components = New System.ComponentModel.Container()
Me.Button1 = New System.WinForms.Button()
Me.Button2 = New System.WinForms.Button()
Me.AxAgent1 = New AxAgentObjects.AxAgent()
AxAgent1.BeginInit()
'@design Me.TrayHeight = 0
'@design Me.TrayLargeIcon = False
'@design Me.TrayAutoArrange = True
Button1.Location = New System.Drawing.Point(32, 8)
Button1.Size = New System.Drawing.Size(80, 32)
Button1.TabIndex = 1
Button1.Text = "Button1"
Button2.Location = New System.Drawing.Point(120, 8)
Button2.Size = New System.Drawing.Size(80, 32)
Button2.TabIndex = 2
Button2.Text = "Button2"
AxAgent1.Size = New System.Drawing.Size(56, 40)
AxAgent1.OcxState = CType(resources.GetObject("AxAgent1.OcxState"), System.WinForms.AxHost.State)
AxAgent1.TabIndex = 0
AxAgent1.Location = New System.Drawing.Point(216, 32)
Me.Text = "Form1"
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.Controls.Add(Button2)
Me.Controls.Add(Button1)
Me.Controls.Add(AxAgent1)
AxAgent1.EndInit()
End Sub
#End Region
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Dispose()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
genie.Left = 500 '设定agent的位置
Genie.Show() '使用show方法显示agent
genie.MoveTo(12, 180) 'moveto方法是移动agent到一个位置,如果加上定时器,你可以很容易地让他在屏幕上乱跑
messagebox.Show("如果你是MM,请按Ctrl+Alt+Del关闭 " & chr(10) & chr(13) & "您真的要继续吗, 等到我不 动再说!")
Genie.Speak("ni hao") '发出声音
messagebox.Show("你好") '对话框,更多的参数请看以前的相关文章
Genie.Speak("WO SHI HAO KAN DI") '发出声音
messagebox.Show("我是好看的") '对话框,更多的参数请看以前的相关文章
Genie.Speak("NI SHI NAN KAN DI") '发出声音
messagebox.Show("你是难看的") '对话框,更多的参数请看以前的相关文章
Genie.Speak("ZIA JIE MIMI ") '发出声音
messagebox.Show("再见 MM") '对话框,更多的参数请看以前的相关文章
genie.Play("Wave") '挥手致意
'众MM请勿生气
'上面是开一个小玩笑,如果要真正的读中文,就用下面的
'Genie.Speak("CNstring ",CNstring.wav) '这里的CNstring就是中文的语音文件
genie.Hide() '使用hide方法隐藏agent
End Sub
Private Sub Form1_MouseDown(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)
'mousedown的事件中窗体中的下拉列表是找不到的,自己写一个吧,加上(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)即可
'下面的代码使窗体可以拖动
Me.Capture() = False '释放鼠标捕获,等同于API的ReleaseCapture()
Me.SendMessage(&HA1S, 2, 0) '唔,这个就是哪个sendmessage的API了,只是第一个句柄参数不再用了。
End Sub
Protected Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
End Class
这个程序如果要在别的系统上运行,一定要制作安装程序(当然先要升级系统组件并安装SDK),请参考前面的几篇文章,因为需要支持数据,VB.Net与VB6不同的是安装的支持组件一般会在当前目录,而不是在系统目录。
VB.Net中文站网址:http://vbnetcn.126.com