当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

C++从资源文件中静态装载图像

    在wxWidgets中装载图像是非常容易的,但是如果将图像文件和可执行文件放到一起,在发布时只需要发布可执行文件,要实现这种功能,一般可以使用资源文件来解决。在windows下的资源文件的源文件是*.rc,编译后叫*.res。在linux下类似,源文件为*.xrc,编译后叫*.xres。但它们是xml格式的,要装载这种资源文件也得动态进行装载。相当于配置文件。

    如果想将其直接编译进可执行文件。需要一个工具wxrc。这个工作在wxWidgets中的untils目录中,可自己编译。可使用这个工具将*.xrc 生成c++代码,如果是图像,就将其转换成字符数组。然后和其它程序一起进行编译。可使用wxrc -c main.xrc -v -o main.h

    main.xrc的格式如下:

 <?xml version="1.0"?>

<resource version="2.3.0.1"> 
 
   <object class="wxBitmap" name="background">background.jpg?http://www.xvna.com</object>

</resource>


    生成的main.h的格式如下:

 // This file was automatically generated by wxrc, do not edit by hand.

#include <wx/wxprec.h>

#ifdef __BORLANDC__

    #pragma hdrstop

#endif

#include <wx/filesys.h>

#include <wx/fs_mem.h>

#include <wx/xrc/xmlres.h>

#include <wx/xrc/xh_all.h>

static size_t xml_res_size_0 = 55966;

static unsigned char xml_res_file_0[] = {

255,216,255,224,0,16,74,70,73,70,0,1,2,0,0,100,0,100,0,0,255,236,0,17,68,
117,99,107,121,0,1,0,4,0,0,0,60,0,0,255,238,0,14,65,100,111,98,101,0,100,
192,0,0,0,1,255,219,0,132,0,6,4,4,4,5,4,6,5,5,6,9,6,5,6,9,11,8,6,6,8,11,
12,10,10,11,10,10,12,16,12,12,12,12,12,12,16,12,14,15,16,15,14,12,19,19,
20,20,19,19,28,27,27,27,28,31,31,31,31,31,31,31,31,31,31,1,7,7,7,13,12,
13,24,16,16,24,26,21,17,21,26,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,255,192,0,17,8,2,201,3,248,3,1,17,0,2,
.
.
102,222,191,202,130,94,195,45,21,39,169,149,113,241,11,45,173,84,25,120,
94,18,190,190,196,107,2,216,129,52,90,4,128,1,130,160,201,16,36,0,147,0,
84,68,164,164,47,80,44,0,96,168,202,0,201,145,130,178,49,229,32,127,255,
217};

static size_t xml_res_size_1 = 161;

 

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接