加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB与FLASH的交互

发布时间:2020-12-17 00:23:51 所属栏目:大数据 来源:网络整理
导读:其实这是一个很容易解决的问题,可惜这个问题属于两不管的境遇,VB的书没有,FLASH的书也没有,对于不懂FLASH的VB写手就是个问题。 一、将FLASH插入VB窗体 在VB工程/部件的列表里找到shockwave flash,添加即可在工具栏里找到FLASH控件 ,添加之。。。 该控件

其实这是一个很容易解决的问题,可惜这个问题属于两不管的境遇,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影片中的一个动态文本框设值.如下图图片请看源文件)

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:vbwja.swf,设置scale model属性为2,quality属性为1。具体的属性设置可按下表自己设定。 属性 值 含义 Scale model 0 全部显示 1 随控件大小变化 2 缩放至控件大小 Quality 0 低分辨率 1 高分辨率 2 自动降低分辨率 3 自动升高分辨率 Loop True 循环播放 False 不循环播放 Playing Ture 播放 False 停止 Movie 要播放的动画路径 MenU Ture 显示快捷菜单(运行时,在动画上点右键) False 不显示快捷菜单 双击窗体,在form_load()中加入: shockwaveflash1.playing=ture 好了现在就可以运行了。(注意:编译后的可执行文件不包含动画文件)如果动画里加上动态按钮,配合鼠标位置判断,就可以作成动态工具条或动态菜单了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读