1. 程式人生 > >Windows使用WxWidgets開發介面(c++)環境搭建

Windows使用WxWidgets開發介面(c++)環境搭建

一直想學習wxWidgets,之前使用的都是wxPython,現在終於鼓起勇氣學習這個了,發現原來是基於vc6.0開發的。所以最好的學習辦法就是安裝vistual studio 2010,方便學習看程式碼。wxWidgets裡面也有demo。

基本上wxWidgets是和MFC類似的。

Similarity to MFC

MFC and wxWidgets macros

MFC version wxWidgets version
BEGIN_MESSAGE_MAP BEGIN_EVENT_TABLE
END_MESSAGE_MAP END_EVENT_TABLE
DECLARE_DYNAMIC
DECLARE_CLASS
DECLARE_DYNCREATE DECLARE_DYMAMIC_CLASS
IMPLEMENT_DYNAMIC IMPLEMENT_CLASS
IMPLEMENT_DYNCREATE IMPLEMENT_DYNAMIC_CLASS
IsKindOf(RUNTIME_CLASS(CWindow)) IsKindOf(CLASSINFO(wxWindow))

MFC and wxWidgets classes


非常經典的WxWidgets架構圖。

1,下載

下載安裝檔案
http://sourceforge.net/projects/wxwindows/files/

安裝檔案:
wxWidgets-2.8.12(特別注意下不要使用開發版本,要使用穩定版本)

2,開發環境使用 visual studio 2010 



安裝IDE參考:


http://blog.csdn.net/freewebsys/article/details/12028265


3,安裝wxwdiget(基於原始碼編譯安裝)



下載zip檔案
開啟工程D:/wxWidgets-2.8.12/build/msw/wx.dsw

可以使用exe安裝,(相當於解壓縮原始碼,不是安裝)也可以使用zip解壓縮,兩個都是將原始碼放到一個目錄,沒有啥區別。



然後編譯,否則不能使用!!!!!!
wxWidgets使用的是vc6.0開發的,需要轉換成vistual studio 2010。(IDE會自動轉換的,耐心等待)


轉換完成之後直接build就行了。


編譯完成沒有任何錯誤資訊。說明安裝成功。



4,編譯demo



在安裝後的wxWidgets-2.9.5/samples目錄下面有samples.dsw
雙擊就可以啟動visual studio了,同樣需要轉換成工程。


繼續等待。轉換完成之後就可以編譯了。


裡面的demo都可以編譯成功,執行如下:


轉換完成之後就可以進行編譯了。發現編譯有4個失敗的。不過沒有關係大多數成功了。

執行一個demo:


豐富的demo找了幾個執行下效果如下:

一個簡單的播放器

一個所有元件的demo。比較全。


5,WxWidgets的一個helloworld

/*
 * hworld.cpp
 * Hello world sample by Robert Roebling
 */
 
#include "wx/wx.h" 
 

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

 
class MyFrame: public wxFrame
{
public:
 
    MyFrame(const wxString& title, 
           const wxPoint& pos, const wxSize& size);

    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
 
    DECLARE_EVENT_TABLE()
};

enum
{
    ID_Quit = 1,
    ID_About,

};
 
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
 
IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( "Hello World", 
         wxPoint(50,50), wxSize(450,340) );
    frame->Show(TRUE);
    SetTopWindow(frame);
    return TRUE;
} 
 
MyFrame::MyFrame(const wxString& title, 
       const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append( ID_About, "&About..." );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, "E&xit" );
 
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );
 
    SetMenuBar( menuBar );

    CreateStatusBar();
    SetStatusText( "Welcome to wxWindows!" );
}

 
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close(TRUE);
}
 
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{

    wxMessageBox("This is a wxWindows Hello world sample",
        "About Hello World", wxOK | wxICON_INFORMATION, this);
}


其他的繼續研究。。。

其他參考:

http://www.cnzui.com/archives/962

http://blog.csdn.net/chinabinlang/article/details/6904143
http://www.codeproject.com/Articles/11515/Introduction-to-wxWidgets