走近VB.Net(九) AxAgent動(dòng)畫(huà)窗體的實(shí)現(xiàn)
2024-07-10 13:01:28
供稿:網(wǎng)友
注冊(cè)會(huì)員,創(chuàng)建你的web開(kāi)發(fā)資料庫(kù),走近vb.net(九) axagent動(dòng)畫(huà)窗體的實(shí)現(xiàn)
前幾天看到一些網(wǎng)友討論msagent的貼子,其實(shí)在vb.net中實(shí)現(xiàn)也很容易卻總是沒(méi)有人肯動(dòng)手一試。現(xiàn)在我就介紹一下這個(gè)很簡(jiǎn)單的方法,希望激勵(lì)更多的同道加入我們的隊(duì)伍,以能相互間促進(jìn)(這次最好比“洋人”早一步)。
首先添加控件(不知道誰(shuí)說(shuō)vb.net不使用控件,真是越傳越玄)
第二步將控件在窗體上畫(huà)出來(lái)(這也是運(yùn)行時(shí)不可見(jiàn)的,不過(guò)他會(huì)出現(xiàn)在窗體上,而不是象timer控件出現(xiàn)在下面。
源代碼如下:
imports system.componentmodel
imports system.drawing
imports system.winforms
public class form1
inherits system.winforms.form
'指定要使用的動(dòng)畫(huà)數(shù)據(jù),這里的merlin.acs是一個(gè)圣誕老人的造型
dim genie as agentobjects.iagentctlcharacterex
const datapath as string = "merlin.acs"
'與vb6不同的是你不需要下載每一個(gè)都有2m多的動(dòng)畫(huà)數(shù)據(jù),在控件中已內(nèi)置了所有的動(dòng)畫(huà)文件
public sub new()
mybase.new() '調(diào)用父類(lèi)的new過(guò)程
form1 = me
'this call is required by the win form designer.
initializecomponent()
'設(shè)置窗體沒(méi)有標(biāo)題欄
me.borderstyle = formborderstyle.none
'設(shè)置窗體的色彩
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 = "少兒不宜"
'裝載動(dòng)畫(huà)
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() '調(diào)用父類(lèi)的dispose過(guò)程
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 '設(shè)定agent的位置
genie.show() '使用show方法顯示agent
genie.moveto(12, 180) 'moveto方法是移動(dòng)agent到一個(gè)位置,如果加上定時(shí)器,你可以很容易地讓他在屏幕上亂跑
messagebox.show("如果你是mm,請(qǐng)按ctrl+alt+del關(guān)閉 " & chr(10) & chr(13) & "您真的要繼續(xù)嗎, 等到我不 動(dòng)再說(shuō)!")
genie.speak("ni hao") '發(fā)出聲音
messagebox.show("你好") '對(duì)話框,更多的參數(shù)請(qǐng)看以前的相關(guān)文章
genie.speak("wo shi hao kan di") '發(fā)出聲音
messagebox.show("我是好看的") '對(duì)話框,更多的參數(shù)請(qǐng)看以前的相關(guān)文章
genie.speak("ni shi nan kan di") '發(fā)出聲音
messagebox.show("你是難看的") '對(duì)話框,更多的參數(shù)請(qǐng)看以前的相關(guān)文章
genie.speak("zia jie mimi ") '發(fā)出聲音
messagebox.show("再見(jiàn) mm") '對(duì)話框,更多的參數(shù)請(qǐng)看以前的相關(guān)文章
genie.play("wave") '揮手致意
'眾mm請(qǐng)勿生氣
'上面是開(kāi)一個(gè)小玩笑,如果要真正的讀中文,就用下面的
'genie.speak("cnstring ",cnstring.wav) '這里的cnstring就是中文的語(yǔ)音文件
genie.hide() '使用hide方法隱藏agent
end sub
private sub form1_mousedown(byval eventsender as object, byval e as system.winforms.mouseeventargs)
'mousedown的事件中窗體中的下拉列表是找不到的,自己寫(xiě)一個(gè)吧,加上(byval eventsender as object, byval e as system.winforms.mouseeventargs)即可
'下面的代碼使窗體可以拖動(dòng)
me.capture() = false '釋放鼠標(biāo)捕獲,等同于api的releasecapture()
me.sendmessage(&ha1s, 2, 0) '唔,這個(gè)就是哪個(gè)sendmessage的api了,只是第一個(gè)句柄參數(shù)不再用了。
end sub
protected sub form1_click(byval sender as object, byval e as system.eventargs)
end sub
end class
這個(gè)程序如果要在別的系統(tǒng)上運(yùn)行,一定要制作安裝程序(當(dāng)然先要升級(jí)系統(tǒng)組件并安裝sdk),請(qǐng)參考前面的幾篇文章,因?yàn)樾枰С謹(jǐn)?shù)據(jù),vb.net與vb6不同的是安裝的支持組件一般會(huì)在當(dāng)前目錄,而不是在系統(tǒng)目錄。