天天看點

VB與flash互動

轉載位址:http://hj20762003.blog.163.com/blog/static/80530446201002494558526/

其實這是一個很容易解決的問題,可惜這個問題屬于兩不管的境遇,VB的書沒有,FLASH的書也沒有,對于不懂FLASH的VB寫手就是個問題。

一、将FLASH插入VB窗體

在VB工程/部件的清單裡找到shockwave flash,添加即可在工具欄裡找到FLASH控件

,添加之。。。

該控件有兩個值得注意的屬性,movie和playing,movie指代FLASH檔案,擴充名.SWF,強烈建議采用相對路徑給其指派,例如:

Dim strpath As String

strpath = App.Path

If Right(strpath, 1) <> "\" Then

strpath = strpath & "\"

End If

shockflash.Movie = strpath & "ping.swf"

而playing為是否自動播放,預設為false,記得改成true哦!

現在你可以用FLASH做為你的閃屏啦!

二、實作互動

首先,打開FLASH,要将做好的FLASH按紐標明,在on release事件裡編寫代碼如下:

{

fscommand("string");

}

這裡的string為任意字元串,傳遞給VB的就是這個字元串。讓我們啟動VB,在shockwave flash控件就會多出fscommand事件,FLASH裡的字元串已經賦給了參數command,假如我們有多個按鈕做為工具欄,那麼我們就可以通過判定command來編寫相應的代碼。以下是我的執行個體:

Private Sub flashmain_FSCommand(ByVal command As String, ByVal args As String)

Select Case command

Case "cmdshop"

Frmkaitai.Show vbModal

Case "cmdvip"

frmMbEmit.Show vbModal

Case "cmdhome"

FrmJiezhang.Show vbModal

Case "cmdbill"

Frmtuitai.Show vbModal

Case "cmdjiankong"

FrmXiaofei.Show vbModal

Case "cmdhelp"

frmAbout.Show vbModal

Case "cmdexit"

End

End Select

End Sub

三、應用

VB界面的惡心是天人共憤的,利用FLASH就可以很好的彌補這一點。FLASH美化界面的能力使得FLASH與各種程式設計工具間的互動變為FLASH推廣的契機。

(一) 在VB中播放Flash動畫

怎樣才能在VB中加入Flash動畫呢?我們隻要使用Flash5自帶的Shockwave Flash.ocx這個控件就可以了。方法如下:

1. 打開VB6.0,建立一個工程,在工具箱上單擊右鍵,選擇Components…,在部件視窗的控件清單中選擇Shockwave flash,然後确定,Flash控件就被加到工具箱上。

2.選擇工具箱上的Flash控件,放到窗體上并調整好大小;

3.在Form_Load()過程中加入如下代碼:

Private Sub Form_Load()

ShockwaveFlash1.Movie = “D:\test.swf”

'這裡寫上你的Flash檔案目錄

ShockwaveFlash1.Menu = False

ShockwaveFlash1.Playing = True

End Sub

其中,ShockwaveFlash1.Movie是用來指定你要播放的Flash動畫的目錄;ShockwaveFlash1.Menu是用來指定是否在Flash動畫播放中封閉右鍵菜單,True為能夠顯示菜單,False為封閉右鍵菜單;ShockwaveFlash1.Playing = True是讓動畫播放。

好了,按F5看看吧,是不是程式中的Flash動畫能播放了? :)

(二) 在Flash中控制VB程式實作互動

現在Flash動畫已經能在VB程式中播放了,那麼怎麼實作在Flash中控制VB的程式進而實作互動式的操作呢?本文的重點也在于此。

首先我們先來了解Flash中控制VB程式的基本原理:在Flash的ActionScript裡面有個叫做FSCommand()的函數,它的主要功能就是發送FScommand指令,例如使動畫全屏播放,隐藏動畫菜單,更重要的就是可以與外部檔案和程式進行通信。而在VB程式中,我們就是利用的Shockwave flash控件的FSCommand()過程來完成這一通信過程,實作資訊發送的功能,并且根據發送出來的不同的指令及參數來實作對VB程式的控制。

明白了吧?簡單的說,我們就是利用Flash中的FSCommand()函數向VB發送指令,利用Shockwave Flash控件來接受這個指令,進而達到Flash控制VB程式的目的。

好了,現在知道了原理,我們就一步一步來實作吧!

