Handshaking between Flash and Visual Basic

 From the age of Command User Interface-based (CUI) UNIX, the IT world has treaded a long way to the world of Windows and Mac, where each piece of software must be accompanied with marvelous GUI. Obviously, creating bug-free software is the primary criteria for a good developer, but providing a good eye-catching interface to it is called as “professional touch”.

Developer studios like VB and VC++ provide numerous options for generating a decent interface. However, after a certain level, one needs some advanced knowledge of the language. On the other hand, designing GUI with multimedia tools like Flash is much easier. For a new developer, it will be nice if you can design some GUI using Flash and incorporate the same in your software using VB or VC++. We will see how simple handshaking between Flash and VB can serve this purpose.

To keep the article short and brief we are going to modify the Smart File Splitter program provided by Chetankumar Digambarrao Akarte (thanks to him!) in the DIQ issue of September 2005; just by replacing the GUI and making it more smarter!

Design

For communication between Flash and Visual Basic, you need to install Macromedia Flash MX 2004 or a later version and Microsoft Visual Basic 6.0. Now, first you need to create a good GUI using Flash. For that, create a new Flash document (in our case Interface.fla) as you want or simply use some template given in the Flash studio itself. We just used the Quiz template_style1 to design the GUI shown in figure 1.




Here we used buttons from the Flash library (go to windows -> other panels -> common library -> buttons).  If you are familiar with Flash, just add buttons and text boxes in the Flash document in different layers as required on your own. For “Browse” and “Save” buttons we have used a small clipart, (copy any clipart and paste on the Flash doc.) which is to be converted into a button (select the image and convert it into a button by pressing F8). After creating the buttons, you need to specify the instance name for each button. To specify instance name, click on a button and then go to the properties menu as shown in figure 2.



Fig 2. Adding instance name

For example, click the Help button and go for the properties, in the instance text box, write the instance name (here we have given hlpBtn as instance name). See figure 2. Similarly, add instance names abtBtn, spltBtn, exitBtn, Save, Browse to About, Split, Exit, Save and Browse buttons, respectively. After creating the text boxes, you need to specify the variable name for each text box. To specify variable name, click on a text box and then go to the properties menu as shown in figure 3. E.g. variable name for the text box save should be given as save.



Fig 3. Adding variable name to textbox


Handshaking from the part of Flash

After naming each control, you need to write some action script. Create a new layer and name it as Action (Insert à Timeline à layer). Click frame number 1 and open the Action Script editor. Refer figure 4.


Fig 4. Writing action script


Write the following code 1 on the action script editor.

Code 1

Browse.onPress=function(){
fscommand("browse clicked");
}

Save.onPress=function(){
fscommand("save clicked");
}
hlpBtn.onPress=function(){
fscommand("help clicked");
}
abtBtn.onPress=function(){

fscommand("about clicked");
}

spltBtn.onPress=function(){
fscommand("split clicked");
}

exitBtn.onPress=function(){
fscommand("exit clicked");
}

Designing your interface is over! Publish the movie and copy Interface.swf file to the directory where you are going to create your Visual Basic project. It is to be noted that only .swf files can communicate with visual basic, not the .exe or any other Flash file extensions.

Handshaking from the part of VB

Now, create a new Visual Basic project (in this case we have opened the smart splitter.vbp project). To communicate with Flash, we have to add an activeX component in our project, called shockwaveflash. To get the component, open the Project menu and go to component option. A new window is opened where you have to check the shockwave Flash checkbox as shown in figure 5.



Fig 5. Adding Shockwave component to VB

Note:
If you did not see the Shockwave Flash in the component window, it means that Flash player is not properly installed.

Now press Apply button and close the component window. In the toolbox window, you will see a new component called ShockwaveFlash. Drag the control to your main VB Form. 

In the form Load event, add the following code.

ShockwaveFlash1.Movie = App.Path + "\interface.swf"

In Option Explicit, declare two string type variables “openfile” and “savefile”:

Dim openfile As String
Dim savefile As String

As we are using the smart file splitter program, the two text boxes “openfile” and “savefiles” are being removed and we have replaced all the “openfile.text” and “savefile.text” commands with “openfile” and “savefile”. 

In FSCommand procedure of the shockwaveflash1 control, add code 2.

Code 2

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

If command = "browse clicked" Then
cmdopen_Click
ElseIf command="save clicked" Then
cmdsave_Click
ElseIf command="help clicked" Then
cmdhelp_Click
ElseIf command="about clicked" Then
cmdabout_Click
ElseIf command="split clicked" Then
cmddoit_Click
ElseIf command="exit clicked" Then
cmdexit_Click
End If
End Sub

Acknowledgement

We are thankful to Chetankumar Digambarrao Akarte for his small and beautiful VB file splitter application. To make the article short and brief, we have just modified the Smart File Splitter program provided by him. We have deleted all the controls, which are not needed any more. Some codes have been modified, which we have properly documented. For simplicity, we have not changed the function/procedure names.

Authors:

Tirthankar Dasgupta is working as a Research Assistant with IIT, Kharagpur. He is available at: iamtirthankar@gmail.com. Sumit Das is an M. Tech. student studying at IIT, Kharagpur. He can be contacted at: sumit.das@gmail.com.




Added on July 3, 2007 Comment

Comments

Post a comment