Visual c ++ for Flash Animation playback
Langgrui, Qingdao branch of Electronic 22nd Research Institute, Ministry of Information Industry
Abstract: By embedding an external SWF file in VC as an internal resource of VC, this article enables it to dynamically release resources to temporary files during program running, in this way, VC can play the Flash animation.
Introduction
Because Flash Animation can easily show users' Imagination Through animation, it is extremely fast and convenient to make the animation originally only belongs to professional production personnel. Because Flash animations are excellent in terms of layers, content, and forms of expression, they have developed rapidly on the Internet, many other vendors use flash to demonstrate advertisements and products on the Internet. The effect is no different from that of videos, but the size is much smaller. Flash not only has a wide range of applications on the network, but also can be used in common applications to implement special effects that are difficult to implement by programming languages such as Vc and Delphi, for example, in some demo programs, you can use flash to create the flash screen before running the program. In this article, we will use the Dynamic Release of embedded resources to play the Flash animation by VC, and provide some implementation code.
Dynamic Release of embedded Resources
Flash animation is a program module here. Although it can also be used as an external resource in the form of files, in order to avoid abnormal program running due to the loss of external modules, files in swf format pre-prepared by Flash 5.0 can be packaged into applications in the form of resources, and they can be recovered from resources to files when the program is running, after use, delete it from the disk through a program.
When importing resources, because the swf format file is not the standard resource of VC, you must specify the resource type "SWF" in the "Resource Type" column during the import ", note that quotation marks must be included here. After adding the resource to the resource view, you can see that the imported SWF resource is saved in binary format. Once added, you cannot edit it in the resource view.
Before using a SwF resource, you must dynamically release it from the application to the file to further use the resource. You can use the macro makeintresource () to convert the resource ID idr_swf to the string name, and then use the findresource () and loadresource () functions to find and load the resource to the memory:
Cstring type = "SWF ";
Hrsrc res = findresource (null, name, type );
Hglobal GL = loadresource (null, Res );
After a resource is loaded into the memory, a pointer to the address of the resource memory is returned by locking the resource memory. In this way, resources are saved from memory to disk, for disk storage operations, the file functions createfile (), and writefile () are used to complete:
Lpvoid Lp = lockresource (GL); // returns a pointer to the address of the resource memory.
Cstring filename = "temp.swf"; // saved temporary file name
// Create_always generates a new file regardless of whether the file is saved or not.
Fp = createfile (filename, generic_write, 0, null, create_always, 0, null );
Dword;
// Sizeofresource obtains the size of the resource file
If (! Writefile (FP, LP, sizeofresource (null, Res), & A, null ))
Return false;
Closehandle (FP); // close the handle
Freeresource (GL); // releases the memory.
After reading the code, you can extract and release the SWF resource from the application to the temporary file temp.swf. After that, you can only operate on this temporary file, regardless of embedded program resources.
Flash Animation playback
Flash Animation in swf format is usually mainly used on webpages. That is to say, ie can support Flash Animation playback. In this way, you do not have to write the code used to play the SWF file separately, which greatly reduces the programming workload. In VC ++ 6.0, A chtmlview class derived from cview is added for processing web pages. This class is supported by Internet Explorer in the background, therefore, when creating a project, you only need to specify the dependent class derived from chtmlview In the last step to enable the program to display web pages without compiling a line of code.
. The code for displaying the default homepage is carried out in the initialization function of the class:
Void cembedmoduleview: oninitialupdate ()
{
Chtmlview: oninitialupdate ();
Navigate2 (_ T ("http://www.microsoft.com", null, null );
}
Obviously, you need to change the first character of navigate2(temp to the storage path of temp.swf. The absolute path is not specified when the resource is released to the file. Therefore, the released resource file should be in the same directory as the application. However, if the absolute path is not specified here, the temporary file cannot be displayed. You can use the following method to obtain the absolute path of the temporary file: complete to obtain the full path of the temporary file. The following is the main code for implementation:
// Obtain the full path of the application
Char exefullpath [max_path];
Getmodulefilename (null, exefullpath, max_path );
// Format it as a string
M_tempfile.format ("% s", exefullpath );
// Remove the full name of the application (15 is the length of the full name of the application file)
Exefullpath [m_tempfile.getlength ()-15] = '/0 ';
// Obtain the Application Path
M_tempfile.format ("% s", exefullpath );
// Obtain the full path of the temporary file
M_tempfile + = "temp.swf ";
Finally, pass the full path m_tempfile of the temporary file to navigate2 () as the parameter to display the Flash animation as the home page (as shown in) when the program is running ).
Because the ghost file temp.swf is extracted from the application resources during the program running, you need to delete it before exiting the program. It is generally implemented through the deletefile () function in the response function of message wm_destory.
Summary
In this paper, the dynamic release of chtmlview and embedded resources is used to play the Flash Animation in the VC program, and the dynamic release of resources is clearly described. By using a similar method, program modules such as dynamic link library and HTML files can be embedded as resources and dynamically released to temporary files during use, this effectively avoids the clutter of too many file modules and the loss of program modules resulting in abnormal program operation. The program described in this article is compiled by Microsoft Visual C ++ 6.0 under Windows 98. Flash animation is made by Macromedia Flash 5.0. The required browser supports Internet Explorer 6.0.