Using XRC in wxWidgets based Application for UI design
相信很多程序员有过类似的经历,想实现一款图形界面的应用软件。核心算法和后台程序都写好后,等到实现界面时总是发现缺乏艺术细胞,对自己设计的界面和对话框等不满意,或者是消耗在挑选控件、配色和布局等繁琐的工作中不能自拔,最终也许会因为UI的问题而导致放弃整个项目。一款没有优秀界面的应用软件在繁多的类似产品中很难获得用户的喜爱。利用wxWidgets我们可以将很多界面设计工作轻松搞定,而且利用XRC,XML based resource,规范,可以将界面设计工作和程序分离,独立的XML文件维护容易,简单直观而且可以使得程序本身只关注于逻辑,何乐而不为呢。
以下包括几个部分介绍XRC,有XRC介绍包括中英文的(摘自网络)和使用步骤,以及几个常用设计工具的链接。
The XML-based resource system, known as XRC, allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time. XRC files can also be compiled into binary XRS files or C++ code (the former makes it possible to store all resources in a single file and the latter is useful when you want to embed the resources into the executable).
There are several advantages to using XRC resources.
- Recompiling and linking an application is not necessary if the resources change.
- If you use a dialog designer that generates C++ code, it can be hard to reintegrate this into existing C++ code. Separation of resources and code is a more elegant solution.
- You can choose between different alternative resource files at run time, if necessary.
- The XRC format uses sizers for flexibility, allowing dialogs to be resizable and highly portable.
- The XRC format is a wxWidgets standard, and can be generated or postprocessed by any program that understands it. As it is based on the XML standard, existing XML editors can be used for simple editing purposes.
XRC was written by Vaclav Slavik.
XRC(XML Resource)的设计来源于wxWidgets,它的想法很简单,就是将界面设计的工作从程序中独立出来。具体的做法是,创建单独的XML文件,负责界面设计,程序运行的时候载入,生成界面。这样做的好处是显而易见的。首先,将繁琐的外观设计代码从程序中去掉,程序更清晰易读。其次,XRC文件独立于程序,程序运行时才调用,因此可以随意更换外观。这种思想并不是wxWidgets的原创,MFC中的RC已经有了,类似的还有HTML和CSS的关系。wxPython从wxWidgets继承而来,当然也保留了XRC。
这里有几点要补充的。一是wxPython的XRC文件中用到的类名称仍然是wxWidgets中的类名称,换句话说,wxPython和wxWidgets可以共用XRC文件,第二点要补充的是XRC文件可以编译成二进制文件XRS,或者编译成C++代码。
- http://hi.baidu.com/madrigar/blog/item/e36c21a44c9787f29052ee84.html
使用XRC的具体步骤如下:
These are the typical steps for using XRC files in your application.
- Include the appropriate headers: normally "wx/xrc/xmlres.h" will suffice;
- If you are going to use XRS files, install wxFileSystem ZIP handler first with wxFileSystem::AddHandler(new wxZipFSHandler);
- call wxXmlResource::Get()->InitAllHandlers() from your wxApp::OnInit function, and then call wxXmlResource::Get()->Load("myfile.xrc") to load the resource file;
- to create a dialog from a resource, create it using the default constructor, and then load it using for example wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
- set up event tables as usual but use the XRCID(str) macro to translate from XRC string names to a suitable integer identifier, for example EVT_MENU(XRCID("quit"), MyFrame::OnQuit).
To create an XRC file, you can use one of the following methods.
- Create the file by hand;
- use wxDesigner, a commercial dialog designer/RAD tool;
- use DialogBlocks, a commercial dialog editor;
- use XRCed, a wxPython-based dialog editor that you can find in the wxPython/tools subdirectory of the wxWidgets CVS archive;
- use wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
- convert WIN32 RC files to XRC with the tool in contrib/utils/convertrc.
A complete list of third-party tools that write to XRC can be found at www.wxwidgets.org/lnk_tool.htm.
It is highly recommended that you use a resource editing tool, since it’s fiddly writing XRC files by hand.
You can use wxXmlResource::Load in a number of ways. You can pass an XRC file (XML-based text resource file) or a zip-compressed file (extension ZIP or XRS) containing other XRC.
You can also use embedded C++ resources
Further reading: http://docs.wxwidgets.org/2.6.3/wx_xrcoverview.html#xrcconcepts
Related posts:
- A rough guide for wxWidgets programming
- FLTK & the List of Widget toolkits based on C/C++
- wxWidgets Programming: Sizer, Frame and Panel
- Book: Cross-Platform GUI Programming with wxWidgets (Bruce Perens)(含中英文版本下载地址)
- Code::Blocks IDE in openSUSE 11.1编译和安装指南
- wxWidget Layout Algorithm Demo - BoxPlanner
- CodeLite Initializaion analysis - 初始化流程分析(1)
- CodeLite Plugin Internal(2)-Load: 插件加载分析
- Singleton模式














