wxWidgets一个界面与数据分离的简单例子
/***************************************************************
* Name: MyApp.h
* Purpose: Defines MyApp Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ #ifndef MYAPP_H_
#define MYAPP_H_ //(*Headers(MyApp)
#include <wx/app.h>
//*) class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
DECLARE_APP(MyApp) #endif // MYAPP_H_
/***************************************************************
* Name: MyApp.cpp
* Purpose: Code for MyApp Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ //(*Headers(MyApp)
#include <wx/wx.h>
#include "MyApp.h"
#include "MyFrame.h"
//*) IMPLEMENT_APP(MyApp) bool MyApp::OnInit()
{
/**< Create the main application window */
MyFrame * frame = new MyFrame(wxT("PersonalRecordDialog Demo")); frame->Show(true); /**< Start the event loop */
return wxOK;
}
/***************************************************************
* Name: MyFrame.h
* Purpose: Defines MyFrame Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ #ifndef MYFRAME_H_
#define MYFRAME_H_ //(*Headers(MyFrame)
#include <wx/wx.h>
//*) class MyFrame : public wxFrame
{
DECLARE_CLASS(MyFrame) DECLARE_EVENT_TABLE() public:
MyFrame(const wxString & title);
virtual ~MyFrame() {} void CreateControls(); //(*Handlers(MyFrame)
void OnAbout (wxCommandEvent & event);
void OnQuit (wxCommandEvent & event);
void OnShowDialog (wxCommandEvent & event);
//*) protected:
//(*Identifiers(MyFrame)
enum{ID_SHOW_DIALOG};
//*)
}; #endif // MYFRAME_H_
/***************************************************************
* Name: MyFrame.cpp
* Purpose: Code for MyFrame Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ //(*Headers(MyFrame)
#include <wx/wx.h>
#include <wx/popupwin.h>
#include <wx/notebook.h>
#include "MyFrame.h"
#include "personalRecord.h"
#include "wx.xpm"
//*) IMPLEMENT_CLASS(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
//(*EventTable(MyFrame)
EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
EVT_MENU (ID_SHOW_DIALOG, MyFrame::OnShowDialog)
//*)
END_EVENT_TABLE() MyFrame::MyFrame(const wxString & title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE)
{
/**< Set the frame icon */
SetIcon(wxIcon(wx_xpm)); /**< Create a menu bar */
wxMenu * fileMenu = new wxMenu;
wxMenu * helpMenu = new wxMenu; helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"), wxT("Show about dialog"));
fileMenu->Append(ID_SHOW_DIALOG, wxT("&Show Personal Record Dialog..."), wxT("Show Personal Record Dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quit this program")); /**< Now append the freshly created menu to the menu bar... */
wxMenuBar * menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help")); /**< ... and attach this menu bar to the frame */
SetMenuBar(menuBar); CreateStatusBar();
SetStatusText(wxT("welcome to wxWidgets!"), ); CreateControls();
} void MyFrame::CreateControls()
{ } void MyFrame::OnAbout(wxCommandEvent & event)
{
wxString msg;
msg.Printf(wxT("PersonalRecordDialog example, built with wxWidgets %s"), wxVERSION_STRING); wxMessageBox(msg, wxT("About this program"), wxOK|wxICON_INFORMATION, this);
} void MyFrame::OnQuit(wxCommandEvent & event)
{
/**< Destroy the frame */
Close(wxOK);
} void MyFrame::OnShowDialog(wxCommandEvent & event)
{
PersonalRecordDialog dialog(this, wxID_ANY, wxT("Personal Record"),
wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
dialog.ShowModal();
}
/***************************************************************
* Name: PersonalRecord.h
* Purpose: Defines PersonalRecordDialog Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ #ifndef PERSONALRECORD_H_
#define PERSONALRECORD_H_ //(*Headers(PersonalRecordDialog)
#include <wx/spinctrl.h>
#include <wx/statline.h>
#include "Data.h"
//*) class PersonalRecordDialog : public wxDialog
{
DECLARE_CLASS(PersonalRecordDialog) DECLARE_EVENT_TABLE() public:
PersonalRecordDialog(); PersonalRecordDialog( wxWindow * parent,
wxWindowID id = ID_PERSONAL_RECORD,
const wxString & caption = wxT("Personal Record"),
const wxPoint & post = wxDefaultPosition,
const wxSize & size = wxDefaultSize,
long style = wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU
); /**< Member initialisation */
void Init(); /**< Creation */
bool Create( wxWindow * parent,
wxWindowID id = ID_PERSONAL_RECORD,
const wxString & caption = wxT("Personal Record"),
const wxPoint & post = wxDefaultPosition,
const wxSize & size = wxDefaultSize,
long style = wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU
); void CreateControls(); void SetDialogValidators(); void SetDialogHelp(); //(*Handlers(PersonalRecordDialog)
void OnVoteUpdate(wxUpdateUIEvent & event);
void OnResetClick(wxCommandEvent & event);
void OnOkClick(wxCommandEvent & event);
void OnCancelClick(wxCommandEvent & event);
void OnHelpClick(wxCommandEvent & event);
//*) protected:
//(*Identifiers(PersonalRecordDialog)
enum{ID_PERSONAL_RECORD,
ID_NAME,
ID_AGE,
ID_SEX,
ID_VOTE,
ID_ON_RESET,
ID_ON_OK,
ID_ON_CANCEL,
ID_ON_HELP};
//*) private:
Data person;
}; #endif //#define _PERSONALRECORD_H_
/***************************************************************
* Name: PersonalRecord.cpp
* Purpose: Code for PersonalRecordDialog Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-10-19
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ //(*Headers(PersonalRecordDialog)
#include "wx/wx.h"
#include "wx/valgen.h" // for TransferDataToWindow()
#include "wx/valtext.h" // for wxTextValidator()
#include "wx/cshelp.h"
#include "PersonalRecord.h"
//*) IMPLEMENT_CLASS(PersonalRecordDialog, wxDialog) BEGIN_EVENT_TABLE(PersonalRecordDialog, wxDialog)
//(*EventTable(PersonalRecordDialog)
EVT_UPDATE_UI (ID_VOTE, PersonalRecordDialog::OnVoteUpdate)
EVT_BUTTON (ID_ON_RESET, PersonalRecordDialog::OnResetClick)
EVT_BUTTON (ID_ON_OK, PersonalRecordDialog::OnOkClick)
EVT_BUTTON (ID_ON_CANCEL, PersonalRecordDialog::OnCancelClick)
EVT_BUTTON (ID_ON_HELP, PersonalRecordDialog::OnHelpClick)
//*)
END_EVENT_TABLE() PersonalRecordDialog::PersonalRecordDialog()
{
Init();
} PersonalRecordDialog::PersonalRecordDialog( wxWindow * parent,
wxWindowID id,
const wxString & caption,
const wxPoint & pos,
const wxSize & size,
long style
)
{
Init();
Create(parent, id, caption, pos, size, style);
} /**< Initialisation */
void PersonalRecordDialog::Init(void)
{
person.Read();
} bool PersonalRecordDialog::Create( wxWindow * parent,
wxWindowID id,
const wxString & caption,
const wxPoint & pos,
const wxSize & size,
long style
)
{
/**< We have to set extra styles before creating the dialog */
SetExtraStyle(wxWS_EX_BLOCK_EVENTS|wxDIALOG_EX_CONTEXTHELP); if(!wxDialog::Create(parent, id, caption, pos, size, style))
{
return false;
} CreateControls();
SetDialogHelp();
SetDialogValidators(); /**< This fits the dialog to the minimum size dictated by the sizer */
GetSizer()->Fit(this); /**< This ensures that the dialog cannot be sized smaller than the minimum size */
GetSizer()->SetSizeHints(this); //Centre the dialog on the parent or screen(if none parent)
Centre(); return true;
} /**< Control creation for PersonalRecordDialog */
void PersonalRecordDialog::CreateControls(void)
{
//(*A top-level sizer
wxBoxSizer * topSizer = new wxBoxSizer(wxVERTICAL);
this->SetSizer(topSizer);
//*) //(*A second box sizer to give more space around the controls
wxBoxSizer * boxSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(boxSizer, , wxALIGN_CENTRE_HORIZONTAL|wxALL, );
//*) /**< A friendly message */
wxStaticText * descr = new wxStaticText(this, wxID_STATIC,
wxT("Please enter your name, age and sex, and specify whether you wish to\nvote in a general election."),
wxDefaultPosition, wxDefaultSize);
boxSizer->Add(descr, , wxALIGN_LEFT|wxALL, ); /**< Spacer */
boxSizer->Add(, , , wxALIGN_CENTER_HORIZONTAL|wxALL, ); /**< Label for the name text control */
wxStaticText * nameLabel = new wxStaticText(this, wxID_STATIC, wxT("&Name:"),
wxDefaultPosition, wxDefaultSize);
boxSizer->Add(nameLabel, , wxALIGN_LEFT|wxALL, ); /**< A text control for the user's name */
wxTextCtrl * nameCtrl = new wxTextCtrl(this, ID_NAME, wxT("Emma"),
wxDefaultPosition, wxDefaultSize);
boxSizer->Add(nameCtrl, , wxGROW|wxALL, ); //(*A horizontal box sizer to contain age,sex and vote
wxBoxSizer * ageSexVoteBox = new wxBoxSizer(wxHORIZONTAL);
boxSizer->Add(ageSexVoteBox, , wxGROW|wxALL, );
//*) /**< Label for the age control */
wxStaticText * ageLabel = new wxStaticText(this, wxID_STATIC, wxT("&Age"),
wxDefaultPosition, wxDefaultSize);
ageSexVoteBox->Add(ageLabel, , wxALIGN_CENTRE_VERTICAL|wxALL, ); /**< A spin control for the user's age */
wxSpinCtrl * ageSpin = new wxSpinCtrl(this, ID_AGE, wxEmptyString,
wxDefaultPosition, wxSize(, -), wxSP_ARROW_KEYS, , , );
ageSexVoteBox->Add(ageSpin, , wxALIGN_CENTRE_VERTICAL|wxALL, ); /**< Label for the sex control */
wxStaticText * sexLabel = new wxStaticText(this, wxID_STATIC, wxT("&Sex:"),
wxDefaultPosition, wxDefaultSize);
ageSexVoteBox->Add(sexLabel, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< Create the sex choice control */
wxString sexStrings[] = { wxT("Male"),
wxT("Female")
};
wxChoice * sexChoice = new wxChoice(this, ID_SEX, wxDefaultPosition,
wxSize(, -), WXSIZEOF(sexStrings), sexStrings);
sexChoice->SetStringSelection(wxT("Female"));
ageSexVoteBox->Add(sexChoice, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< Add a spacer thar stretches to push the Vote control to the right */
ageSexVoteBox->Add(, , , wxALIGN_CENTER_VERTICAL|wxALL, ); wxCheckBox * voteCheckBox = new wxCheckBox(this, ID_VOTE, wxT("&Vote"),
wxDefaultPosition, wxDefaultSize);
voteCheckBox->SetValue(true);
ageSexVoteBox->Add(voteCheckBox, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< A dividing line before the OK and Cancel buttons */
wxStaticLine * line = new wxStaticLine(this, wxID_STATIC,
wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL);
boxSizer->Add(line, , wxGROW|wxALL, ); //(*A horizontal box sizer to contain Reset,OK,Cancel and Help
wxBoxSizer * okCancelBox = new wxBoxSizer(wxHORIZONTAL);
boxSizer->Add(okCancelBox, , wxALIGN_CENTER_HORIZONTAL|wxALL, );
//*) /**< The Reset button */
wxButton * reset = new wxButton(this, ID_ON_RESET, wxT("&Reset"),
wxDefaultPosition, wxDefaultSize);
okCancelBox->Add(reset, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< The OK button */
wxButton * ok = new wxButton(this, ID_ON_OK, wxT("&OK"),
wxDefaultPosition, wxDefaultSize);
okCancelBox->Add(ok, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< The Cancel button */
wxButton * cancel = new wxButton(this, ID_ON_CANCEL, wxT("&Cancel"),
wxDefaultPosition, wxDefaultSize);
okCancelBox->Add(cancel, , wxALIGN_CENTER_VERTICAL|wxALL, ); /**< The Help button */
wxButton * help = new wxButton(this, ID_ON_HELP, wxT("&Help"),
wxDefaultPosition, wxDefaultSize);
okCancelBox->Add(help, , wxALIGN_CENTER_VERTICAL|wxALL, );
} /**< Set the validators for the dialog controls */
void PersonalRecordDialog::SetDialogValidators(void)
{
FindWindow(ID_NAME)->SetValidator(wxTextValidator(wxFILTER_ALPHA, person.GetName())); FindWindow(ID_AGE)->SetValidator(wxGenericValidator(person.GetAge())); FindWindow(ID_SEX)->SetValidator(wxGenericValidator(person.GetSex())); FindWindow(ID_VOTE)->SetValidator(wxGenericValidator(person.GetVote()));
} //Sets the help text for the dialog controls
void PersonalRecordDialog::SetDialogHelp(void)
{
wxString nameHelp = wxT("Enter your full name.");
wxString ageHelp = wxT("Specify your age");
wxString sexHelp = wxT("Specify your gender, male or female.");
wxString voteHelp = wxT("Check this if you wish to vote."); FindWindow(ID_NAME)->SetHelpText(nameHelp);
FindWindow(ID_NAME)->SetToolTip(nameHelp); FindWindow(ID_AGE)->SetHelpText(ageHelp);
FindWindow(ID_AGE)->SetToolTip(ageHelp); FindWindow(ID_SEX)->SetHelpText(sexHelp);
FindWindow(ID_SEX)->SetToolTip(sexHelp); FindWindow(ID_VOTE)->SetHelpText(voteHelp);
FindWindow(ID_VOTE)->SetToolTip(voteHelp);
} /**< wxEVT_UPDATE_UI event handler for ID_CHECKBOX */
void PersonalRecordDialog::OnVoteUpdate(wxUpdateUIEvent & event)
{
wxSpinCtrl * ageCtrl = (wxSpinCtrl*)FindWindow(ID_AGE); if(ageCtrl->GetValue() < )
{
event.Enable(false);
event.Check(false);
}
else
{
event.Enable(true);
}
} /**< wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RESET */
void PersonalRecordDialog::OnResetClick(wxCommandEvent & event)
{
Init();
TransferDataToWindow();
} void PersonalRecordDialog::OnOkClick(wxCommandEvent & event)
{
TransferDataFromWindow();
person.Save();
this->Destroy();
} void PersonalRecordDialog::OnCancelClick(wxCommandEvent & event)
{
this->Destroy();
} void PersonalRecordDialog::OnHelpClick(wxCommandEvent & event)
{
wxString helpText = wxT("Please enter your full name, age and gender.\n")
wxT("Also indicate your willingness to vote in general elections.\n\n")
wxT("No non-alphabetical characters are allowed in the name field.\n")
wxT("Try to be honest about your age."); wxMessageBox(helpText, wxT("Personal Record Dialog Help"), wxOK|wxICON_INFORMATION, this);
}
/***************************************************************
* Name: Data.h
* Purpose: Defines Data Class
* Author: PingGe (414236069@qq.com)
* Created: 2013-11-22
* Copyright: PingGe (http://www.cnblogs.com/pingge/)
* License:
**************************************************************/ #ifndef DATA_H
#define DATA_H //(*Headers(Data)
#include <wx/wx.h>
#include <wx/file.h>
//*) class Data
{
public:
Data() : __name(wxEmptyString), __age(), __sex(MALE), __vote(wxNO) {}
virtual ~Data() {} //(*Set(Data)
void SetName(const wxString & name) {__name = name;}
void SetAge(const int & age) {__age = age;}
void SetSex(const bool & sex) {__sex = sex;}
void SetVote(const bool & vote) {__vote = vote;}
//*) //(*Get(Data)
wxString * GetName() {return &__name;}
int * GetAge() {return &__age;}
int * GetSex() {return &__sex;}
bool * GetVote() {return &__vote;}
//*) void Save() const
{
wxFile fout(wxT("person.txt"), wxFile::write);
size_t len = __name.length();
fout.Write(&len, sizeof(size_t)); fout.Write(__name);
fout.Write(&__age, sizeof(__age));
fout.Write(&__sex, sizeof(__sex));
fout.Write(&__vote, sizeof(__vote));
fout.Close();
}
void Read()
{
wxFile fin(wxT("person.txt"), wxFile::read); size_t filesize;
fin.Read(&filesize, sizeof(size_t));
if(filesize != )
{
char * buf = new char[filesize];
fin.Read((void*)buf, filesize);
__name = wxString::From8BitData(buf);
}
else
{
__name = wxEmptyString;
} fin.Read(&__age, sizeof(__age));
fin.Read(&__sex, sizeof(__sex));
fin.Read(&__vote, sizeof(__vote));
fin.Close();
} private:
enum{MALE, FEMALE}; wxString __name;
int __age;
int __sex;
bool __vote;
}; #endif // DATA_H
由于本人水平有限,有些地方写的不好,望多多指教
wxWidgets一个界面与数据分离的简单例子的更多相关文章
- Robot Framework与Web界面自动化测试学习笔记:简单例子
假设环境已经搭建好了.这里用RIDE( Robot Framework Test Data Editor)工具来编写用例.下面我们对Robot Framework简称rf. 我们先考虑下一个最基本的登 ...
- ABAP 通过sumbit调用另外一个程序使用job形式执行-简单例子
涉及到两个程序: ZTEST_ZUMA02 (主程序) ZTEST_ZUMA(被调用的程序,需要以后台job执行) "ztest_zuma 的代码 DATA col TYPE i VALUE ...
- [Machine-Learning] 一个线性回归的简单例子
这篇博客中做一个使用最小二乘法实现线性回归的简单例子. 代码来自<图解机器学习> 图3-2,使用MATLAB实现. 代码link 用到的matlab函数 由于以前对MATLAB也不是非常熟 ...
- 解析大型.NET ERP系统 界面与逻辑分离
Windows Forms程序实现界面与逻辑分离的关键是数据绑定技术(Data Binding),这与微软推出的ASP.NET MVC的原理相同,分离业务代码与界面层,提高系统的可维护性. 数据绑定 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- .Net MVC&&datatables.js&&bootstrap做一个界面的CRUD有多简单
我们在项目开发中,做得最多的可能就是CRUD,那么我们如何在ASP.NET MVC中来做CRUD呢?如果说只是单纯实现功能,那自然是再简单不过了,可是我们要考虑如何来做得比较好维护比较好扩展,如何做得 ...
- jQuery MiniUI开发系列之:UI和数据分离
使用MiniUI需要注意:UI和数据是分离的. 传统的WEB开发,开发者经常将数据库操作.服务端业务.HTML标签写在一个页面内. 这样会造成开发的混乱,并且难以维护和升级. 使用MiniUI开发的时 ...
- 【web前端面试题整理07】我不理解表现与数据分离。。。
拜师传说 今天老夫拜师了,老夫有幸认识一个JS高手,在此推荐其博客,悄悄告诉你,我拜他为师了,他承诺我只收我一个男弟子..... 师尊刚注册的账号,现在博客数量还不多,但是后面点会有干货哦,值得期待. ...
- Robot Framework--05 案例设计之流程与数据分离
转自:http://blog.csdn.net/tulituqi/article/details/7651049 这一讲主要说一下案例设计了.还记得我们前面做的case么?先打开浏览器访问百度,输入关 ...
随机推荐
- CentOS7修改网卡为eth0
CentOS7修改网卡为eth0 1.编辑网卡信息 [root@linux-node2~]# cd /etc/sysconfig/network-scripts/ #进入网卡目录 [root@lin ...
- NFS,FTP
一. NFS1. NFS简介NFS全称是network file systemNFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件. 假 ...
- jQuery插件综合应用(一)注册
一.介绍 注册和登录是每个稍微有点规模的网站就应该有的功能.登陆功能与注册功能类似,也比注册功能要简单些.所以本文就以注册来说明jQuery插件的应用. jQuery插件的使用非常简单,如果只按照jQ ...
- jquery阻止事件的两种实现方式
再阻止事件冒泡的方面,jquery有两种方式: 一种是 return false;另外一种是 e.stopPropagation() html代码 <form id="form1&qu ...
- php练习7——类的运用(四则运算or面积计算[javascript小技巧——根据需求显示不同界面])
要求:请编写一个类,该类可以进行四则运算,也可以进行矩形面积计算 1.程序 viewCount.html Count.class.php printCount.php 2.结果 ...
- 【pyhton】成员资格运算符
>>> name = '小甲鱼' >>> '鱼' in name True >>> '肥鱼' in name False 来自小甲鱼的课后习题
- 《C和指针》章节后编程练习解答参考——第10章
10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...
- OC - 正则表达式 - RegexKitLite
正则表达式使用步骤: 1. 创建正则表达式对象, 设置约束条件; NSString *pattern = @"\\d{1,3}"; NSRegularExpression *reg ...
- ubuntu14.04下unix网络编程环境的配置
建议 unpv13e/README看一下,忽略一下内容 ===================================================================== 操作 ...
- redis百度百科和维基百科知识总结:
1. 百度百科知识总结: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis ...