Add Displaying Macromedia Flash .SWF files in your Delphi Application!


Related Links
Removing the popup menu in Macromedia Flash .OCX
Macromedia Flash Resizing

Macromedia Method descriptions for ActiveX Control

If you have considered using your Macromedia Flash files to help add some flavor to your Delphi applications, but never knew how, this article is for you.

Download demo project

First of all you must find the file titled

"SWFLASH.OCX" usually in the C:\Windows\System\Macromed\Flash directory.

You can then in Delphi click on the menu Component --> Import ActiveX Control. Choose the SWFlash.OCX file and import it. Once you install it, you will then have the TShockWaveFlash component to drag onto your form.

PROBLEM: The important thing to remember is that if deploy your application then they must have this OCX file or it will not run.

SOLUTION: To keep your program simple and only have 1 file to distribute, I would suggest to create a resource file of the OCX and include in your .EXE. Extract the file if needed and register it. I have included some sample code on how to do this.

CREATING RESOURCE FILE: (Skip this section if you already understand it). Create a new Text File in Notepad. Type the following line:

Flash RCDATA "SWFLASH.OCX"

Make sure the text file is saved as Filename.rc, I saved mine as FlashOCX.rc. You must also have the .rc file in the same directory as the SWFLASH.OCX file in order for this to work.

Open your command prompt (DOS window) and find the brcc32.exe file in your bin directory wherever you installed Delphi. Type this and execute it in DOS:

Brcc32 DirofFile\FlashOCX.rc
Example: Brcc32 c:\windows\system\flashocx.rc

It should now have created a file titled "FLASHOCX.RES". You can now include this into your application.

{$R *.RES}
{$R FLASHOCX.RES}

The R Directive tells your program to include that Resource File.

HOW TO EXTRACT AND REGISTER:

First off we need to check to see if we can play the Flash File or not. Click on Project --> View Source (in Delphi 5) and pull up the project source code. We want to check for the EOleSysError Message when creating the first form. If we encounter the error, then we know we must register the OCX on that particular machine.

uses comobj

begin
Application.Initialize;
try
Application.CreateForm(TForm1, Form1);
except
On EOleSysError Do
begin
//Register OCX File because not found.
end;
end;
Application.Run;
end.

This next bit of source code that I will display will give you what is needed to extract the resource file and place into the Windows System Directory.

uses
windows, classes, sysutils

var
aSystemDirZ : array[0..2047] of Char;
fSystemDir : String;

...

GetSystemDirectory ( aSystemDirZ, 2047 );
fSystemDir := aSystemDirZ;

ResStream := TResourceStream.Create(0, 'Flash', RT_RCDATA);
try
FileStream := TFileStream.Create(fSystemDir+'SWFLASH.OCX', fmCreate);
try
FileStream.CopyFrom(ResStream, 0);
finally
FileStream.Free;
end;
finally
ResStream.Free;
end;

ENTIRE CODE:

