作者kvykn (simple life)
看板C_and_CPP
标题Re: [问题] 请帮帮我找到程式进入点.....
时间Sun Mar 22 14:20:43 2009
我举个基本小程式当范例说明好了 一个基本的wx程式开起来会有两个类别
一个是App类别
另一个是Frame类别
观念是视窗只是个互动式的介面 在视窗上面做的所有事情都叫做事件(event)
包括你可能用滑鼠点了file或open 这都是event
当某个event被触发 程式就会做下个动作
例如 你点了open 跑出点选档案视窗 然後你选了某个档案按下确定
而这些event会有一个处理event的机制去管理 这叫event handling
视窗跟使用者还有视窗跟视窗本身就是靠着这个东西来互动...
每一个wx程式一定都会有一个App类别(也只能有一个)
而你所想知道的程式进入点就是那个OnInit();
App类别架构如下
// 宣告
class mainloopApp : public wxApp
{
public:
virtual bool OnInit();
};
OnInit()的实作内容就是等效於main()里面的内容 稍微看一下内容大概可以知道
OnInit() new一个Frame 然後秀出来 return值为真 程式开始进入event loop
若return为否 就中止程式
这跟main()的差别很大
// 实作
IMPLEMENT_APP(mainloopApp);
bool mainloopApp::OnInit()
{
//(*AppInitialize
bool wxsOK = true;
wxInitAllImageHandlers();
if ( wxsOK )
{
mainloopFrame* Frame = new mainloopFrame(0);
Frame->Show();
SetTopWindow(Frame);
}
//*)
return wxsOK;
}
接下来看的是视窗的骨架 举凡你看的到的看不到的元件都要写在这里面
按钮啊 功能表啊 图框啊 什麽的都在这边规划 当然一个一个自己刻很累人
所以有工具 code::blocks 有内建 wxsmith 可以让你作视窗 并且把程式码一并写入
这很方便 不过你也要了解他帮你写了什麽东西比较容易进步
// Frame宣告部份
class mainloopFrame: public wxFrame
{
public:
mainloopFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~mainloopFrame();
private:
//(*Handlers(mainloopFrame)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
//*)
//(*Identifiers(mainloopFrame)
static const long idMenuQuit;
static const long idMenuAbout;
static const long ID_STATUSBAR1;
//*)
//(*Declarations(mainloopFrame)
wxStatusBar* StatusBar1;
//*)
DECLARE_EVENT_TABLE()
};
从这Frame的建构子可以了解 整个视窗的架构 我不做解释了
// 实作部份 不过之前还有一小段event table 不过我不常用 我都用connect()
mainloopFrame::mainloopFrame(wxWindow* parent,wxWindowID id)
{
//(*Initialize(mainloopFrame)
wxMenuItem* MenuItem2;
wxMenuItem* MenuItem1;
wxMenu* Menu1;
wxMenuBar* MenuBar1;
wxMenu* Menu2;
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE, _T("id"));
MenuBar1 = new wxMenuBar();
Menu1 = new wxMenu();
MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit
the application"), wxITEM_NORMAL);
Menu1->Append(MenuItem1);
MenuBar1->Append(Menu1, _("&File"));
Menu2 = new wxMenu();
MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show
info about this application"), wxITEM_NORMAL);
Menu2->Append(MenuItem2);
MenuBar1->Append(Menu2, _("Help"));
SetMenuBar(MenuBar1);
StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
int __wxStatusBarWidths_1[1] = { -1 };
int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
SetStatusBar(StatusBar1);
Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&mainloopFrame::OnQuit);
Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&mainloopFrame::OnAbout);
//*)
}
mainloopFrame::~mainloopFrame()
{
//(*Destroy(mainloopFrame)
//*)
}
void mainloopFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
void mainloopFrame::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
以上两个OnXxxxxx method是按钮的实作 一个实作了关闭视窗 一个秀出wx的资讯
当然你也可以做一个按钮 用connect去撷取当滑鼠按下按钮的event
按下按钮的执行内容就简单多了阿 就跟一般写程式一样
看你是要计算1+1=2
还是要Hello world之类的
咳咳 小结论来了 因为这种event loop 的架构 很多地方都可以是entry
视窗这种是跟使用者互动的程式 在OnInit写太多东西也没什麽好处啦
个人小经验 有说错的话请不吝指正
reference wxwidgets的官方文件
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.118.7.163
1F:推 Alexboo:你真是个好人 03/22 15:59
2F:→ kvykn:楼上这样说我就想删掉这一篇欸 03/22 16:22