Using Flash related in MFC

Source: Internet
Author: User
Tags rewind strcmp visual studio 2010

Originated from http://my.oschina.net/ypimgt/blog/62573

First, the preparatory work

First step : Download and install Adobe Flash Player.

Download the latest Flash Player (approximately 2.66M) from the official website (http://get.adobe.com/cn/flashplayer/) and install it. For WINDOWS 32-bit systems, the default installation directory is: C:\WINDOWS\system32\Macromed\Flash\; for 64-bit systems: C:\Windows\SysWOW64\Macromed\Flash.

The large version number of the Flash control is represented by a number, such as 9,10,11, and the minor version number is indicated in lowercase letters, such as A, B, C, D, E. If the version I installed is: Flash10l.ocx.

Step two : Register the Flash control flash10l.ocx with Regsvr32.exe.

From the Start menu, open the Run dialog box and enter:

regsvr32 C:\Windows\SysWOW64\Macromed\Flash\Flash10l.ocx Register the control (you can add controls to the project after registration), if the registration is successful, you will be prompted as follows:

Unregister control's command behavior

Regsvr32/u C:\Windows\SysWOW64\Macromed\Flash\Flash10l.ocx.

Second, new MFC project, add Flash Control

The first step : Create a dialog-based MFC program.

Open Visual Studio 2010, create a new project, select MFC application, and name it Mfcflash.

In the next selection settings, select the application type is dialog based, others can be set by default. After the project is completed, the program adds the Mfcflash and Mfcflashdlg header files and implementation files by default, as follows:

Step Two : Add the Shockwave Flash object class.

VC + + 6.0, you can use ClassWizard to add ActiveX controls, and VS2005 later versions do not. If you want to add a class for an ActiveX, you can open the Add Class from ActiveX Control Wizard dialog box after you select the project in Solution Manager and then, in the menu, click project → add Class → MFC class in ActiveX control. Choose Add Class from "file", find the installation location of Flash control, add Shockwave Flash Control, interface select "Ishockwaveflash" to generate Cshockwaveflash class (the name of the class can be modified). After inserting, VC will automatically put this class into the project.

Step three : Add the Shockwave Flash Object control.

Find "Dialog" in the Resource View and select the Idd_mfcflash_dialog in it, right click on it, select "Insert ActiveX Control" and select "Shockwave Flash Object" from the list that pops up.

Like other Windows controls such as button, edit, we can use the member functions of this class to manipulate the object, to control the Flash animation and to interact with its own program. Right-click on the Flash control and select "Add Variable" and set "variable name" as: M_flashplayer. You can right-click on the Flash control again and select Properties to change its ID to Idc_shockwaveflash.

Fourth Step : Use the member function Loadmovie and play to import and play the animation.

In the MFCFlashDlg.cpp file, locate the Cmfcflashdlg::oninitdialog () function and add the code before "return TRUE":

CString str = _t ("e:\\vs\\flash\\media\\test.swf");

M_flashplayer.loadmovie (0, str);

M_flashplayer. Play ();

where M_flashplayer.loadmovie (0, str) is used to import the animation program, STR for the Flash path to play, to use absolute path (note that the path is separated by a double slash "\ \"); M_flashplayer. Play () starts to play the animation.

Relative path to absolute path:

wchar_t Szpath[max_path];

memset (szpath, 0, sizeof (CHAR) *max_path);

GetCurrentDirectory (MAX_PATH, szpath);

StrCat (szpath, _t ("\ \ Fan. swf"));

Now run the program, you can see flash embedded in the program. There are several commonly used functions that can be used to control the playback of an animation, such as Gotoframe (long framenum) can go to Framenum to play (starting from 0), Stopplay () can stop playing ... , there are many, you can try it yourself. I made a flight meter, the interface is ugly, make it easy to see ^_^

Third, the realization of MFC and Flash communication

1, flash through Fscommand message call MFC

The first step: Add a message handler function.

Right-click on the Flash control and select "Add Event handler", select "Cmfcflashdlg" in the class list, and the message type select "FSCommand", the function handler name is automatically "Fscommandshockwaveflash". It is probably in the form of:

void Cmfcflashdlg::onfscommandshockwaveflash (LPCTSTR command, LPCTSTR args); the function has two parameters, which is the action of Flash The two parameters in the Fscommand statement in the script. In fact, not necessarily two parameters are used, flash script can be used in a parameter, so that the function on the side as long as the first parameter processing on the line.

Step Two: Write the message processing code.

In the Fscommand message handler function that you just added, two parameters are processed. In fact, it is the operation of the string comparison, according to what is the string to determine what the user is doing. This is probably the case:

void Cplayflashdlg::onfscommandshockwaveflash1 (LPCTSTR command, LPCTSTR args)
{
Todo:add your control notification handler code here
if (0 = = strcmp (Command, "BT"))
{
if (0 = = strcmp (args, "enter"))
{
MessageBox ("Welcome into the system!") ”);
}
}
else if (0 = = strcmp (Command, "quit"))
{
MessageBox ("You have chosen to quit!") ”);
Cdialog::oncancel ();
}
}