1. 首先要打開Flash5制作一個互動按鈕,并在按鈕上面加上如下代碼:

on (release) {

fscommand ("Send Action");

//發送Send Action這個指令

}

這個指令的作用是當按下按鈕後Flash向VB發送出名為”Send Action”的指令。當然,這個指令的名字在實際應用中可以叫做其他的任何名字。

2. 将Flash導出成為swf檔案,如文章第一部分所述插入到VB6.0中,下面就是VB怎麼接受這個指令的事情了;

3. 在窗體上輕按兩下Shockwave Flash控件,進入代碼編輯視窗,加入如下語句

其中,ShockwaveFlash1_FSCommand這個過程是專門用來接收Flash發送的FSCommand指令的,其中第一個參數command就是與Flash發送過來的指令相對應的,當其一緻的時候,則執行後面的程式(本例中是彈出MsgBox)。好了,看看程式運作的結果吧:

Private Sub ShockwaveFlash1_FSCommand(ByVal command As String, ByVal args As String)

If command = "Send Action" Then

msg = MsgBox("Flash與VB成功結合了!", vbApplicationModal, "成功了!")

'當接到Send Action指令的時候

'這裡加入你需要的程式

End If

End Sub

FSCommand in ActionScript

To be able to communicate with any application outside the Flash environment, ActionScript exposes a method, FSCommand (command, arguments). This method takes two parameters, first one is the command you wish to execute and second the arguments that you can pass if needed. FSCommand can be called from anywhere in flash, on a button click, during execution of the flash, or anywhere you want to call some outside code. For demonstration purpose our sample application calls it on the Button click.

AS中的FSCommand方法

為了能夠與非FLASH環境的外部應用程式通訊,AS裡提供了一種方法,FSCommand(指令符,參數).

這個方法有兩個參數,第一個參數是你打算執行的指令符,第二個參數是你打算傳遞給執行指令符時所需的參數,如果有需要參數的話.

這一方法能在FLASH中随處被調用,在一個按鈕上,在播放FLASH的時候,或者其它你想要呼叫外部代碼的任何地方.以下的示範我們講這一方法置放于一個按鈕事件上.

Flash.ocx (SWFlash.ocx) ActiveX component

To view the Flash .swf files in VB you have to add the Flash component to the project. To add a component, click on the Project and then on Components. Look for the name “Shockwave Flash” in the list and include it in the project. The file name against this component points to Flash.ocx (or SWFlash.ocx).

This component is installed on your system when you install Macromedia Flash MX or the Flash Player. It is a full-featured ActiveX component with interfaces that enable its usage within VB applications, using Microsoft’s COM/ActiveX technologies.

This component has the ability to play Flash .swf files and is the core of Flash integration with Visual Basic programs.

Flash playing inside VB programs

The fundamental point to remember is: Flash integration in VB simply means the ability to play Flash files inside your VB programs using the Flash component. These .swf files are exactly the same as any Flash file played in your browser and nothing special. The same content can be reused on the Web and inside your desktop VB programs.

Flash.ocx(SWFlash.ocx) ActiveX控件.

為了能夠在VB中播放flash影片,你需要加載一個控件到VB工程中去.

點選"Project",然後點選"Components".(漢化版VB标簽為:工程->部件,打開控件選擇面闆的熱鍵為CTRL+T)然後找到"Shockwave Flash",并選中.這一檔案名連接配接着名為Flash.ocx(或SWFlash.ocx)的控件.

這一控件在你安裝Macromedia Flash MX 或者 Flash 播放器的時候便自動安裝在你的系統當中了.它是一個全特征型的,可由使用微軟COM/ActiveX技術的VB程式控件界面的ActiveX控件.此控件能夠播放FLASH影片,是FLASH影片與VB程式互動的核心部分.

在VB程式中播放FLASH影片

有一個基本原則你必須清楚,那就是:能在VB中通過FLASH控件播放FLASH影片.這裡的".swf"格式的Flash影片是正常的,由flash本身生成的一般的flash影片.相同的内容,卻能在你的VB程式裡再生.

FSCommand event

The Flash component has several functions and events, which you can find by using VB’s Object Inspector. The most important of these is the FSCommand event. As you may guess, this is closely related to the FSCommand ActionScript function.

When you call FSCommand in your ActionScript and if the Flash file is being played inside the Flash component, it will generate a FSCommand event in your VB program. If we write code inside the FSCommand in VB, this code will then execute.

