最簡單的wxWidgets程式

來源:互聯網
上載者:User

開始學習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!"));
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.