Re: [問題] 請幫幫我找到程式進入點.....
看板C_and_CPP (C/C++)作者kvykn (simple life)時間16年前 (2009/03/22 14:20)推噓1(1推 0噓 1→)留言2則, 2人參與討論串3/3 (看更多)
我舉個基本小程式當範例說明好了 一個基本的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
推
03/22 15:59, , 1F
03/22 15:59, 1F
→
03/22 16:22, , 2F
03/22 16:22, 2F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章