You must still register the OCX file, and I will now display the entire code so you can see how it would all fit together.

  1. program FlashPlayer;
  2.  
  3. uses
  4.   Forms, Dialogs, comobj, windows, classes, sysutils,
  5.   uMain in 'uMain.pas' {Form1};
  6.  
  7. {$R *.RES}
  8. {$R FLASHOCX.RES}
  9.  
  10. type
  11.   TRegFunc = function : HResult; stdcall;
  12.  
  13. function WinExecAndWait32( FileName: String; Visibility  : Integer ) : Cardinal;
  14. var { by Pat Ritchey }
  15.     zAppName     : array[0..512] of char;
  16.     zCurDir      : array[0..255] of char;
  17.     WorkDir      : String;
  18.     StartupInfo  : TStartupInfo;
  19.     ProcessInfo  : TProcessInformation;
  20. begin
  21.     StrPCopy( zAppName, FileName );
  22.     GetDir  ( 0,        WorkDir  );
  23.     StrPCopy( zCurDir,  WorkDir  );
  24.  
  25.     FillChar( StartupInfo, Sizeof( StartupInfo ), #0 );
  26.  
  27.     StartupInfo.cb          := Sizeof( StartupInfo );
  28.     StartupInfo.dwFlags     := STARTF_USESHOWWINDOW;
  29.     StartupInfo.wShowWindow := Visibility;
  30.  
  31.     if ( not CreateProcess( nil,
  32.                             zAppName, { pointer to command line string }
  33.                             nil, { pointer to process security attributes }
  34.                             nil, { pointer to thread security attributes }
  35.                             false, { handle inheritance flag }
  36.                             CREATE_NEW_CONSOLE or { creation flags }
  37.                             NORMAL_PRIORITY_CLASS,
  38.                             nil, { pointer to new environment block }
  39.                             zCurDir, { pointer to current directory name }
  40.                             StartupInfo, { pointer to STARTUPINFO }
  41.                             ProcessInfo ) ) then
  42.     begin
  43.         Result := $FFFFFFFF; { pointer to PROCESS_INF }
  44.         MessageBox( Application.Handle, PChar( SysErrorMessage( GetLastError ) ), 'Yipes!', 0 );
  45.     end
  46.     else
  47.     begin
  48.         WaitforSingleObject( ProcessInfo.hProcess, INFINITE );
  49.         GetExitCodeProcess ( ProcessInfo.hProcess, Result   );
  50.         CloseHandle        ( ProcessInfo.hProcess           );
  51.         CloseHandle        ( ProcessInfo.hThread            );
  52.     end;
  53. end;
  54.  
  55. var
  56.   aSystemDirZ : array[0..2047] of Char;
  57.   aShortPath  : array[0..2047] of Char;
  58.   fSystemDir  : String;
  59.   aCommand    : String;
  60.   aHandle     : Cardinal;
  61.   aFunc       : TRegFunc;
  62.   ResStream   : TResourceStream;
  63.   FileStream  : TFileStream;
  64. begin
  65.  
  66.   GetSystemDirectory ( aSystemDirZ, 2047      );
  67.   fSystemDir := aSystemDirZ;
  68.   Application.Initialize;
  69.   try
  70.   Application.CreateForm(TForm1, Form1);
  71.   except
  72.     On EOleSysError Do
  73.     begin
  74.       ResStream := TResourceStream.Create(0, 'Flash', RT_RCDATA);
  75.         try
  76.           FileStream := TFileStream.Create(fSystemDir+'SWFLASH.OCX', fmCreate);
  77.           try
  78.             FileStream.CopyFrom(ResStream, 0);
  79.           finally
  80.             FileStream.Free;
  81.           end;
  82.         finally
  83.           ResStream.Free;
  84.         end;
  85.  
  86.       try
  87.         {Register the OCX File}
  88.         aHandle := LoadLibrary( PChar( fSystemDir+'SWFLASH.OCX' ) );
  89.         if ( aHandle >= 32 ) then
  90.         begin
  91.             aFunc := GetProcAddress( aHandle, 'DllRegisterServer' );
  92.             if ( Assigned( aFunc ) = TRUE ) then
  93.             begin
  94.                 GetShortPathName( PChar( fSystemDir+'SWFLASH.OCX' ), aShortPath, 2047 );
  95.                 aCommand := Format( '%s
  96. egsvr32.exe /s %s', [fSystemDir, aShortPath] );
  97.                 WinExecAndWait32( aCommand, SW_HIDE );
  98.             end;
  99.             FreeLibrary( aHandle );
  100.         end;
  101.  
  102.         //Try Creating the Form Again
  103.         try
  104.           Application.CreateForm(TForm1, Form1);
  105.         except
  106.           ShowMessage('Unable to find Macromedia Shockwave Flash.');
  107.         end;
  108.  
  109.       except
  110.         ShowMessage('Unable to register Macromedia Shockwave Flash.');
  111.       end;
  112.         {End of Registering the OCX File}
  113.     end;
  114.   end;
  115.   Application.Run;
  116. end.


END OF PROJECT SOURCE

HOW TO USE ACTIVEX CONTROL:

Properties

ReadyState (get only) - 0=Loading, 1=Uninitialized, 2=Loaded, 3=Interactive, 4=Complete.

TotalFrames (get only) - Returns the total number of frames in the movie. This is not available until the movie has loaded. Wait for ReadyState = 4.

FrameNum (get or set) - The currently displayed frame of the movie. Setting this will advance or rewind the movie.

Playing (get or set) - True if the movie is currently playing, false if it is paused.

Quality (get or set) - The current rendering quality (0=Low, 1=High, 2=AutoLow, 3=AutoHigh). This is the same as the QUALITY parameter.

ScaleMode (get or set) - Scale mode (0=ShowAll, 1= NoBorder, 2 = ExactFit). This is the same as the SCALE parameter.

AlignMode (get or set) - The align mode consists of bit flags. (Left=+1, Right=+2, Top=+4, Bottom=+8). This is the same as the SALIGN parameter.

BackgroundColor (get or set) - Override the background color of a movie. An integer of the form red*65536+green*256+blue use -1 for the default movie color.

Loop (get or set) - True if the animation loops, false to play once. Same as the MOVIE parameter.

Movie (get or set) - The URL source for the Flash Player movie file. Setting this will load a new movie into the control. Same as the MOVIE parameter.

Methods

Play() - Start playing the animation.

Stop() - Stop playing the animation.

Back() - Go to the previous frame.

Forward() - Go to the next frame.

Rewind() - Go to the first frame.

SetZoomRect(int left, int top, int right, int bottom) - Zoom in on a rectangular area of the movie. Note that the units of the coordinates are in twips (1440 units per inch). To calculate a rectangle in Flash, set the ruler units to Points and multiply the coordinates by 20 to get TWIPS.

Zoom(int percent) - Zoom the view by a relative scale factor. Zoom(50) will double the size of the objects in the view. Zoom(200) will reduce the size of objects in the view by one half.

Pan(int x, int y, int mode) - Pan a zoomed in movie. The mode can be: 0 = pixels, 1 = % of window.

Events

OnProgress(int percent) - Generated as the Flash Player movie is downloading.

OnReadyStateChange(int state) - Generated when the ready state of the control changes. The possible states are 0=Loading, 1=Uninitialized, 2=Loaded, 3=Interactive, 4=Complete.

FSCommand(string command, string args) - This event is generated when a GetURL action is performed in the movie with a URL and the URL starts with "FSCommand:". The portion of the URL after the : is provided in command and the target is provided in args. This can be used to create a response to a frame or button action in the Shockwave Flash movie.

For further information see the Macromedia Flash Website

In order to include your Flash files into your Delphi Application, just type in the directory of the .SWF file, then make "Embed Movie" = True, and it will be included in your file and not look at the Movie parameter any longer.
Related Discussions
  • HOW CAN I DEACTIVE DIAL-UP CONECTION? (2001-01-28 13:54:30)
    A guy named Ruben recently gave me this answer when I asked nearly the same question......... ----------------------------------------------------...
  • MORE EXPLAIN (RUN AN APPLICATION BEFORE ALL OTHERS) (2001-01-26 17:11:58)
    There are other ways... You can make a boot disk. Anyway, your install program will be a DOS program. BUT, if you put your program in RunServices,...
  • CREATING SHORTCUTS (2001-01-28 03:16:27)
    This is a sample from the MSDN, I tried to translate it into Delphi. I did not try to execute it but it should work. If it won't, tell me and I'll...
  • DELETING THE EXEFILE... (2001-01-28 06:05:04)
    function CreateTempFileName: string; var TempDir: array of Char; count, len: LongInt; dummy, sec: Word; start: string; begin...
  • DON'T APPEAR IN TOOLBAR!!! (2001-01-28 02:59:37)
    Another possibility is to set the extended window style of the application (!) window to WS_EX_PALETTEWINDOW: SetWindowLong(Application.Handle,...
  • PLEASE HELP IMAGE DOWNLOADER... (2001-01-28 19:17:26)
    Hey! Ok... Here's a sample application. I made a sample unit for you and it works and does just what you need. Here's the code: unit Unit1;...
  • HOW TO DISCONNECT FROM THE INTERNET IN DELPHI 5 (2001-01-28 13:55:14)
    A guy named Ruben recently gave me this answer when I asked nearly the same question......... ----------------------------------------------------...
  • DETECTING SHADES OF COLOURS (GRAYSCALE) (2001-01-31 06:54:41)
    I'm Russian but I live in Belgium, so i'm not teh best in English too :) The way you calculate the greyscale color is the wrong one. red >...
  • STOPPING AUTO-FOCUS ON MDI CHILD CREATE (2001-01-29 09:55:39)
    I do not know how to fix your problem, but why do you not use threads if you only do calculations? Threads will be much more efficient. Stephan
  • NOBODY CAN HELP ME???? (2001-01-30 10:03:06)
    Here the link to Deborah Pate's sample projects using bookmarks and properties: http://www.djpate.freeserve.co.uk/Bookmarks.zip visit her...
Latest News
Submit News Form Past News
Latest Forum Entries