打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VB与FLASH的交互

ActionScript 2.0有效

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的强大功能,更可以轻松实现本地指定路径的文件存储.联网交互功能等等.听起来都是蛮不错的哦.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Flash ActiveX Based on Flash 8***??
如何做一个FLASH播放器
VB自定义控件的制作,ACTIVEX,.OCX
c# 与flash
delphi6?OCX控件的制作
DLL 组件(Component) 对象(Object) OLE控件 ActiveX控件 ...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服