引き続き、wxWidgetsについて色々試してみる。今回はWindowsを2つ用意し、サブウインドウからメインウインドウを操作してみることにする。
前回、前々回のコードに加えて、サブウインドウのクラスを用意する。
- controlwindow.hpp
#ifndef __CONTROLWINDOW_HPP__ #define __CONTROLWINDOW_HPP__ #include <wx/wx.h> #include "mainwindow.hpp" class ControlWindow : public wxFrame { private: MainWindow* main_; public: ControlWindow(const wxString& title, MainWindow* main); void OnQuit(wxCommandEvent & event); }; #endif //__CONTROLWINDOW_HPP__
- controlwindow.cpp
#include "controlwindow.hpp" #include "mainwindow.hpp" ControlWindow::ControlWindow(const wxString& title, MainWindow* main) : wxFrame(NULL, wxID_ANY, title, wxPoint(500, 100), wxSize(350, 250)), main_(main)
{ wxPanel* panel = new wxPanel(this, wxID_ANY); wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20)); Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ControlWindow::OnQuit)); button->SetFocus(); } void ControlWindow::OnQuit(wxCommandEvent & WXUNUSED(event)) { this->main_->Close(true); Close(true); }
ポイントはサブウインドウ側にメインウインドウクラスへのポインタを持つところ。
- main.cpp
#include "main.hpp" #include "mainwindow.hpp" #include "controlwindow.hpp" IMPLEMENT_APP(DECSApp) bool DECSApp::OnInit() { MainWindow *mainwindow = new MainWindow(wxT("OpenGL Integration")); ControlWindow *controlwindow = new ControlWindow(wxT("Control Window"), mainwindow); mainwindow->Show(true); controlwindow->Show(true); return true; }
このようにメインウインドウクラスのインスタンスを生成後にそのポインタを渡して、サブウインドウのイオンスタンスを生成した。また、サブウインドウのOnQuitメンバ関数内でメインウインドウのクローズも行っている。
サブウインドウにはボタンコントロールを配置し、ボタンが押されるとサブウインドウのOnQuitメンバ関数が呼ばれるようになっている。
これをビルドして実行すると、
このように2つのウインドウが表示され、ボタンを押すと両方のウインドウが終了する。
本当にこの方法がwxWidgetsの作法として正しいのかどうかは調べてみる必要があるが、ひとまずこれで意図した動作はするようだ。
コメント