FSCommand事件

這個FLASH插件中有幾個方法和事件,這些你都可以通過VB對象檢視器找到.而這其中又數FSCommand事件最為重要.正如你可能想到的,這個和AS中的FSCommand方法有互相聯系.當你在AS中調用FSCommand方法,FLASH影片又是在VB中的FLASH插件中播放的時候,在VB程式中就會産生一個FSCommand事件了.如果你為VB中的FSCommand事件編寫一定代碼的話,這些代碼将會被執行.

Set/GetVariable functions

These functions are used when communication needs to flow in the opposite direction i.e. from your VB program to the Flash player.

A very useful technique is to loop inside the Flash program on a variable, and then to set the variable from VB to indicate something.

Our sample application

To illustrate the concepts described in this article, we have made a simple sample application. When you click a button in Flash, it will notify your VB program which will then set a Flash textbox’s text from the VB program. It looks like this:

(圖檔請看源檔案)

1. Button click fires FSCommand in ActionScript

2. FSCommand trapped in VB

3. VB sets variable in Flash

4. Textbox gets new value

Creating the simple Flash file

Open Flash MX application. Create a new file and add a textbox from Tools window to the frame. The basic structure in Flash is called a Frame. Right click on the textbox and choose Properties. This will open a properties window at the bottom of Flash screen. Select Dynamic Text in the Text tool option. Set the Instance name as sampleField. Set the Var as sampleFieldVar. Then enter any text into the textbox. This value we will change from VB using FSCommand – but more about that later.

Now to add a button, on the right side you might see a Flash UI Components window. Incase it’s not there, click on Window in menu and click on Components. This brings up the UI components. Drag and drop a Push Button onto the frame. Right click on the button and click on Actions. This opens a window where you can type. In case you are not able to type into that window find an option within this actions window to be set to Expert Mode instead of a Normal Mode. Now type in the following lines there:

on(press) {

fscommand(“setvalue”, “Sample Value”);

}

Now save this file as sample.fla. Then to convert it to an swf file, go to File and click on Export Movie. Save the file as sample.swf. That’s it, you are ready to use this flash file in your VB application.

Set/GetVariable方法

這兩個方法是用來做VB向FLASH影片回饋工作的.

一個非常有用的技巧,便是讓VB使用上面的方法,設制FLASH影片中的一個變量值,以此來說明一些事情.

我們的例子

為了說明文章中描述的内容,我們需要做一個簡單的程式.實作這樣一個效果:當你點一個FLASH中的按鈕,VB程式将會為FLASH影片中的一個動态文本框設值.如下圖

VB與flash互動

圖檔請看源檔案)

1.激發FSCommand方法的按鈕

2.在VB中攔截FSCommand事件.

3.調用setVariable方法,設定FLASH影片中的變量值

4.FLASH中的動态文本框獲得新值.

建立例子的FLASH檔案

打開FLASH程式.建立一個新檔案,添加一個動态文本框和一個按鈕入場景中.文本框執行個體名為:sampleField,文本變量名為:sampleFieldVar,按鈕執行個體名可不用設,按鈕上加上AS代碼:

on(press) {

fscommand(“setvalue”, “Sample Value”);

}

現在儲存這個FLASH檔案到指定目錄,然後再生成影片.這樣一個簡單的FALSH影片便在那個儲存FLASH檔案的目錄下生成好了.這便是我們準備加到VB程式中要用到的FLASH影片.

Programming VB-Flash interaction

Insert a ShockwaveFlash component onto a VB form. Name the Flash component as swfFlashScreen. Now to load the Flash (.swf) file into this component use the following function:

Call swfFlashScreen.LoadMovie(0, filename)

The filename is the full path of the swf file. The first parameter signifies the level where Flash is to be loaded; we used 0 for root level.

The FSCommand event is invoked automatically when FSCommand is called from Flash file. So you have the command and arguments in VB (generated from Flash), and you can perform the required actions on them.

Now, that we are able to pass command/values from Flash to VB, what about the other way round, i.e. how to pass information from VB to Flash? I will explain how we did it by passing a value from VB to Flash.

In Flash file, we made a textbox named sampleField, with var as sampleFieldVar. We can set the value of the textbox from VB using this Var field name. Use the following method to do it:

Call swfFlashScreen.SetVariable("sampleFieldVar", value)

The first parameter is the textfield Var name, and the second is the value to be assigned.

