Wxwidget 动态加载资源文件

一.创建自己的资源文件

你可以从一个xml文件中加载对话框,frame窗口,菜单条,工具条等等。不用c++代码去实现,这符合代码界面和代码分离的原则。 我们可以使用DialogBlocks,XRCed和wxGlade还有wxDesigner工具去绘制我们界面保存在xxx.xrc格式的文件中


二.加载资源文件

要使用rXRC文件,在代码中包含wx/xrc/xmlres.h头文件。如果你打算把XRC文件转化成二进制XRS文件使用。在代码中还有增加zip文件系统函数 你可以在Oninit函数添加如下代码:
#include ”wx/filesys.h”
#include ”wx/fs zip.h”
wxFileSystem::AddHandler(new wxZipFSHandler);
初始化XRC系统并加载一个XRC文件 在Oninit函数添加如下代码:
wxXmlResource::Get()−>InitAllHandlers();
wxXmlResource::Get()−>Load(wxT(”resources.xrc”));


三.用wxrc翻译XRC文件加载到代码中:

//例如
wxrc -c -e resource.xrc
//执行这条命令后就会产生resource.cpp resource.h

你也可以完全指定翻译文件和函数例如:
 wxrc -c -e -n InitAboutDialog Dlg_About.xrc -o dlg_About.cpp
然后你就可以在Oninit函数中添加这个函数的调用
在Oninit函数文件中添加extern void InitAboutDialog();
在Oninit函数添加InitAboutDialog();
这2行代码和wxXmlResource::Get()->Load("rc/About.xrc");是等价的。

四.wxrc 参数

Wxwidget 动态加载资源文件_第1张图片

五,这里写了个小例子供大家参考

#include "wx/wx.h"
#include "wx/image.h"
#include "wx/xrc/xmlres.h"
//#include "resource.h"

// the application icon
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
    #include "rc/appicon.xpm"
#endif

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
    // override base class virtuals
    // ----------------------------

    // this one is called on application startup and is a good place for the app
    // initialization (doing it here and not in the ctor allows to have an error
    // return: if OnInit() returns false, the application terminates)
    virtual bool OnInit();
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    // ctor(s)
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    void OnDlg1(wxCommandEvent& event);
    void OnDlg2(wxCommandEvent& event);

private:
    // any class wishing to process wxWidgets events must use this macro
    DECLARE_EVENT_TABLE()
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(XRCID("menu_quit"),  MyFrame::OnQuit)
    EVT_MENU(XRCID("menu_about"), MyFrame::OnAbout)
    EVT_MENU(XRCID("menu_dlg1"), MyFrame::OnDlg1)
    EVT_MENU(XRCID("menu_dlg2"), MyFrame::OnDlg2)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

extern void InitXmlResource();
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    wxImage::AddHandler(new wxGIFHandler);
    //wxImage::AddHandler(new wxXPMHandler);
    wxXmlResource::Get()->InitAllHandlers();
    //wxXmlResource::Get()->Load("rc/resource.xrc");
    InitXmlResource();

    MyFrame *frame = new MyFrame("XML resources demo",
                                 wxPoint(50, 50), wxSize(450, 340));
    frame->Show(true);
    return true;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
       : wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
    SetIcon(wxICON(appicon));

    SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu"));
    SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar"));
}

// event handlers
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf( _T("This is the about dialog of XML resources demo.\n")
                _T("Welcome to %s"), wxVERSION_STRING);

    wxMessageBox(msg, "About XML resources demo", wxOK | wxICON_INFORMATION, this);
}

void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event))
{
    wxDialog dlg;
    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
    dlg.ShowModal();
}

void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event))
{
    wxDialog dlg;
    wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2");
    dlg.ShowModal();
} 

资源文件resource.xrc


  
    
    
      
      
      
        
        filesave.gif
      
      
      
        
      
      
        
      
      
      
        
      
    
  
  
    
    2,2
    
      fileopen.gif
      Open catalog
    
    
      filesave.gif
      Save catalog
    
    
      update.gif
      Update catalog - synchronize it with sources
    
    
    
      quotes.gif
      1
      Display quotes around the string?
    
    
    
      fuzzy.gif
      Toggled if selected string is fuzzy translation
      1
    
  
  
    
      
        
          fuzzy.gif
          fileopen.gif
        
      
      
        
          
            
          
          
        
        wxALIGN_CENTER
      
      
        
          
        
        10d
        wxALL
      
      
        
          

Hi,

man
100,45d
Hello, we are inside a NOTEBOOK... 50,50d Hello, we are inside a NOTEBOOK... 50,50d 1 wxEXPAND wxVERTICAL
wxVERTICAL 200,200d Hello, this is an ordinary multiline\n textctrl.... wxEXPAND|wxALL 10 1 10 wxLEFT wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_RIGHT 10 Second testing dialog 我在ubuntu上运行的示例如下:
弹出的窗口和窗口上的图标都是我们动态调运xrc文件生成的。
Wxwidget 动态加载资源文件_第2张图片

项目工程代码:http://download.csdn.net/download/bxd1314/9603707

你可能感兴趣的:(wxWidget入门)