開始學習wxWidgets,看的是Cross-Platform GUI Programming.pdf這個電子書,說的很詳細。按照他上面的程式,敲了半天,出了好幾個錯誤,都是打字錯誤。
還好找到了,現在 代碼如下。
/*********************************************************************
* Name: main.cpp
* Purpose: Implements simple wxWidgets application with GUI.
* Author: hwh
* Created: 2011/11/24
* Copyright:
* License: wxWidgets license (www.wxwidgets.org)
*
* Notes:
*********************************************************************/
/*
#include <wx/wx.h>
// application class
class wxMiniApp : public wxApp
{
public:
// function called at the application initialization
virtual bool OnInit();
// event handler for button click
void OnClick(wxCommandEvent& event) { GetTopWindow()->Close(); }
};
IMPLEMENT_APP(wxMiniApp);
bool wxMiniApp::OnInit()
{
// create a new frame and set it as the top most application window
SetTopWindow( new wxFrame( NULL, -1, wxT(""), wxDefaultPosition, wxSize( 100, 50) ) );
// create new button and assign it to the main frame
new wxButton( GetTopWindow(), wxID_EXIT, wxT("Click!") );
// connect button click event with event handler
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxMiniApp::OnClick) );
// show main frame
GetTopWindow()->Show();
// enter the application's main loop
return true;
}
*/
#include "wx/wx.h"
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
// 主視窗類的建構函式
MyFrame(const wxString& title);
// 事件處理函數
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// 聲明事件表
DECLARE_EVENT_TABLE()
};
// 有了這一行就可以使用MyApp& wxGetApp()了
DECLARE_APP(MyApp)
// 告訴主應用程式是哪個類
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
// 建立主視窗
MyFrame * frame = new MyFrame(wxT("Minimal wxWidgets App"));
// 顯示主視窗
frame->Show(true);
// 開始事件處理迴圈
return true;
}
// 類的事件表MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("Hello and welcom to %s"), wxVERSION_STRING);
wxMessageBox(msg, wxT("About Minimal"), wxOK | wxICON_INFORMATION, this);
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
// 釋放主視窗
Close();
}
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// 建立菜單條
wxMenu * fileMenu = new wxMenu;
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quiz this program"));
// 添加“關於”菜單
wxMenu * helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"), wxT("Show about dialog"));
// 將功能表項目添加到菜單條中
wxMenuBar * menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&help"));
// 然後將菜單條放置在主視窗上
SetMenuBar(menuBar);
// 建立一個狀態條來讓一切更有趣些
CreateStatusBar(2);
SetStatusText(wxT("Welcom to wxWidegets!"));
}