---
title: "wxwidgets-学习心得(2)"
author: "deepfuture"
date: "2013-05-14T22:17:32Z"
tags: []
categories: ["编程语言"]
slug: "wxwidgets-2"
draft: false
lastmod: "2013-05-14T22:17:32Z"
id: 1869482
url: "https://deepfuture.iteye.com/blog/1869482"
---

```
#include <wx/wx.h>
#include <ctime>
using namespace std;
// 应用程序类
class wxMyApp : public wxApp
{
public:
 // 虚函数
 virtual bool OnInit();
};



//窗口类
class wxMyFrame:public wxFrame
{
 public:
        //主窗口    
     wxMyFrame(const wxString &title);
        //事件处理函数   
  void OnQuit(wxCommandEvent& event);
  void OnAbout(wxCommandEvent& event); 
  void OnOpen(wxCommandEvent& event); 
};
#include "mywx.xpm"
//构造函数
wxMyFrame::wxMyFrame(const wxString& title):wxFrame(NULL,wxID_ANY,title)
{
    
    //设置窗口图标
 SetIcon(wxIcon(mywx_xpm));
 //创建菜单条
 wxMenu *fileMenu=new wxMenu;
 wxMenu *helpMenu=new wxMenu;
 fileMenu->Append(wxID_OPEN,wxT("&Open file...\tALT-O"),wxT("现在时间"));
 helpMenu->Append(wxID_EXIT,wxT("E&xit\tAlt-X"),wxT("退出程序"));
 //将菜单项加到菜单条。
 wxMenuBar *menuBar=new wxMenuBar();
 menuBar->Append(fileMenu,wxT("&File"));
 menuBar->Append(helpMenu,wxT("&Help"));
 //将菜单条放置到主窗口
 SetMenuBar(menuBar);
 //创建状态条
 CreateStatusBar(2);
 SetStatusText(wxT("我的第二个WX程序"));
    // 创建一个按钮  
    new wxButton(this, wxID_ABOUT, wxT("关于我") );  
  
    // 与事件处理器连接
    Connect(wxID_ABOUT,wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxMyFrame::OnAbout) );  
 Connect(wxID_EXIT,wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxMyFrame::OnQuit) );  
 Connect(wxID_OPEN,wxEVT_COMMAND_MENU_SELECTED , wxCommandEventHandler(wxMyFrame::OnOpen) );  
}
//窗口类的事件处理函数 
void wxMyFrame::OnQuit(wxCommandEvent& event)
{
        wxMessageBox(wxT("<<deepfuture.iteye.com>>wxwidgets欢迎您再来"),wxT("BYE,不要想我噢~"),wxOK|wxICON_INFORMATION,this);
  Close();
}
void wxMyFrame::OnAbout(wxCommandEvent& event)
{
  wxString msg;
  msg.Printf(wxT("<<deepfuture.iteye.com>>wxwidgets版本号：%s"),wxVERSION_STRING);
  wxMessageBox(msg,wxT("我的第二个WX程序"),wxOK|wxICON_INFORMATION,this);
}
void wxMyFrame::OnOpen(wxCommandEvent& event)
{
        wxMessageBox(wxT("wxwidgets欢迎您"),wxT("文件已经打开~"),wxOK|wxICON_INFORMATION,this);
}
bool wxMyApp::OnInit()
{
 wxMyFrame *frame=new wxMyFrame(wxT("第二个wx程序"));
 frame->Show(true);
 return true;
}
IMPLEMENT_APP(wxMyApp);

```

 

![](http://dl.iteye.com/upload/attachment/0084/4876/4d7f0895-89d3-368d-a830-9e592a4061e8.jpg)