This call works for the currently loaded Flash screen. Now that value is in the flash textbox, you can use it the way you like.

編寫VB-FLASH互動

插入一個FLASH插件到VB表單中,為之命名為swfFlashScreen.現在使用下面的方法加載剛剛的FLASH影片到這個元件.

Call swfFlashScreen.LoadMovie(0, filename)

你也可以在插件的屬性面闆中直接指定加載檔案的位址.

第一個參數表示加載影片的級别,這裡設成0,表示根目錄級别.

當插件中的FLASH影片中激發了FSCommand事件量,插件便會自動攔截FSCommand事件.根據參數決定VB要做的相關操作即可. 現在我們能夠從FLASH中傳遞指令和參數到VB中了.然後傳回又應該做什麼呢,也就是說,如何把資訊回報到FLASH影片中呢.我将會向你描述我是如何把值從VB傳遞到FLASH中的.

在先前的FLASH檔案中,我們已經設定了一個執行個體名為 sampleField 的文本框,并将其文本變量設為 sampleFieldVar.為了能改變這個文本框中的值.我們使用下面的方法:

Call swfFlashScreen.SetVariable("sampleFieldVar", value)

第一個參數表示文本框變量的變量名,第二個值表示要賦予的值.當你按下FLASH中的按鈕之後,文本框會立即更新.

VB中的代碼:

Private Sub swfFlashScreen_FSCommand(ByVal command As String, ByVal args As String)

If command = "setvalue" Then

Call swfFlashScreen.SetVariable("sampleFieldVar", value)

end if

end sub

Passing notifications from VB to Flash

Invoking VB from Flash is easy by using the fscommand ActionScript call. To notify Flash from VB, we have no direct mechanism.

Suppose Flash calls VB to do something using the FSCommand, and proceeds after VB has done some processing in the FSCommand. In this case, VB needs to tell Flash when processing is over. Since VB can’t call Flash, we will use the data-passing mechanism (SetVariable) to simulate function calls.

We achieve this by having an ActionScript variable and waiting on it. Flash loops on this variable waiting for VB to set the variable, and proceeds execution after VB has put in a value there.

For example, put a textbox on the Flash frame. The initial value of a textbox is ‘undefined’. We made use of this property of textbox to wait on the timer. Then we called the VB fscommand to carry out our operation there. When VB returns, it sets that variable we are waiting on. Then we clear the interval and proceed in Flash. Until this variable is set, Flash keeps on looping in that function.

Example:

Create a textbox with Var name: VBCalling

// this function in flash makes a VB call and waits until the variable is set to some value from VB

function CallVBFunction() {

fscommand(“Test”, “Test”); // function call to VB

keepWaitingForResponse(); // call the flash function

}

// this function doesn’t go ahead until the VBCalling is set from VB

function keepWaitingForResponse() {

intervalID = setInterval(

function () { // this function is called ever 100 ms until the intervalID is cleared

if (VBCalling != undefined) {

VariableReturned(VBCalling);

}

}, 100);

}

// this function is called only after the variable is set from VB

function VariableReturned(value) {

clearInterval(intervalID); // clear the interval and stop the looping

// you can use the value if you want to and do your thing here

}

從VB釋出通知到FLASH

從FLASH中呼叫VB中的方法可以通過FSCommand這一"接口"輕松實作.而反過來,由VB向FLASH釋出呼叫通知卻沒有直接的方法.

假設FLASH通過FSCommand呼叫VB做一些操作,當VB把這些操作執行完畢的時候,要向FLASH回報一個消息告知操作完成.但是VB卻不能直接呼叫FLASH中的方法,但是我們可以使用資料傳遞機制,(即setVariable)來模拟呼叫方法.

我們通過FLASH等待某一特定變量值來實作上述呼叫過程.在FLASH中設定一個循環監聽器,監聽從VB傳來的被改變的特定變量的值.VB将先前的操作完成後,執行setVariables方法将FLASH中正被監聽着的變量賦以新值.這時.正在監聽的FLASH判定該變量值是否改變.如果改變,即說明,VB已經執行完它的操作.換句話講.也可以在這裡設裡消息篩選.根據該變量的不同的值,呼叫FLASH中不同的方法,進而間接地實作VB呼叫FLASH中方法的目的.

