wxWidgets学习笔记02

Writing Your First Application

1.Introduction

//base.h
#ifndef __BASE_H 
// Make sure to only declare these classes once 
#define __BASE_H 

class MainApp: public wxApp // MainApp is the class for our application 
{
	// MainApp just acts as a container for the window, or frame in MainFrame
public:
	virtual bool OnInit(); 
};

class MainFrame: public wxFrame // MainFrame is the class for our window, 
{ 
	// It contains the window and all objects in it 
public: 
	MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size); 
}; 

DECLARE_APP(MainApp)

#endif
//base.c
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#	include <wx/wx.h>
#endif 
#include "base.h"

IMPLEMENT_APP(MainApp) // A macro that tells wxWidgets to create an instance of our application 

bool MainApp::OnInit() 
{
	// Create an instance of our frame, or window 
	MainFrame *MainWin = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(300, 200));
	MainWin->Show(true); // show the window 
	SetTopWindow(MainWin); // and finally, set it as the main window 
	return true;
} 

MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size) 
: wxFrame((wxFrame *) NULL, -1, title, pos, size) 
{
	// normally we would initialize objects such as buttons and textboxes here 
}

在这里插入图片描述

What Does This Code Do?

wxApp类是应用程序本身。 它处理我们应用程序的属性,并检查事件(例如单击按钮)。 基本上,它控制着我们的应用程序。

class MainApp: public wxApp // MainApp is the class for our application
{
	// MainApp just acts as a container for the window, or frame in MainFrame
public:
	virtual bool OnInit();
};

WxFrame是我们将要注意的主要类,wxFrame表示一个具有自己的宽度,高度,样式和对象的窗口。 这是你在启动该程序时看到的内容,尽管我们的内容非常空白。

MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size) 
: wxFrame((wxFrame *) NULL, -1, title, pos, size) 
{
	// normally we would initialize objects such as buttons and textboxes here 
}

现在,让我们看一下以下初始化代码:

bool MainApp::OnInit() 
{
	// Create an instance of our frame, or window 
	MainFrame *MainWin = new MainFrame(_("Hello World!"), wxDefaultPosition, wxSize(300, 200));
	MainWin->Show(true); // show the window 
	SetTopWindow(MainWin); // and finally, set it as the main window 
	return true;
}

MainFrame()的第一个参数非常简单,只是框架的标题。 下一个参数是窗口将显示的位置。 我们使用默认位置,但是如果你希望它出现在屏幕上的其他位置,则可以使用wxPoint(int x, int y) 函数。 (例如,要在屏幕的左上角显示它,可以使用wxPoint(0,0))。第三个参数是我们窗口的大小; 我不会为此烦恼,因为这很容易说明。 创建MainFrame类的实例之后,我们仅显示该窗口并将其设置为主窗口。 不需要最后一步,并且如果不使用它,则将第一帧设置为主窗口,但是最好有。

2.Adding A Button

当你在Windows或任何多任务操作系统下使用应用程序时,总是单击按钮,使用菜单等。每当你这样做时,都会触发一个事件,并且操作系统会向该程序发送一条消息。 程序初始化时,它会告诉OS如果发生这些事件之一,则需要执行什么操作,并在接收到事件时执行适当的功能。 因此,当你单击按钮时,你的OS会告诉你正在运行的程序,这是发生了什么,该程序告诉OS执行一个功能,然后运行该功能。

下面代码显示一个带有一个大按钮的窗口,单击该按钮将关闭该窗口。

//base.h 
#ifndef __BASE_H // Make sure to only declare these classes once
  #define __BASE_H
    class MainApp: public wxApp // MainApp is the class for our application
    { 
    // MainApp just acts as a container for the window,
    public: // or frame in MainFrame
      virtual bool OnInit();
    };
    
    class MainFrame: public wxFrame // MainFrame is the class for our window,
    {
    // It contains the window and all objects in it
    public:
      MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );
      wxButton *HelloWorld;
      void OnExit( wxCommandEvent& event );
  
      DECLARE_EVENT_TABLE()
    };
  
    enum
    {
      BUTTON_Hello = wxID_HIGHEST + 1 // declares an id which will be used to call our button
    };
 
  #endif
//base.c 
#include <wx/wxprec.h>
  #ifndef WX_PRECOMP
    #include <wx/wx.h>
  #endif
  #include "base.h"
 
  IMPLEMENT_APP(MainApp) // Initializes the MainApp class and tells our program
  // to run it
  bool MainApp::OnInit()
  {
    // Create an instance of our frame, or window
    MainFrame *MainWin = new MainFrame(_T("Hello World!"), wxPoint(1, 1), wxSize(300,200));
    MainWin->Show(TRUE); // show the window
    SetTopWindow(MainWin); // and finally, set it as the main window
 
   return TRUE;
  } 
 
  BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
    EVT_BUTTON ( BUTTON_Hello, MainFrame::OnExit ) // Tell the OS to run MainFrame::OnExit when
  END_EVENT_TABLE() // The button is pressed

  MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize
    &size): wxFrame((wxFrame*)NULL,  - 1, title, pos, size)
  {
    HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"),
      // shows a button on this window
    wxDefaultPosition, wxDefaultSize, 0); // with the text "hello World"
  }
 
  void MainFrame::OnExit( wxCommandEvent& event )
  {
    Close(TRUE); // Tells the OS to quit running this process
  }

