开始学习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!"));
}