舉一個例子說明.放一個動态文本框在主場景.設其預設值為undefined.這時,我們呼叫VB,讓其做我們想要的操作,當操作完畢時,VB将文本框裡的值改變,即,将文本變量值改變.而在FLASH這邊,已經設定好的定時器正以某種頻率監聽着該文本變量的值是否改變.如果改變,說明VB執行完畢.清除定時器.再呼叫FLASH相關的方法,當然,如果有必要的話.

例子:

//建立一個變量名為 VBCalling的動态文本框

// 這個方法用于呼叫VB,将同時開始執行keepWaitingForResponse方法.監聽變量值是否經由VB改變.

function CallVBFunction() {

fscommand(“Test”, “Test”); // 呼叫VB中的方法

keepWaitingForResponse(); // 呼叫FLASH中的方法

}

// 該函數在VB未收到資料改變的指令是不會向前執行,将一直處于監聽狀态

function keepWaitingForResponse() {

intervalID = setInterval(

function () { // 該函數每過100毫秒執行一次,直到定時器被清除

if (VBCalling != undefined) {

VariableReturned(VBCalling);

}

}, 100);

}

//該函數隻在監聽發現變量值被VB改變的時候被執行.

function VariableReturned(value) {

clearInterval(intervalID); // 清除定時器,結束監聽

// 你可以在這裡放上其它FLASH中的方法,如果有必要的話

}

KAN:

可以說,FLASH正是利用了FSCommand這一"接口"來實作與VB程式的互動.既能讓FLASH調用VB中的方法,也能讓VB輕松控制FLASH影片的播放.

更多FLASH插件方法,請在VB工程中按F2,再找到shockwaveFlash插件.你會發現有好多關于桢控制的方法,以及一些關于加載的方法.

用VB做自己的可以拖動播放的FLASH播放器,也就變得容易了.

更重要的是,結合VB的強大功能,更可以輕松實作本地指定路徑的檔案存儲.聯網互動功能等等.聽起來都是蠻不錯的哦.

我隻翻譯了源文檔中的與VB-FLASH互動比較關鍵的部分内容.其它内容,如果各位有興趣,可以自己琢磨琢磨.

需要Flash   4/5   的支援,安裝OCX控件  

  1)在Flash裡的Action中,對按鈕的   OnRelease()事件設定FSCommand指令  

  在FScommand的對話框裡有兩欄:Command和Argument,這裡面所填的内容将被傳到調用的函數裡作為參數。一般說來:Command欄填的是你想調用的函數名;Argument欄則填上函數使用的參數。  

  如:Command欄填“call_alert”;Argument欄填“Hello   world”    

  VB   中:  

  Sub   Flash1_DOFSCommand(Command,Argument)  

  中,可以檢查兩個參數,讓後在Show   another   windows  

  VB中有這個專門控件的  

  用VB播放Flash動畫  

  Flash是一種矢量格式的動畫檔案,可以包含動畫、聲音、超文本連結,而檔案的體積卻很小。特别适合在網頁中使用。  

  其實我們還可以用VB來實作同樣的效果:  

  打開VB5後建立一個工程,在工具箱上單擊右鍵,選擇部件,在部件視窗中選擇Shockwave   Flash,然後“确定”,Flash控件就加載到工具箱中。  

  輕按兩下Flash控件使它加載到我們的窗體中,調整一下大小,在屬性框中設定movie屬性為Flash的動畫路徑,如:C:\vbwj\a.swf,設定scale   model屬性為2,quality屬性為1。具體的屬性設定可按下表自己設定。  

  屬性  

  值  

  含義  

  Scale   model  

  全部顯示  

  1  

  随控件大小變化  

  2  

  縮放至控件大小  

  Quality  

  低分辨率  

  1  

  高分辨率  

  2  

  自動降低分辨率  

  3  

  自動升高分辨率  

  Loop  

  True  

  循環播放  

  False  

  不循環播放  

  Playing  

  Ture  

  播放  

  False  

  停止  

  Movie  

  要播放的動畫路徑  

  MenU  

  Ture  

  顯示快捷菜單(運作時,在動畫上點右鍵)  

  False  

  不顯示快捷菜單  

  輕按兩下窗體,在form_load()中加入:  

  shockwaveflash1.playing=ture  

  好了現在就可以運作了。(注意:編譯後的可執行檔案不包含動畫檔案)如果動畫裡加上動态按鈕,配合滑鼠位置判斷,就可以作成動态工具條或動态菜單了。