在这里插入图片描述

这段代码声明了我们的按钮将使用的ID,将其设置为wxID_HIGHEST + 1,以避免它与wxWindows已经声明的默认ID之一相同。

 enum
 {
   BUTTON_Hello = wxID_HIGHEST + 1 // declares an id which will be used to call our button
 };

这段代码定义了当持有BUTTON_Hello ID的按钮发生EVT_BUTTON类型的事件或单击按钮时,应执行函数MainFrame :: OnExit。

  BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
  EVT_BUTTON ( BUTTON_Hello, MainFrame::OnExit ) // Tell the OS to run MainFrame::OnExit when
  END_EVENT_TABLE() // The button is pressed

这段代码添加了一个按钮,其parent设置为“ this”(或MainFrame),以便它显示在此框架上,其ID设置为BUTTON_Hello,我们已在前面声明了它,一些默认值,标签设置为“ Hello World” ,最后一个参数是默认样式。最后一个参数也可以是几种不同的样式。 以下是一些(来自手册):

  • wxBU_LEFT: 将标签左对齐。 仅限于WIN32。
  • wxBU_TOP :将标签对准按钮的顶部。 仅限于WIN32。
  • wxBU_RIGHT: 右对齐位图标签。 仅限于WIN32。
  • wxBU_BOTTOM: 将标签对准按钮的底部。 仅限于WIN32。
  • wxBU_EXACTFIT: 创建尽可能小的按钮,而不是使其达到标准大小(这是默认行为)。
HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"), // shows a button on this window
wxDefaultPosition, wxDefaultSize, 0);                   // with the text "hello World"

最后的代码很简单,我们告诉wxWindows在单击按钮时运行的函数。 我们只是运行Close()命令来退出程序。

  void MainFrame::OnExit()
  {
    Close(TRUE); // Tells the OS to quit running this process
  }

3.Using The WxTextCtrl

首先,让我们添加一个状态栏! 这并不像听起来那样难,实际上可以用一行简单的代码来完成:

  MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size) \
                       : wxFrame((wxFrame *) NULL, -1, title, pos, size)
  {
  
  CreateStatusBar(2);
  
  ...
  
  }

第一个也是唯一的参数是状态栏可以包含多少部分。 如果您只想一次在状态栏中显示一条消息,请将其设置为“ 1”;如果您希望更多,则将其设置为另一条数字。 现在,让我们用一个漂亮的文本框替换这个难看的按钮。

//base.h 
...
  
  class MainFrame: public wxFrame // MainFrame is the class for our window,
  { // It contains the window and all objects in it
  
  public: 
  
    MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );
    wxTextCtrl *MainEditBox;
  
  ...
  
  };
