/***************************************************************
* 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一个界面与数据分离的简单例子的更多相关文章

  1. Robot Framework与Web界面自动化测试学习笔记:简单例子

    假设环境已经搭建好了.这里用RIDE( Robot Framework Test Data Editor)工具来编写用例.下面我们对Robot Framework简称rf. 我们先考虑下一个最基本的登 ...

  2. ABAP 通过sumbit调用另外一个程序使用job形式执行-简单例子

    涉及到两个程序: ZTEST_ZUMA02 (主程序) ZTEST_ZUMA(被调用的程序,需要以后台job执行) "ztest_zuma 的代码 DATA col TYPE i VALUE ...

  3. [Machine-Learning] 一个线性回归的简单例子

    这篇博客中做一个使用最小二乘法实现线性回归的简单例子. 代码来自<图解机器学习> 图3-2,使用MATLAB实现. 代码link 用到的matlab函数 由于以前对MATLAB也不是非常熟 ...

  4. 解析大型.NET ERP系统 界面与逻辑分离

    Windows Forms程序实现界面与逻辑分离的关键是数据绑定技术(Data Binding),这与微软推出的ASP.NET MVC的原理相同,分离业务代码与界面层,提高系统的可维护性. 数据绑定 ...

  5. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  6. .Net MVC&&datatables.js&&bootstrap做一个界面的CRUD有多简单

    我们在项目开发中,做得最多的可能就是CRUD,那么我们如何在ASP.NET MVC中来做CRUD呢?如果说只是单纯实现功能,那自然是再简单不过了,可是我们要考虑如何来做得比较好维护比较好扩展,如何做得 ...

  7. jQuery MiniUI开发系列之:UI和数据分离

    使用MiniUI需要注意:UI和数据是分离的. 传统的WEB开发,开发者经常将数据库操作.服务端业务.HTML标签写在一个页面内. 这样会造成开发的混乱,并且难以维护和升级. 使用MiniUI开发的时 ...

  8. 【web前端面试题整理07】我不理解表现与数据分离。。。

    拜师传说 今天老夫拜师了,老夫有幸认识一个JS高手,在此推荐其博客,悄悄告诉你,我拜他为师了,他承诺我只收我一个男弟子..... 师尊刚注册的账号,现在博客数量还不多,但是后面点会有干货哦,值得期待. ...

  9. Robot Framework--05 案例设计之流程与数据分离

    转自:http://blog.csdn.net/tulituqi/article/details/7651049 这一讲主要说一下案例设计了.还记得我们前面做的case么?先打开浏览器访问百度,输入关 ...

随机推荐

  1. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  2. GDI画验证码

    Random r = new Random(); string str = ""; for (int i = 0; i < 5; i++) { int a= r.Next(0 ...

  3. PHP源码阅读笔记一(explode和implode函数分析)

    PHP源码阅读笔记一一.explode和implode函数array explode ( string separator, string string [, int limit] )此函数返回由字符 ...

  4. SQL函数:小写金额转换成大写

    /********************************************************作者:版本:1.0创建时间:20020227修改时间:功能:小写金额转换成大写参数:n ...

  5. 双积分式(A/D)转换器电路结构及工作原理

    1.转换方式 V-T型间接转换ADC. 2.  电路结构 图1是这种转换器的原理电路,它由积分器(由集成运放A组成).过零比较器(C).时钟脉冲控制门(G)和计数器(ff0-ffn)等几部分组成 图1 ...

  6. jquery插件的写法

    jquery插件及zepto插件,写法上有些区别. 区别点: 1.自定义事件的命名空间 jq的时间命名空间是用点“.”,而zepto是用冒号“:” 如 //jquery $(this).trigger ...

  7. Xcode 设置输出颜色

    是不是看厌了Xcode单调的Log,在此,我教大家实现带颜色的Log 一 首先你需要安装一个Xcode插件---XCode Colors 插件Github地址 https://github.com/r ...

  8. Codeforces Round #197 (Div. 2) : A

    水题一个: 直接贴代码: #include<cstdio> #include<algorithm> #include<cstring> using namespac ...

  9. 【Uvalive4960】 Sensor network (苗条树,进化版)

    [题意] 给出N个点,M条边,问这N个点形成的生成树的最大权值边-最小权值边的最小值 InputThe input consists of several test cases, separated ...

  10. 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

    The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...