スポンサーリンク
// https://docs.wxwidgets.org/3.0/overview_helloworld.html // プリプロセッサに以下二つを追加 // __WXMSW__ // WXUSINGDLL // サブシステムをWindowsに設定(WinMainで呼び出すので) // Windows (/SUBSYSTEM:WINDOWS) #ifndef WX_PRECOMP #include <wx/wx.h> #endif #include <wx/gdicmn.h> // wxPointに必要 #include <wx/frame.h> // wxFrameに必要 ///////////////////////////////////// ///////////////////////////////////// ///////////////////////////////////// #include <string> // ウィンドウ作成 class MyFrame : public wxFrame { public: void PostCreate() { // ウィンドウサイズ this->SetSize(wxSize(200, 200)); // ウィンドウのサイズを設定 this->Layout(); // レイアウトの更新 } void func1(); void func2(); void func3(); void func4(); void func5(); void func6(); void func7(); void func8(); void func9(); void func10(); MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size) { // CallAfter : 現在処理中のイベントが終わったらPostCreateを実行 // コンストラクタはウィンドウ生成イベント扱い CallAfter(&MyFrame::PostCreate); //func1(); //func2(); //func3(); func4(); //func5(); //func6(); //func7(); //func8(); //func9(); //func10(); } private: }; ///////////////////////////////////// ///////////////////////////////////// ///////////////////////////////////// // wxWidgetsのアプリケーション作成 class MyApp : public wxApp { public: virtual bool OnInit() { MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340)); frame->Show(true); return true; } }; ///////////////////////////////////// ///////////////////////////////////// ///////////////////////////////////// // WinMainをマクロで定義 wxIMPLEMENT_APP(MyApp);
// 全体に広げる void MyFrame::func1() { wxButton* mybutton = new wxButton( this, wxID_ANY, "button", wxDefaultPosition, wxSize(100, 100), wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); // 縦方向のBoxSizer wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); // ウィジェットをSizerに追加 // wxEXPANDでフルサイズに広げる(1=比率) sizer->Add(mybutton, 1, wxEXPAND | wxALL, 0); // パネルにSizerを適用 this->SetSizer(sizer); }
// 上下に平等 void MyFrame::func2() { wxButton* mybutton1 = new wxButton( this, wxID_ANY, "1", wxDefaultPosition, wxSize(100, 100), wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); mybutton1->SetMinSize(wxSize(20, 20)); wxButton* mybutton2 = new wxButton( this, wxID_ANY, "2", wxDefaultPosition, wxSize(100, 100), wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); mybutton2->SetMinSize(wxSize(20, 20)); // 上下に並べて表示 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); // ウィジェットをSizerに追加 // wxEXPANDでフルサイズに広げる(1=比率) sizer->Add(mybutton1, 1, wxEXPAND | wxALL, 0); sizer->Add(mybutton2, 1, wxEXPAND | wxALL, 0); // パネルにSizerを適用 this->SetSizer(sizer); // レイアウトの更新 this->Layout(); }
// 左右に平等 void MyFrame::func3() { wxButton* mybutton1 = new wxButton( this, wxID_ANY, "1", wxDefaultPosition, wxSize(100, 100), wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); mybutton1->SetMinSize(wxSize(20, 20)); wxButton* mybutton2 = new wxButton( this, wxID_ANY, "2", wxDefaultPosition, wxSize(100, 100), wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); mybutton2->SetMinSize(wxSize(20, 20)); // 左右に並べて表示 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); // ウィジェットをSizerに追加 // wxEXPANDでフルサイズに広げる(1=比率) sizer->Add(mybutton1, 1, wxEXPAND | wxALL, 0); sizer->Add(mybutton2, 1, wxEXPAND | wxALL, 0); // パネルにSizerを適用 this->SetSizer(sizer); // レイアウトの更新 this->Layout(); }
// 画面中央 固定サイズ void MyFrame::func4() { wxButton* mybutton1 = new wxButton( this, wxID_ANY, "1", wxDefaultPosition, wxSize(100, 100), // ボタンのサイズを指定 wxTAB_TRAVERSAL | wxBORDER_SIMPLE ); wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); sizer->Add(0, 0, 1, wxEXPAND, 0); // ダミー。proportion=1で「残りスペースを埋める」設定 sizer->Add(mybutton1, 0, wxALIGN_CENTER | wxALL, 0); // proportion=0 で固定サイズ sizer->Add(0, 0, 1, wxEXPAND, 0); // ダミー。proportion=1で「残りスペースを埋める」設定 this->SetSizer(sizer); }
// 画面四分割 wxGridSizer void MyFrame::func5() { // 2x2のGridSizerを作成(行数:2, 列数:2, 両マージン:0) wxGridSizer* sizer = new wxGridSizer(2, 2, 0, 0); // 4つのボタンを作成して追加 wxButton* button1 = new wxButton(this, wxID_ANY, "1"); wxButton* button2 = new wxButton(this, wxID_ANY, "2"); wxButton* button3 = new wxButton(this, wxID_ANY, "3"); wxButton* button4 = new wxButton(this, wxID_ANY, "4"); // 各ボタンをSizerに追加(比率1、フル展開) sizer->Add(button1, 1, wxEXPAND | wxALL, 0); sizer->Add(button2, 1, wxEXPAND | wxALL, 0); sizer->Add(button3, 1, wxEXPAND | wxALL, 0); sizer->Add(button4, 1, wxEXPAND | wxALL, 0); this->SetSizer(sizer); this->Layout(); }
// 画面四分割 wxSizer void MyFrame::func6() { wxButton* button1 = new wxButton(this, wxID_ANY, "1"); wxButton* button2 = new wxButton(this, wxID_ANY, "2"); wxButton* button3 = new wxButton(this, wxID_ANY, "3"); wxButton* button4 = new wxButton(this, wxID_ANY, "4"); // 大きな縦方向Sizer(2行) wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); // 1行目 wxBoxSizer* row1 = new wxBoxSizer(wxHORIZONTAL); row1->Add(button1, 1, wxEXPAND | wxALL, 0); row1->Add(button2, 1, wxEXPAND | wxALL, 0); mainSizer->Add(row1, 1, wxEXPAND | wxALL, 0); // 2行目 wxBoxSizer* row2 = new wxBoxSizer(wxHORIZONTAL); row2->Add(button3, 1, wxEXPAND | wxALL, 0); row2->Add(button4, 1, wxEXPAND | wxALL, 0); mainSizer->Add(row2, 1, wxEXPAND | wxALL, 0); // フレームにSizerを適用 this->SetSizer(mainSizer); this->Layout(); }
// 左側幅固定 void MyFrame::func7() { wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); // 左のボタン(幅50px固定) wxButton* leftButton = new wxButton(this, wxID_ANY, "Left"); leftButton->SetMinSize(wxSize(50, -1));// 幅50px, 高さは自動調整 leftButton->SetMaxSize(wxSize(50, -1)); // 右のボタン(残りスペースを埋める) wxButton* rightButton = new wxButton(this, wxID_ANY, "Right"); // Sizerに追加 sizer->Add(leftButton, 0, wxEXPAND | wxALL, 0); // 右は proportion=0 で固定サイズ sizer->Add(rightButton, 1, wxEXPAND | wxALL, 0);// 左は proportion=1 で残りを埋める // フレームに適用 this->SetSizer(sizer); this->Layout(); }
// 右側幅固定 void MyFrame::func8() { wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); // 左のボタン(残りスペースを埋める) wxButton* leftButton = new wxButton(this, wxID_ANY, "Left"); // 右のボタン(幅500px固定) wxButton* rightButton = new wxButton(this, wxID_ANY, "Right"); // 幅を固定するための設定 rightButton->SetMinSize(wxSize(50, -1)); rightButton->SetMaxSize(wxSize(50, -1)); // Sizerに追加 sizer->Add(leftButton, 1, wxEXPAND | wxALL, 0); // 左は proportion=1 で残りを埋める sizer->Add(rightButton, 0, wxEXPAND | wxALL, 0); // 右は proportion=0 で固定サイズ // フレームに適用 this->SetSizer(sizer); this->Layout(); }
// 上側高さ固定 void MyFrame::func9() { wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); // 上のボタン(高さ固定) wxButton* topButton = new wxButton(this, wxID_ANY, "Top"); topButton->SetMinSize(wxSize(-1, 50)); // 高さ100pxを保証 topButton->SetMaxSize(wxSize(-1, 50)); // 高さ100pxに固定 // 下のボタン(残りスペースを埋める) wxButton* bottomButton = new wxButton(this, wxID_ANY, "Bottom"); // Sizerに追加 sizer->Add(topButton, 0, wxEXPAND | wxALL, 0); // proportion=0 で固定サイズ sizer->Add(bottomButton, 1, wxEXPAND | wxALL, 0); // proportion=1 で残りスペース // フレームに適用 this->SetSizer(sizer); this->Layout(); }
// 下側高さ固定 void MyFrame::func10() { wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); // 上のボタン wxButton* topButton = new wxButton(this, wxID_ANY, "Top"); // 下のボタン wxButton* bottomButton = new wxButton(this, wxID_ANY, "Bottom"); // 高さを固定するための設定 bottomButton->SetMinSize(wxSize(-1, 50)); bottomButton->SetMaxSize(wxSize(-1, 50)); // Sizerに追加 sizer->Add(topButton, 1, wxEXPAND | wxALL, 0); // proportion=1 で残りスペース sizer->Add(bottomButton, 0, wxEXPAND | wxALL, 0); // proportion=0 で固定サイズ // フレームに適用 this->SetSizer(sizer); this->Layout(); }