//base.c
  ...
  
  MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
  : wxFrame((wxFrame *) NULL, -1, title, pos, size)
  {
  // Initialize our text box with an id of TEXT_Main, and the label "hi"
  MainEditBox = new wxTextCtrl(this, TEXT_Main, "Hi!", wxDefaultPosition, wxDefaultSize,  
    wxTE_MULTILINE | wxTE_RICH , wxDefaultValidator, wxTextCtrlNameStr);
  
  ...

wxTextCtrl类具有许多有用的功能,但我们仅介绍三个。 这三个是:LoadFile(“ filename.txt”),SaveFile(“ filename.txt”)和Clear()。 您应该能够猜到LoadFile()将给定文件名的内容加载到文本控件中,SaveFile()将文本框的内容保存为给定文件名,而Clear()清除文本控件的内容。

现在添加菜单栏。 将菜单栏添加到当前框架并不难,并且菜单项的事件与我们在上一课中创建的按钮的事件相同。 首先,我们包含,并使用以下内容创建菜单栏的实例:

  MainMenu = new wxMenuBar();

现在我们需要一个菜单,从传统的文件菜单开始:

  wxMenu *FileMenu = new wxMenu();

现在我们已经声明了一个菜单和菜单栏,让我们向其中添加一些项目。 我们可以通过输入以下内容添加一个退出选项(使用MENU_Quit的ID):

  FileMenu->Append(MENU_Quit, "&Quit", "Quit the editor");
  /* Adds a menu item with the label "Quit", id of MENU_Quit, and set the status bar caption to 
  "Quit the editor" when the mouse hovers over it.
  We can also use FileMenu->AppendSeparator() to add a menu separator */

现在,我们只需将“文件”菜单附加到我们的菜单栏,并告诉框架“ MainMenu”应为其菜单栏。

  MainMenu->Append(FileMenu, "&File");
  SetMenuBar(MainMenu);

现在,如果我们使用下面的源代码,我们将得到一个看起来像这样的应用程序:

//base.h
#ifndef __BASE_H // Make sure to only declare these classes once
  #define __BASE_H
  #include <wx/frame.h>
  #include <wx/textctrl.h>

  class MainApp: public wxApp // MainApp is the class for our application
  { // MainApp just acts as a container for the window,
  public: // or frame in MainFrame
    virtual bool OnInit();
  };

  class MainFrame: public wxFrame // MainFrame is the class for our window,
  { // It contains the window and all objects in it
  public:
    MainFrame( const wxString& title, const wxPoint& pos, const wxSize& size );
    wxTextCtrl *MainEditBox;
    wxMenuBar *MainMenu;
    void Quit(wxCommandEvent& event);
    void NewFile(wxCommandEvent& event);
    void OpenFile(wxCommandEvent& event);
    void SaveFile(wxCommandEvent& event);
    void SaveFileAs(wxCommandEvent& event);
    void CloseFile(wxCommandEvent& event);

    DECLARE_EVENT_TABLE()
  };

  enum
  {
    TEXT_Main = wxID_HIGHEST + 1, // declares an id which will be used to call our button
    MENU_New,
    MENU_Open,
    MENU_Close,
    MENU_Save,
    MENU_SaveAs,
    MENU_Quit
  };

  #endif
//base.c  
#include <wx/wxprec.h>
  #ifndef WX_PRECOMP
  #include <wx/wx.h>
  #endif

  #include "base.h"

  BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
  EVT_MENU(MENU_New, MainFrame::NewFile)
  EVT_MENU(MENU_Open, MainFrame::OpenFile)
  EVT_MENU(MENU_Close, MainFrame::CloseFile)
  EVT_MENU(MENU_Save, MainFrame::SaveFile)
  EVT_MENU(MENU_SaveAs, MainFrame::SaveFileAs)
  EVT_MENU(MENU_Quit, MainFrame::Quit)
  END_EVENT_TABLE()


  IMPLEMENT_APP(MainApp) // Initializes the MainApp class and tells our program
  // to run it
  bool MainApp::OnInit()
  {
    MainFrame *MainWin = new MainFrame(wxT("Hello World!"), wxPoint(1,1),
    wxSize(300, 200)); // Create an instance of our frame, or window
    MainWin->Show(TRUE); // show the window
    SetTopWindow(MainWin);// and finally, set it as the main window

    return TRUE;
  }

  MainFrame::MainFrame(const wxString& title,
    const wxPoint& pos, const wxSize& size)
      : wxFrame((wxFrame *) NULL, -1, title, pos, size)
  {
    CreateStatusBar(2);
    MainMenu = new wxMenuBar();
    wxMenu *FileMenu = new wxMenu();

    FileMenu->Append(MENU_New, wxT("&New"),
      wxT("Create a new file"));
    FileMenu->Append(MENU_Open, wxT("&Open"),
      wxT("Open an existing file"));
    FileMenu->Append(MENU_Close, wxT("&Close"),
      wxT("Close the current document"));
    FileMenu->Append(MENU_Save, wxT("&Save"),
      wxT("Save the current document"));
    FileMenu->Append(MENU_SaveAs, wxT("Save &As"),
      wxT("Save the current document under a new file name"));
    FileMenu->Append(MENU_Quit, wxT("&Quit"),
      wxT("Quit the editor"));

    MainMenu->Append(FileMenu, wxT("File"));
    SetMenuBar(MainMenu);

    MainEditBox = new wxTextCtrl(this, TEXT_Main,
      wxT("Hi!"), wxDefaultPosition, wxDefaultSize,
      wxTE_MULTILINE | wxTE_RICH , wxDefaultValidator, wxTextCtrlNameStr);
      Maximize();
  }

  void MainFrame::NewFile(wxCommandEvent& WXUNUSED(event))
  {
  }

  void MainFrame::OpenFile(wxCommandEvent& WXUNUSED(event))
  {
    MainEditBox->LoadFile(wxT("base.h"));
  }

  void MainFrame::CloseFile(wxCommandEvent& WXUNUSED(event))
  {
    MainEditBox->Clear();
  }

  void MainFrame::SaveFile(wxCommandEvent& WXUNUSED(event))
  {
    MainEditBox->SaveFile(wxT("base.h"));
  }

  void MainFrame::SaveFileAs(wxCommandEvent& WXUNUSED(event))
  {
  }

  void MainFrame::Quit(wxCommandEvent& WXUNUSED(event))
  {
    Close(TRUE); // Tells the OS to quit running this process
  }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值