天天看点

wxWidgets+CodeBlocks 官方HelloWorld程序

一、环境搭建

wxWidgets+CodeBlocks 环境搭建过程推荐大家参考博客:

http://hi.baidu.com/aklongke/item/6e64fbdbb009533a49e1dd36

http://blog.csdn.net/joliny/article/details/3505566

另外值得注意的:

1. wxWidgets和CodeBlocks版本是否符合。例如笔者在安装时使用wxWidgets-3.0.0和CodeBlocks 12.11失败。

看到下图时,恍然大悟:CodeBlocks 12.11不支持wxWidgets-3.0.0

wxWidgets+CodeBlocks 官方HelloWorld程序

2.字符格式设置

程序开发难免会用到中文,加入GBK 。

执行“Settings——Complier...”

wxWidgets+CodeBlocks 官方HelloWorld程序

3.加入wxWidgets路径

wxWidgets+CodeBlocks 官方HelloWorld程序

二、HelloWorld 工程

1.创建wxWidgets工程,并删除所有工程文件(只是借用wxWidgets工程配置)

2.新建"Hello.cpp"文件,并敲入如下代码(用的是wxWidgets-3.0.0-docs-chm中的代码)。

// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

class MyApp: public wxApp
{
public:
    virtual bool OnInit();
};

class MyFrame: public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    wxDECLARE_EVENT_TABLE();
};

enum
{
    ID_Hello = 1
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Hello,   MyFrame::OnHello)
    EVT_MENU(wxID_EXIT,  MyFrame::OnExit)
    EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
    frame->Show( true );
    return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)        : wxFrame(NULL, wxID_ANY, title, pos, size)
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );
    menuBar->Append( menuHelp, "&Help" );
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( "Welcome to wxWidgets!" );
}

void MyFrame::OnExit(wxCommandEvent& event)
{
    Close( true );
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox( "This is a wxWidgets' Hello world sample", "About Hello World", wxOK | wxICON_INFORMATION );
}

void MyFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}
           

3. 编译

出现很多错误。一一解决。

3.1 调试错误:

23:27: error: ISO C++ forbids declaration of 'wxDECLARE_EVENT_TABLE' with no type [-fpermissive]
           

解决:将wxDECLARE_EVENT_TABLE()、wxBEGIN_EVENT_TABLE()、wxEND_EVENT_TABLE()前缀wx去掉

3.2 调试错误:

37:23: error: expected constructor, destructor, or type conversion before ';' token
           

解决:将wxIMPLEMENT_APP(MyApp)改为IMPLEMENT_APP(MyApp),并在其前加DECLARE_APP(MyApp)

3.3 调试错误:

41:84: error: conversion from 'const char [12]' to 'const wxString' is ambiguous
           

解决:所有字符串使用wxT()。如wxT("Hello World")

参考网址:http://wiki.wxwidgets.org/WxString

修改之后,编译成功。

4.运行

wxWidgets+CodeBlocks 官方HelloWorld程序