WxWidgets: Use the xrc file separation interface and code

Source: Internet
Author: User
Tags wxwidgets

Wxwidgets supports xrc files. We can place strings, controls, interface la S, and images in xrc files and call these files after the program runs. This allows you to modify the interface, string, and graphics used in the program without modifying the program, and even implement i18n (internationalization ).
WxWidgets provides good support for standard controls, and you can use wxformbuilder to generate xrc files.

TextFrame. xrc
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resource version = "2.3.0.1" xmlns = "http://www.wxwidgets.org/wxxrc">
<Object class = "wxFrame" name = "main_frame">
<Title> TextDemo </title>
<Object class = "wxMenuBar" name = "main_menu">
<Object class = "wxMenu" name = "file_menu">
<Label> _ File </label>
<Object class = "wxMenuItem" name = "save_menuitem">
<Label> _ Save </label>
</Object>
</Object>
</Object>
<Object class = "wxTextCtrl" name = "tc">
<Style> wxTE_MULTILINE </style>
</Object>
</Object>
</Resource>


WxTextApp. h
# Ifndef TEXTFRAME_H _
# Define TEXTFRAME_H _

Class TextFrame: public wxFrame
{
Public:
TextFrame (wxWindow * parent );
Virtual ~ TextFrame ();

Void OnSave (wxCommandEvent & event );
Public:

 

Private:
DECLARE_EVENT_TABLE ()
};

# Endif/* TEXTFRAME_H _*/

 


TextApp. cpp

 

# Include "wx/wx_gch.h"

# Include "TextApp. h"
# Include "TextFrame. h"

IMPLEMENT_APP (TextApp)


Bool TextApp: OnInit ()
{
// Initialize the xrc Processor
WxXmlResource: Get ()-> InitAllHandlers ();
// Load the xrc File
If (! WxXmlResource: Get ()-> Load (_ T ("rc/TextFrame. xrc ")))
Return false;

TextFrame * frame = new TextFrame (wxWindow *) NULL );
Frame-> Show (true );
Return true;
}

 

 

TextFrame. h


# Ifndef TEXTFRAME_H _
# Define TEXTFRAME_H _

Class TextFrame: public wxFrame
{
Public:
TextFrame (wxWindow * parent );
Virtual ~ TextFrame ();

Void OnSave (wxCommandEvent & event );
Public:

 

Private:
DECLARE_EVENT_TABLE ()
};

# Endif/* TEXTFRAME_H _*/

 

TextFrame. cpp


# Include "wx/wx_gch.h"

# Include "TextFrame. h"
# Include "wx/filedlg. h"

 

BEGIN_EVENT_TABLE (TextFrame, wxFrame)
EVT_MENU (XRCID ("save_menuitem"), TextFrame: OnSave)
END_EVENT_TABLE ()

// WxMemoryDC dc; // The wxMemoryDC object must be constructed to ensure debugging.

TextFrame: TextFrame (wxWindow * parent)
{
// Initialize this window from xrc Resources
WxXmlResource: Get ()-> LoadFrame (this, parent, _ T ("main_frame "));
}

TextFrame ::~ TextFrame ()
{
}

Void TextFrame: OnSave (wxCommandEvent & event)
{
WxFileDialog fd (this,
_ T ("saving files "),
_ T ("."),
_ T (""),
_ T ("txt file (*. txt) | *. txt | all file (*. *) | *.*"),
WxSAVE,
Wxdefaposition position );
If (fd. ShowModal () = wxID_ OK)
{
// Find the control from the xrc Resource
WxTextCtrl * tc = XRCCTRL (* this, "tc", wxTextCtrl );
Tc-> SaveFile (fd. GetFilename ());
WxMessageBox ("saved successfully ");
}
}
 
If you have high requirements for page beautification, you have to write controls by yourself. The controls you write can support xrc.
Perform the following ten steps to create a custom control:
(1) Compile a class declaration. It should have a default constructor, a complete constructor, A Create Function for two-step creation, and an Init function for initializing internal data.
(2) Add a function DoGetBestSize, which returns the most suitable size of the control based on the internal control (such as the label size.
(3) If the existing event classes cannot meet your needs, add a new event class for your control. For example, the existing wxCommandEvent can be used for events with complicated internal buttons being pressed, but more complex controls require more complex event classes. If you add a new event class, you should also add corresponding event ing macros.
(4) Compile the code to display information on your new control.
(5) Compile the underlying mouse and keyboard control code, and generate custom events in the processing functions, so that the application can handle them accordingly.
(6) Compile a default event handler function so that the control can process standard events (such as events that process standard commands such as wxID_COPY or wxID_UNDO) or update events on the default user interface.
(7) add an optional validators class so that the application can use it to make the transfer between data and controls easy, and add the data verification function.
(8) Add a resource processing class (optional) so that you can use your custom control in the XRC file.
(9) test your custom controls on all platforms you are about to use.
(10) Compile the document.


From camel's blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.