Skip to content

Using resource files in wxWidgets

FANG edited this page Sep 2, 2019 · 1 revision

Abstract

This page describes three ways of adding resources to programs.

RC

In Visual Studio, resources are *.rc files, which is created by VS. An resource.h file is like:

#define ICON_MYNOTEPAD                  1001

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        1002
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         10001
#define _APS_NEXT_SYMED_VALUE           2001
#endif
#endif

An RC file is like this:

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
/////////////////////////////////////////////////////////////////////////////
// 中文(简体,中国) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED

/////////////////////////////////////////////////////////////////////////////
//
// Icon
//

// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
ICON_MYNOTEPAD          ICON                    "MyNotePad.ico"

#endif    // 中文(简体,中国) resources
/////////////////////////////////////////////////////////////////////////////

It's pretty easy to load the icon:

#include "resource.h"
SetIcon(wxICON(ICON_MYNOTEPAD));

WXRC

In Linux, resources are *.xrc files in XML format. When using wxWidgets, a XRC file is like:

<?xml version="1.0"?>
<resource version="2.3.0.1">
<object class="wxIcon" name="app_icon">app.ico</object>
<object class="wxBitmap" name="app_logo">logo.bmp</object>
</resource>

It can be compiled to a C++ code file or Zip archive by wxrc command in wxWidgets:

wxrc abc.xrc -v -c -o abc.h

Then use it just like this:

#include <wx/xrc/xmlres.h>
#include "abc.h"
 
bool CManagerApp::OnInit()
{
    wxInitAllImageHandlers();
    wxXmlResource::Get()->InitAllHandlers();
    InitXmlResource();
    ...
    SetIcon(wxXmlResource::Get()->LoadIcon(wxT("app_icon")));
    wxBitmap app_logo(wxXmlResource::Get()->LoadBitmap(wxT("app_logo")));
}

By default, wxrc generates an XRS file, which is a Zip archive:

wxrc abc.xrc -o abc.xrs

Then use it just like this:

#include <wx/filesys.h>
#include <wx/fs_arc.h>
#include <wx/xrc/xmlres.h>

bool CManagerApp::OnInit()
{
    wxInitAllImageHandlers();
    wxXmlResource::Get()->InitAllHandlers();
    wxFileSystem::AddHandler(new wxArchiveFSHandler);
    wxXmlResource::Get()->Load(wxT("abc.xrs"));
    ...
    SetIcon(wxXmlResource::Get()->LoadIcon(wxT("app_icon")));
    wxBitmap app_logo(wxXmlResource::Get()->LoadBitmap(wxT("app_logo")));
}

As illustrated above, an XRS file will be apart from the executable file.

XPM

XPM(X Pixmap) is an image format in X Windows system, it's a text file that compatible with C syntax. It looks like:

/* XPM */
static const char *ICON_MYAPP_xpm[] = {
/* columns rows colors chars-per-pixel */
"48 48 142 2 ",
"   c #00006161FFFF",
...
"z. c #FEFEFEFEFFFF",
"x. c white",
"c. c None",
/* pixels */
"c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.",
...
"c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c.c."
};

Use it:

#include "app_icon.xpm"
SetIcon(wxIcon(ICON_MYAPP));

Reference

Clone this wiki locally