This is just the simplest form of processing, of course, you can send complex strings in flash, and you need to do more processing here.

2. MFC calls Flash via CallFunction function

The first step: Flash in the externalinterface.addcallback with processing.

In Flash, use Externalinterface.addcallback to register functions that can be called externally, for example: Externalinterface.addcallback ("Setalt", Setalt);

function Setalt (para:string = "3150")

{

Alt.text = para; Alt is a dynamic text in the SWF file

}

Step Two: MFC uses the CallFunction function to pass in parameters.

You want MFC to actively send content to flash and let flash respond, using the CallFunction method of the control. In MFC, call the CallFunction method, pass in a more complex string, describe the name of the function to be called, parameters, and so on, in the form of XML rendering. For details, refer to the "XML format for external APIs" article of the Flash Help. The arguments passed here callfunction are in XML format, as follows:

CString temp = _t ("<invoke name=\" setalt\ "returntype=\" xml\ ">\

<arguments><string>3500ft</string></arguments>\

</invoke> ");

M_flashplayer.callfunction (temp);

Note that,<arguments> must be a complete line, and I'm not sure why. Run the program again and you can see that the value of Alt has changed from 3280FT to 3500FT.

The corresponding class of the Shockwave Flash Object control is the Cshockwaveflash class, a common function of the class:

Play () play animation Stopplay () Stop animation isplaying (): Whether the animation is playing (True,false)

Gotoframe (Frame_number) jump to a frame (frame_number+1)

Totalframes () Gets the total number of animation frames Currentframe () callback the current animation frame number-1

Rewind () causes the animation to return to the first frame Setzoomrect (left,top,right,buttom) to enlarge the specified area

Zoom (percent) changes the animation size pan (x_position,y_position,unit) pans The animation in the X, y direction

Percentloaded () returns the percentage of animation loaded (0-100)

Loadmovie (level_number,path) Loading animations

Tgotoframe (Movie_clip,frame_number) Movie_clip jump to the specified number of frames
Tgotolabel (muvie_clip,label_name) Movie_clip jump to the specified label
Tcurrentframe (Movie_clip) callback Movie_clip Current frame-1

Tcurrentlabel (Movie_clip) Callback Movie_clip current tab

Tplay (movie_clip) play Movie_clip Tstopplay (movie_clip) Stop movie_clip playback

GetVariable (variable_name) Get variable setvariable (variable_name,value) variable assignment

Tcallframe (movie_clip,frame_number) Call action on the specified frame

Tcalllabel (Movie_clip,label) Call the action on the specified label

Tgetproperty (movie_clip,property) Gets the specified property of the Movie_clip

Tsetproperty (movie_clip,property,number) sets the specified properties of the Movie_clip

Description of Cshockwaveflash controls in MFC

The Acticex control used by Flash in MFC plays Shockwave Flash Object, and its corresponding class is Cshockwaveflash. The following is a description of the common properties and methods for the control:

==================================
Description of the related properties and methods of the Flash Player control.
==================================

++++++++++++++++++++++++
++++++++++ Property ++++++++++
++++++++++++++++++++++++
Syntax: Alignmode as Long
Description: Alignment (linked to Salign property). The position that the movie appears in the control can be adjusted when the control's aspect ratio is inconsistent with the movie and Wmode is not exactfit.
Property values are aligned with the corresponding alignment:
1: Align Left
2: Right-justified
4: Top-aligned
8: Bottom Alignment
Note: You can also combine the various alignments by adding the four base values together. For example, align both left and top, the property value is set to 5.

Syntax: BackgroundColor as Long
Description: The background color of the movie. The default movie background color is-1. If the movie has an undertone or a picture as the background, then you can't see how the change in the property value will affect it.
Note: Color values use RGB-formatted color values

Syntax: Base as String
Description: Specifies the base address that is used to resolve claims for all relative paths in the movie. This property is particularly useful when the movie is not in the same directory as the other files it needs. If not specifically specified, the value of base defaults to ".", which is the path of the current movie.

Syntax: BGColor as String
Description: The background color of the movie. Unlike BackgroundColor, bgcolor is a six-bit hexadecimal number, each of which represents a red-green-blue color value. For example, FFEEAA means that the R value is ff,g value is a value of AA.

Syntax: Devicefont as Boolean
Description: Determines whether to use fonts embedded in the movie, and the default value is false. Setting this property value to true forces the player to use the Local system font without using the fonts embedded in the movie.

Syntax: Embedmovie as Boolean
Description: Whether the movie is stored in the container in which the control resides. When you've loaded a movie, set this property to True, and you don't have to read the SWF file when you play the movie. This allows the SWF file to be inserted into the program without having to read the file again. But when this property is set to True, The movie property of the control no longer accepts new values. To play another movie (assigning a new value to the movie property), you must first set the property (Embedmovie) to false.


Syntax: Framenum as Long
Description: The number of the current frame of the movie (counting from 0). Setting this property value will cause the movie to stop at the frame specified by Framenum.
Note: Not only can you get the current frame, you can also set the current frame, that is, jump to a frame

Syntax: Loop as Boolean
Note: Controls whether the movie loops. Set to True to loop, set to False to play only once.

Syntax: Menu as Boolean
Description: Whether the menu is displayed. Set to True to show all menus, set to false the menu is masked, but there is still a "about Macromedia Flash Player ..." That will open the Macromedia website when clicked. If you really don't like this menu, The goal should be achieved by intercepting the mouse messages in the program.

Syntax: Movie as String
Description: The movie path (URL) to play. Set this property to the URL of a SWF file, and when the property is set, the control will automatically load the file and play it.

Syntax: Playing as Boolean
Description: The current playback state. If the movie is playing, the property value is true, otherwise false.

Syntax: Quality as Long
Description: Screen quality.
The properties of the quality can be taken:
0: Equivalent to Quality2 take "low"
1: Equivalent to Quality2 take "high"
2: Equivalent to Quality2 take "Autolow"
3: Equivalent to Quality2 take "Autohigh"

Syntax: Quality2 as String
Description: Picture quality
The properties of the quality can be taken:
Low: Focuses on playback speed regardless of display effect and does not enable anti-aliasing.
High: Focus on the screen regardless of playback speed, and always enable anti-aliasing. If the movie does not contain animation, the bitmap is smoothed, and if there is an animation, the bitmap is not smoothed.
Autolow: Focus on playback speed first, but improve the display whenever possible. Disables anti-aliasing first when it starts playing. If the player detects that the processor is capable of withstanding, enable anti-aliasing.
Autohigh: Start with the playback speed and display effect, but sacrifice picture quality to ensure speed if necessary. Enables anti-aliasing when you start playback. However, if the actual frame rate is slower than the rate specified at design time, anti-aliasing is disabled to improve playback speed.

Syntax: ReadyState as Long
Description: The current state of the movie.
The values of the readystate are:
0: Loading in
1: not initialized
2: Loaded
3: Interacting
4: complete Example
Note: You can use this property to roughly determine the SWF file's read progress, but if you want to get more accurate judgment, you should use Fscommand () in the SWF file to achieve the interaction and contact with VB, that is, to send data to VB.

Syntax: Salign as String
Description: The alignment mode is the same as the alignmode above, but the values are in a different form.
Desirable values:
L: Align Left
T: Top-aligned
R: Right Justified
B: Bottom-aligned
Note: When using the combined alignment method, the order of the l,t,r,b cannot be changed, such as: Left and bottom alignment: salign= "LB"

Syntax: Scale as String
Description: Controls the zoom mode of the movie.
The value of the scale can be taken:
ShowAll: Displays the entire movie area within the control, keeping the movie's length-to-width ratio intact, and the size of the movie depends on the smaller side of the control's length or width.
Noborder: Displays part of the movie area within the control, keeping the movie's length-to-width ratio constant, and the size of the movie depends on the length of the control or the larger side of the width.
Exactfit: Displays all movie areas within the control, regardless of the length-to-width ratio of the movie, forcing the length of the movie to be equal to the control's length width.

Syntax: ScaleMode as Long
Description: The zoom mode is the same as scale, except that the property value is a number.
ScaleMode can take:
0: Equivalent to scale take "ShowAll"
1: Equivalent to scale take "Noborder"
2: Equivalent to scale take "Exactfit"

Syntax: Totalframes as Long
Description: Returns the total number of frames in the movie. This parameter is valid until the movie loading is complete, i.e. readystate=4

Syntax: WMode as String
Description: The window mode of the control (implementation of vector graph in VB interface display of important properties).
Wmode can take:
The default value of the Window:wmode property, which works as a typical Flash player, is to play the movie in the control's rectangular window, which generally provides the fastest animation effect.
Opaque: Make the movie opaque.
Transparent: Create a transparent movie, and if you have a transparent clip in the movie, you can see the background underneath the control. But with this property value, the animation may play slower.


++++++++++++++++++++++++
++++++++++ Method ++++++++++
++++++++++++++++++++++++
Play ()//Play Animation

Stopplay ()//Stop animation

IsPlaying ()//Whether the animation is playing (True,false)

Gotoframe (frame_number)//Jump to a frame (frame_number+1)

Currentframe ()//return current animation frame number-1

Rewind ()//To return the animation to the first frame

Setzoomrect (Left,top,right,buttom)//Enlarge specified area

Zoom (Percent)//Change animation size

Pan (x_position,y_position,unit)//Make animation pan in X, y direction

Percentloaded ()//returns the percentage of animation loaded (0-100)

Loadmovie (Level_number,path)//Load animation

GetVariable (variable_name)//Get variable

SetVariable (variable_name,value)//variable assignment

Totalframes ()//Gets the total number of frames in the animation

Tplay (Movie_clip)//Play Movie_clip

Tstopplay (Movie_clip)//stop Movie_clip Playback

Tgotoframe (Movie_clip,frame_number)//Movie_clip jump to the specified number of frames

Tgotolabel (Muvie_clip,label_name)//Movie_clip jump to the specified label

Tcurrentframe (Movie_clip)//backhaul Movie_clip Current Frame-1

Tcurrentlabel (Movie_clip)//Callback Movie_clip current tab

Tcallframe (Movie_clip,frame_number)//Call action on the specified frame

Tcalllabel (Movie_clip,label)//Call action on the specified label

Tgetproperty (Movie_clip,property)//Get Movie_clip specified property

Tsetproperty (Movie_clip,property,number)//Set the specified property of the Movie_clip

Using Flash related in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.