gtkmm的GUI開發上最大的困難點在於文檔與範例稀少,學習的來源有限,故在自己學習的路上希望能做些筆記,
這一系列筆記一方面為了強化自己的記憶,另一方面給有興趣一同研究這套跨平台GUI庫的同好一點參考的來源。
如果尚不知道如何架設環境的朋友可以看我的另一篇文章,"gtkmm環境架設筆記"。
這次主要紀錄練習關於BOX容器的應用,範例檔來自於gtkmm開發團隊在github上的box容器範例,我會在源碼中在難以理解的地方加上註釋,並對原有官方
註釋進行翻譯。
執行範例時要傳入參數,共有1,2,3種選項。
擷取畫面一
擷取畫面二
擷取畫面三
開始進入正題
首先是頭文件 "examplewindow.h",用於定義主要窗口的構成
//$Id: examplewindow.h 705 2006-07-19 02:55:32Z jjongsma $ -*- c++ -*- /* gtkmm example Copyright (C) 2002 gtkmm development team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window //主要視窗,必須繼承自Gtk::Window { public: ExampleWindow(int which); virtual ~ExampleWindow(); protected: //Signal handlers: void on_button_quit_clicked(); //自訂義Button點擊事件 //Child widgets: Gtk::Button m_button; //button 容器宣告 Gtk::Box m_box1; //box 容器宣告 Gtk::Box m_boxQuit; //box 容器宣告 Gtk::Button m_buttonQuit; //Button 容器宣告 Gtk::Label m_Label1, m_Label2;//Label 容器宣告 Gtk::Separator m_separator1, m_separator2;//用於控件分離的控件 官方字典連結 }; #endif //GTKMM_EXAMPLEWINDOW_H
接著是 "examplewindow.cpp",實現剛剛各個容器的定義。
#include <iostream> #include "examplewindow.h" #include "packbox.h" ExampleWindow::ExampleWindow(int which) : m_box1(Gtk::ORIENTATION_VERTICAL), m_buttonQuit("Quit") { set_title("Gtk::Box example"); PackBox *pPackBox1, *pPackBox2, *pPackBox3, *pPackBox4, *pPackBox5;//這裡這五個控件是自訂義控件,定義內容寫於"packbox.h" switch(which)//這是一開始傳進來的參數,用於決定顯示哪個demo { case 1://第一個範例 { m_Label1.set_text("Gtk::Box(Gtk::ORIENTATION_HORIZONTAL); set_homogeneous(false);");//這裡一整句是在設定靜態文本控件的字串,內容為 設定box為橫向,且內部子控件可以大小不一
// 將控件對齊線設定為左側m_Label1.set_halign(Gtk::ALIGN_START); m_Label1.set_valign(Gtk::ALIGN_START); // Pack the label into the vertical box (vbox box1). Remember that // widgets added to a vbox will be packed one on top of the other in // order.//將靜態文本控件打包到直向box(vbox)中,所有被打包進vbox的控件將會依次由上自下排列
m_box1.pack_start(m_Label1, Gtk::PACK_SHRINK);//Gtk::PACK_SHERINK :將m_Label1打包進m_box1中,大小與子控件關聯 官網字典連結 // Create a PackBox - homogeneous = false, spacing = 0,//創建自訂義PackBox,內部子控件可以大小不一致,間距為零。// options = Gtk::PACK_SHRINK, padding = 0 pPackBox1 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK));//gtkmm可以動態載入控件,使用的方法為gtk::manage