File operations-read from Windows resource files

Source: Internet
Author: User

=============== read =============== for Windows resource filesWindows Resource Action functionsloadaccelerators loading shortcut keys Resources
LoadBitmap Loading Bitmap Resources
LoadCursor Loading Cursor Resources
LoadIcon Load Icon Resource
LoadMenu Loading Menu Resources
LoadString Load string Resource
Windows Resource Action functionsFindResource Load A resource of the specified name, type from the specified module
Sizeofresource get the length of the resource data
LoadResource load the specified resource to memory
Lockresource locks the specified resource and returns a pointer to the resource data
PNS FreeResource releases the specified resource
39 The steps that we typically load a resource are as follows:41 use LoadLibrary () to load the module, or get the module for the current application.
43 calls FindResource () to find the specified resource from the specified module and returns a handle to the resource. 45 Call Sizeofresource () to get the length of the resource data. 47 Call LoadResource () to load the resource into memory. 49 calls Lockresource () to lock the resource and returns a pointer to the resource data. 51 manipulate data with data pointers. 53 after use, call FreeResource () to release the specified resource. $ The following example program demonstrates how to add a word resource to the current program and read it at run time, so the reader can experience it carefully. - - select "Win32" → "Win32 Project" → "console program" command to create the Resloader. - AOpen the Resource View, import a Word document, and the resource type is defined as"WORD", the resource is named idr_word1,6-as shown in 22. + -(Click to view larger image) Figure 6- AImport a Word file as a resource $ write the main program as follows: the theThe program6-6"To load custom data from a resource the the on#include"stdafx.h" - Geneva#include"ResLoader.h" in Geneva the GenevaCWinApp Theapp; the to About . int_tmain () the - { the , intnRetCode =0; the the + Ten if(!AfxWinInit (:: GetModuleHandle (NULL),NULL,:: GetCommandLine (),0)) the One { Bayi A_tprintf (_t ("Error: MFC initialization failed \ n")); the -nRetCode =1; the - } - the Else - - { the -HRSRC hres =FindResource (theapp.m_hinstance,Makeintresource (Idr_word1), _t ("WORD")); the - //Get length the +DWORD len =Sizeofresource (theapp.m_hinstance, hres); - -Hglobal HG =LoadResource (theapp.m_hinstance, hres); the +LPVOID LP =(LPSTR) Lockresource (Hg); the A the at //Save the contents of a resource to a Word file94 -CFile file; the -File. Open (_t ("Out.doc"), CFile::Modewrite |cfile::modecreate); 98 - Char* CP = (Char*) LP; About - for(inti =0; i < Len; i++) - in { 101 -File. Write (cp++,1); 102 to } 103 + 104 -CString FilePath =file. GetFilePath (); the thefile. Close (); 106 * 107 $ //Open the file108 Panax NotoginsengShellExecute (null,_t ("Open"),filePath, NULL, NULL, SW_SHOW); the -FreeResource (Hg); 111 the } the + 113 A returnnRetCode;
the }
theWhen the program runs, it will automatically generate a Out.doc file in the current directory and automatically start Word to open it, 6-as shown in 23. 117(Click to view larger image) Figure 6- atRun Results118 Tips119 - Note the use of the ShellExecute () function, which executes a system command, such as opening a file. 121 122 CD Guide123 124The item corresponds to the directory on the disc"\ch06\resloader"。

Original address: http://book.51cto.com/art/200908/145793.htm

File operations-read from Windows resource files

Related Article

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.