【wxWidgets教程】窗体:wxFrame创建与使用技巧

继以《wxWidgets应用程序:代码结构与生命周期》为核心的多篇关于wxWidgets应用程序的文章之后,本文将继续介绍wxWidgets的主界面元素“窗体wxFrame”。窗体即GUI应用程序的主窗口。wxWidgets教程完整目录

窗体类wxFrame和应用程序类一样,是wxWidgets程序的重要组成部分。从某种意义上讲,wxWidgets应用程序=应用程序类+窗体类。与应用程序类一样,在程序开发中使用窗体类的主要方式是创建一个wxFrame的派生类MyFrame,然后在MyFrame中构造函数中创建窗体的各类控件及其它界面元素。

一、wxFrame定位

(二)功能定位

在wxWidgets应用程序中,wxFrame充当GUI控件控件的容器,是应用程序的主窗口。

(一)体系定位

wxWidgets库中定义的窗体体系如下:

备注:灰色部分的类表示该类有派生类,图中未画出其派生类。

图 20 wxFrame类图

二、使用窗体

wxFrame是一个可以包含其他窗口,并且大小可变的窗口类。MyApp::OnInit()的基本任务就是创建一个wxFrame类型的对象。尽管不是必须,但在通常情况下,我们都有必要创建一个派生自wxFrame的窗口对象。

(一)创建窗体

创建一个自定义窗体类MyFrame,头文件:

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
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() 

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!");
}

(二)调用窗体

然后在MyApp::OnInit()中创建并显示MyFrame。

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

也可以使用wxFrame的Create()函数创建窗体,Create()与该构造函数拥有相同的参数,二者在功能是等价的,示例如下:

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

程序运行效果如下图所示:

三、示例代码下载

本文示例代码可在《wxWidgets窗体:掌握wxFrame创建与使用技巧》示例代码下载。

四、总结

窗体是wxWidigets GUI应用程序最重要的界面容器。本文对其进行全面而初步介绍,旨在为窗体的正确、快捷使用提供入门指南。

关于窗体创建过程中各类参数的详细说明,请参考《wxWidgets窗体:wxFrame构造函数详解》一文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万法若空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值