zoukankan      html  css  js  c++  java
  • 使用wxWidgets for 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</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;
    static unsigned char xml_res_file_1[] = {
    60,63,120,109,108,32,118,101,114,115,105,111,110,61,34,49,46,48,34,32,101,
    110,99,111,100,105,110,103,61,34,85,84,70,45,56,34,63,62,10,60,114,101,
    115,111,117,114,99,101,32,118,101,114,115,105,111,110,61,34,50,46,51,46,
    48,46,49,34,62,10,32,32,60,111,98,106,101,99,116,32,99,108,97,115,115,61,
    34,119,120,66,105,116,109,97,112,34,32,110,97,109,101,61,34,98,97,99,107,
    103,114,111,117,110,100,34,62,114,101,115,111,117,114,99,101,115,46,104,
    36,98,97,99,107,103,114,111,117,110,100,46,106,112,103,60,47,111,98,106,
    101,99,116,62,10,60,47,114,101,115,111,117,114,99,101,62,10};

    void InitXmlResource()
    {

        
    // Check for memory FS. If not present, load the handler:
        {
            wxMemoryFSHandler::AddFile(wxT(
    "XRC_resource/dummy_file"), wxT("dummy one"));
            wxFileSystem fsys;
            wxFSFile 
    *= fsys.OpenFile(wxT("memory:XRC_resource/dummy_file"));
            wxMemoryFSHandler::RemoveFile(wxT(
    "XRC_resource/dummy_file"));
            
    if (f) delete f;
            
    else wxFileSystem::AddHandler(new wxMemoryFSHandler);
        }

        wxMemoryFSHandler::AddFile(wxT(
    "XRC_resource/resources.h$background.jpg"), xml_res_file_0, xml_res_size_0);
        wxMemoryFSHandler::AddFile(wxT(
    "XRC_resource/resources.h$main.xrc"), xml_res_file_1, xml_res_size_1);
        wxXmlResource::Get()
    ->Load(wxT("memory:XRC_resource/resources.h$main.xrc"));
    }

    其后在主程序中调用 InitXmlResource函数。

    要想装载这个background.jpg图,使用以下语句:
        wxXmlResource::Get()->InitAllHandlers();     
    wxBitmap background = wxXmlResource::Get()->LoadBitmap(_T("background"));
    m_pStaticBitmap = new wxStaticBitmap(this, wxID_ANY, background, wxPoint(0, 0), wxSize(1024, 768));
    但要注意使用wxStaticBitmap时应在wxFrame中的OnSize中加入
        m_pStaticBitmap->Refresh();
    否则在windows下当窗口缩小后再放大时,图像显示有一些问题。但在linux下可以不加这条语句。

  • 相关阅读:
    PPR的断管
    排水地漏的功能与种类
    PPR管及管件的类型、规格与选用
    水龙头的安装、拆卸与阀芯更换
    为不同的用户生成不同的 Kibana 界面
    如何让匿名的用户访问受限的资源
    Beats processors
    Elasticsearch 开发入门
    Elasticsearch Dockerfile 例子
    燃气热水器的结构与安装
  • 原文地址:https://www.cnblogs.com/nokiaguy/p/1195638.html
Copyright © 2011-2022 走看看