最简单的wxWidgets程序

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值