5/8/2011 6:57:52 PM
Title:
Please provide script for bi-directoinal communication between SWF file (AS3) and visual basic stand
I am new to flash and visual basic. I have a menu.swf (with 7 buttons) inserted in visual basic form. What I am trying to do is when someone clicks on any button on menu swf, it calls a function in visual basic to check for presence of CD and and CD-ROM drive letter and then visual basic calls/plays the corressponding swf file. I have read about external interface but I was worndering if it can be used in stand-alone application and also whenever one talks of externalinterface, they invariably use javascript and html but there is no html or javascript in my application. COULD SOMEONE PLEASE PROVIDE ME A SAMPLE SCRIPT FOR CALLING A FUNCTION FROM FLASH (AS3) TO VISUAL BASIC APPLICATION AND FROM VISUAL BASIC APPLICATION TO FLASH? Thanks, any help wud b highly appreciated. I am tired of searching in the net and not getting it working.
5/9/2011 2:01:30 AM
you need to use fscommand for communication from VB to flash . Here is a tutorial for that
http://pages.cpsc.ucalgary.ca/~saul/vb_examples/tutorial12/index.html
5/9/2011 2:09:02 AM
external interface is used for interaction of flash when embedded in flash. In case you have embedded your flash file in VB or C# application you can use fscommand for bidirectional communication between your application and flash file. See this example
http://www.codeproject.com/KB/cs/fscommand.aspx
5/9/2011 11:49:30 AM
First of all, thank you very much for answering but I am not satisfied with the answers/links provided because I am using actionscript 3.0 (as I had mentioned in my question) but both links refer to actionscript 2.0 so they are not of much use to me. I have read that we cannot use setvariable as one of the link suggests. Can someone show a sample script where the visual basic application can pass CD drive letter to swf file (actionscript 3.0) or how to call a function in swf file from visual basic application. Thanks
5/9/2011 8:38:10 PM
I have managed to call a function in visual basic application from flash using fscommand as well as externalInterface, but I am still facing 2 problems:
1) while calling a function using externalinterface, I am able to call the function in visual basic only once (not every time I click on flash button which it should), could someone further improve this code so that I get the function called everytime I click? and
2) I am not able to call a function in flash from visual basic, can someone write working sample code in actionscript 3?
I am inserting the codes. Thanks
Insert Code here
1) Externalinterface code: code in flash file is as follows:
exInt_btn.addEventListener(MouseEvent.CLICK, flashtoVB);
function flashtoVB(event:MouseEvent):void
{
ExternalInterface.call("vbFunction");
}
visual basic code:
Private Sub AxShockwaveFlash1_vbFunction(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxShockwaveFlash1.Enter
MsgBox("function called from flash")
End Sub
COULD SOMEONE IMPROVE THIS CODE SO THAT IT IS CALLED EVERYTIME THE FUNCTION IS CALLED/EVERYTIME THE BUTTON IN FLASH IS CLICKED?
2) fscommand example:
exit_btn.addEventListener(MouseEvent.CLICK, closeProjector);
function closeProjector(event:MouseEvent):void
{
fscommand("exit", "ok");
Private Sub AxShockwaveFlash1_FSCommand(ByVal sender As Object, ByVal e As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles AxShockwaveFlash1.FSCommand
If e.command = "exit" Then
Me.Close()
End If
End Sub
This one is working perfectly.
}
5/10/2011 4:57:34 AM
Your flash code looks fine. But I don't remember VB now used it with flash many years back. I hope this is exactly what you require, As3.0 to VB communication.
http://nitin-sinha.blogspot.com/2009/07/visual-basic-and-as30-communication.html
5/11/2011 9:18:24 PM
Thank you so much, it certainly helped. I had a code which was giving error like HRESULT E_FAIL etc., but after reading the link I changed allowscriptaccess to always and it worked, although I am facing new problems. My code is as follows:
ACTIONSCRIPT CODE:
part1_btn.addEventListener(MouseEvent.CLICK, getFromVbNet);
function getFromVbNet(event:MouseEvent):void
{
fscommand("getting","");
}
function FlashFunction(o:Object)
{
var r:String = "";
// display the arguments we were passed
for (var v in o)
r += v + o[v] + "\r";
myText.text= r;
trace(r); // sent from VB (cdDriveLetter)
SoundMixer.stopAll();
var myrequest:URLRequest=new URLRequest(r + "anim/part1a.swf");
myloader.load(myrequest);
stage.addChild(myloader);
return "really FlashFunction was called"; // this goes back to VB
}
if (ExternalInterface.available)
ExternalInterface.addCallback('FlashFunction', FlashFunction);
Visual basic code:
Private Sub AxShockwaveFlash1_FSCommand(ByVal sender As Object, ByVal e As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles AxShockwaveFlash1.FSCommand
If e.command = "getting" Then
Dim x As String
Dim r As String
x = ""
x = x & "<invoke name=""FlashFunction"" returntype=""xml"">"
x = x & "<arguments>"
x = x & "<object>"
x = x & "<property id=''><string>" + ReturnFirstCDRomName() + "</string></property>"
x = x & "</object>"
x = x & "</arguments>"
x = x & "</invoke>"
r = AxShockwaveFlash1.CallFunction(x)
The problem that I am facing is how to write the following code to fire multiple FlashFunctions to load swf on rest of the buttons. How can I write the following code to load swf corresponding to rest of buttons.
Dim x As String
Dim r As String
x = ""
x = x & "<invoke name=""FlashFunction"" returntype=""xml"">"
x = x & "<arguments>"
x = x & "<object>"
x = x & "<property id=''><string>" + ReturnFirstCDRomName() + "</string></property>"
x = x & "</object>"
x = x & "</arguments>"
x = x & "</invoke>"
r = AxShockwaveFlash1.CallFunction(x)
5/12/2011 5:31:44 AM
@sanjay you can either declare multiple functions in flash and call then from VB on click of different buttons. For this in actionscript declare add call back like this
function FlashFunction1(o:Object)
{//swf load code here for button 1}
function FlashFunction2(o:Object)
{//swf load code here for button 2}
ExternalInterface.addCallback('FlashFunction1', FlashFunction1);
ExternalInterface.addCallback('FlashFunction2', FlashFunction2);
similarly in VB on button click call the function with there respective names in xml nodes
"<invoke name=""FlashFunction1"" returntype=""xml"">"
"<invoke name=""FlashFunction2"" returntype=""xml"">"
5/13/2011 10:09:39 AM
I want to thank flash_guy, Bob_senior and troy for helping me, guiding me. I have been able to to do what I have set out for, but a small problem still remains, to make a flash swf full screen all one has to do is to write fscommand ("fullscreen" true); and the swf nicely expands but I am facing problem trying to full screen flash contained in visual basic application. I have used the following code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
AxShockwaveFlash1.ScaleMode = 0 'ShowAll
End Sub
and the following code:
Private Sub Form_Resize()
On Error Resume Next
AxShockwaveFlash1.Width = Me.ScaleWidth
AxShockwaveFlash1.Height = Me.ScaleHeight
AxShockwaveFlash1.Left = 0
AxShockwaveFlash1.Top = 0
End Sub
The problem I am facing are:
1) Height increases disproportionately, I have exit button at the right top corner and play, pause rewind buttons at the bottom, both are only partially visible. What can I do to set it right? what can I do reduce its height.
2) Also, it is not perfectly at center like with fscommand ("fullscreen", "true") in flash. There is more vacant/empty space on right side than on left side of the flash movie.
3) I am getting this error: 'ScaleWidth' is not a member of 'myapplication.form1'. 'ScaleHeight' is not a member of 'myapplication.form1'. Any suggestion would be highly appreciated.
5/14/2011 12:02:33 AM
there are only 4 types of scale modes that are available in flash. You can check the detail of other modes info in this already answered question
http://askmeflash.com/qdetail/505/to-get-floating-structure-in-flex
Check the answer by Inder he has defined limitations and effects of all the modes.