2008年1月29日 星期二

[程式設計] 跨平台 GUI-Library -- wxWidgets

在 Eee PC 上寫程式嗎? 可以試試 wxWidgets ....

(官方網站)

wxWidgets 是一組 GUI 程式庫, 用它來寫的視窗程式幾乎可以在不用修改的原始碼的情況下, 在 Win32, Mac OS, GTK+, X11, Motif, WinCE 等作業平台上重新編譯並且執行. 讓你的應用程式GUI介面瞬間變成跨平台程式.

你可以用 C++, Python, Perl, C#/.Net 等不同的程式語言開發 wxWidgets based 應用程式.  ~ 酷.

   

 

或許你覺得使用 Java 也可以達到跨平台的功能. 但是使用 wxWidgets 則是在底層直接使用 該平台的原生視窗程式碼, 而且編譯出來的是執行檔不需要虛擬機器. 所以效能上當然比 Java 應用程式高出許多. 對於目標裝置需要高效率的 3D 程式或遊戲以及對記憶體要求比較嚴格的平台. wxWidgets 是可以考慮的選項.

 

下面程式碼是最簡單的手寫視窗程式: Hello World

參考自 a tiny tutorial by Robert Roebling

----------------------  code ----------------------

#include "wx/wx.h"

// 宣告的部分: 應用程式

class MyApp: public wxApp {
    virtual bool OnInit();  // <----- 等一下, 程式一開始會從這裡執行,

                                                  //             建立 MyFrame 視窗
};

// 宣告的部分: 一個視窗類別

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()

 

// 實作的部分: 應用程式起始的部分 ~~ main

// 每一個程式都有 main , 而 xwWidgets  的 main 是包在如下的巨集裡面.

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {
    MyFrame *frame = new MyFrame( _T("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, _T("&About...") );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, _T("E&xit") );

    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _T("&File") );

    SetMenuBar( menuBar );

    CreateStatusBar();
    SetStatusText( _T("Welcome to wxWidgets!") );
}

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

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

----------------------  end of code ----------------------

 

 

by Jing

 

References

[1] 用 xwWidgets 開發 Eee PC 的程式範例 (link)

[2] 利用 BCB IDE 整合在一起的 xwVCL (link)

[3] 如何設定 Dev-C++ 搭配 xwWidgets  (link)

[4] 如何利用 Visual Studio C++ 寫 xwWidgets   程式 (link)

[5] 如何搭配 OpenCV 播放攝影機傳回來的影像串流 (link)

 

 

沒有留言:

張貼留言