[wxWidgets]_[初级]_[使用wxFormBuilder设计XRC文件快速开发界面]


[wxWidgets]_[初级]_[使用wxFormBuilder设计XRC文件快速开发界面]_第1张图片


场景:

1.wxWidgets是一个优秀的界面库,它的xrc界面布局格式也是很优秀的,可以使用类似mfc的所见即所得的设计方式,我发现它和xcode的interface builder的理念如出一辙。

都是界面与业务分离,界面与业务动态绑定。


2.快速开发小软件时,使用wxFormBuilder来设计好界面,生成XRC文件,当然不想给别人看到xrc文件的内容也可以用工具编译为二进制文件,参考wx.chm文档吧。之后程序载入界面再绑定行为很优雅和高效。wxFormBuilder设计好之后按f5直接看运行效果很赞。

3.博客导出下载工具 就是用wxFormBuilder来设计界面的,xrc文件本身不应该手动编辑,因为它的结构化信息比较多,用软件代为生成就最适合不过了。


文件1: wxFormBuilder生成的xrc文件另存为blog.xrc:


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<resource xmlns="http://www.wxwindows.org/wxxrc" version="2.3.0.1">
	<object class="wxFrame" name="MyFrame1">
		<style>wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL</style>
		<size>466,300</size>
		<bg>#ffffff</bg>
		<title>博客备份导出(infoworld)</title>
		<centered>1</centered>
		<aui_managed>0</aui_managed>
		<object class="wxBoxSizer">
			<orient>wxVERTICAL</orient>
			<object class="spacer">
				<option>0</option>
				<flag></flag>
				<border>5</border>
				<size>0,20</size>
			</object>
			<object class="sizeritem">
				<option>0</option>
				<flag>wxEXPAND</flag>
				<border>0</border>
				<object class="wxBoxSizer">
					<orient>wxHORIZONTAL</orient>
					<object class="sizeritem">
						<option>0</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxStaticText" name="m_staticText2">
							<label>博客网址:</label>
							<wrap>-1</wrap>
						</object>
					</object>
					<object class="sizeritem">
						<option>1</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxTextCtrl" name="m_textCtrl1">
							<style>wxSIMPLE_BORDER</style>
							<value></value>
						</object>
					</object>
					<object class="sizeritem">
						<option>0</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxButton" name="m_button1">
							<label>备份</label>
							<default>0</default>
						</object>
					</object>
				</object>
			</object>
			<object class="sizeritem">
				<option>0</option>
				<flag>wxEXPAND</flag>
				<border>5</border>
				<object class="wxBoxSizer">
					<orient>wxHORIZONTAL</orient>
					<object class="sizeritem">
						<option>0</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxStaticText" name="m_staticText3">
							<label>输出目录:</label>
							<wrap>-1</wrap>
						</object>
					</object>
					<object class="sizeritem">
						<option>1</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxTextCtrl" name="m_textCtrl6">
							<style>wxTE_READONLY|wxSIMPLE_BORDER</style>
							<value></value>
						</object>
					</object>
					<object class="sizeritem">
						<option>0</option>
						<flag>wxALL</flag>
						<border>5</border>
						<object class="wxButton" name="m_button2">
							<label>浏览</label>
							<default>0</default>
						</object>
					</object>
				</object>
			</object>
			<object class="sizeritem">
				<option>0</option>
				<flag>wxALL</flag>
				<border>5</border>
				<object class="wxStaticText" name="m_staticText21">
					<label>输出窗口:</label>
					<wrap>-1</wrap>
				</object>
			</object>
			<object class="sizeritem">
				<option>1</option>
				<flag>wxEXPAND</flag>
				<border>5</border>
				<object class="wxBoxSizer">
					<orient>wxHORIZONTAL</orient>
					<object class="sizeritem">
						<option>1</option>
						<flag>wxALL|wxEXPAND</flag>
						<border>5</border>
						<object class="wxTextCtrl" name="m_textCtrl2">
							<style>wxTE_MULTILINE|wxSIMPLE_BORDER</style>
							<size>466,200</size>
							<value></value>
						</object>
					</object>
				</object>
			</object>
		</object>
		<object class="wxStatusBar" name="m_statusBar1">
			<style>wxST_SIZEGRIP</style>
			<fields>1</fields>
		</object>
		<object class="wxMenuBar" name="m_menubar1">
			<label>MyMenuBar</label>
			<object class="wxMenu" name="m_menu1">
				<label>文件</label>
				<object class="wxMenuItem" name="wxID_EXIT">
					<label>退出\tAlt+X</label>
					<help></help>
				</object>
			</object>
			<object class="wxMenu" name="m_menu2">
				<label>帮助</label>
				<object class="wxMenuItem" name="kMenuFeedback">
					<label>反馈\tF1</label>
					<help></help>
				</object>
			</object>
		</object>
	</object>
</resource>

文件2:main.cpp调用载入xrc和绑定事件,以下调用python部分可以删除,无关。


bool MyApp::OnInit()
{
	if (!wxApp::OnInit())
	{
		return false;
	}
	Py_Initialize();
  PyObject* obj = Py_InitModule("redirection", RedirectionMethods);
  assert(obj);
  PyRun_SimpleString("\
import redirection\n\
import sys\n\
class StdoutCatcher:\n\
    def write(self, stuff):\n\
        redirection.stdoutredirect(stuff)\n\
sys.stdout = StdoutCatcher()");

  wxInitAllImageHandlers();
  wxXmlResource::Get()->InitAllHandlers();
  
//载入Ui目录下的所有xrc文件
  wxString ui_dir = GetAppRunDirectory()+wxT("/Ui");
  wxXmlResource::Get()->LoadAllFiles(ui_dir);

  MyFrame *frame = new MyFrame(NULL);
  bool loaded = wxXmlResource::Get()->LoadFrame(frame, NULL, "MyFrame1");
  assert(loaded);
  frame->Init();
  frame->Show(true);
  return true;
}

//1.创建MyFrame之后动态绑定事件
void MyFrame::Init()
{
	 backup_button_ =  XRCCTRL(*this, "m_button1", wxButton);
   backup_button_->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(MyFrame::OnBackup), this, XRCID("m_button1"));
   XRCCTRL(*this, "m_button2", wxButton)->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(MyFrame::OnBrowse), this, XRCID("m_button2"));

	 pText_ = XRCCTRL(*this, "m_textCtrl2", wxTextCtrl);

   url_ = XRCCTRL(*this, "m_textCtrl1", wxTextCtrl);
   output_ = XRCCTRL(*this, "m_textCtrl6", wxTextCtrl);

   Connect(wxXmlResource::GetXRCID(wxT("kMenuFeedback")), wxEVT_COMMAND_MENU_SELECTED, 
    wxCommandEventHandler( MyFrame::OnFeedback ) );
   Connect(wxXmlResource::GetXRCID(wxT("wxID_EXIT")), wxEVT_COMMAND_MENU_SELECTED, 
    wxCommandEventHandler( MyFrame::OnExit ) ); 

   wxIcon icon;
   icon.LoadFile(wxT("blog.ico"),wxBITMAP_TYPE_ICO ,32,32);
   SetIcon(icon);

   gFrame = this;
}

注意:载入xrc后,wx会自动生成一个控件对应的id,这时候用 wxXmlResource::GetXRCID(wxT("kMenuFeedback"))通过name来获取id即可。


你可能感兴趣的:(wxwidgets,布局文件,wxformBuilder,xrc)