ここに新しければいれば、私達のRSSの供給を予約購読したいと思う場合もある非常に報知的な記事週間に…およびNOを得る私達の時事通信に、また(上の右メニューで)私達は決して無差別に送信しない!
wxWidgetsは多くの特徴を含んでいるオープンソース十字プラットホームC++のプログラミングのツールキットである。 それはGUIとの成長の適用のために最も頻繁に使用されが、またネットワーキング、ファイルアクセス、そして多くのために特徴を提供し。 Windows、Linux、macおよびある埋め込まれた装置のために利用できる版によって十字のプラットホームのプログラミングは大いにより容易になる。
私は自分自身wxWidgetsに非常に新しいが、まだの下でwxWidgetsで個人指導初心者をペンで書いてしまった-
この個人指導の間に、テキスト・ファイルに荷を積み、救うことができる非常に簡単なテキスト・エディタは組み立てられる。 個人指導は適用目的、メイン・ウィンドウおよびでき事のテーブルに焦点を合わせる。
適用
すべてのwxWidgetsの塗布はwxAppから単に得られ、単一のメンバー、wxAppを打ち消す必要がある:: OnInitは、窓を作成し。 窓が開いている限り、適用はまたある。 次のコードは必要とされるコードを説明する。
#include 「wx/wx.h」のクラスMyApplication: 公共のwxApp { 公衆: bool OnInit (); </code> }; bool MyApplication:: OnInit () { // TODO: ショーウィンドウ 帰りの偽; } IMPLEMENT_APP (MyApplication);
最初のラインはwxWidgetsの塗布のために必要とされるヘッダー・ファイルのほとんどを含んでいる。 それから新しいクラスはwxApp呼ばれるMyApplicationから、およびMyApplicationによって得られる:: OnInit ()機能は作成される。 最終的に、IMPLEMENT_APPにより(MyApplication)、必要適用は動くである。 このマクロはある特定のプラットホームのための正しいコードに実際に適用を始めるために拡大する。
wxWidgetsのフレーム
適用を作成した後、窓枠はそれで作成される制御ニーズ作成される必要があり。 フレームの目的はwxFrameから下記に示されているように、得られ、複数の機能およびテキスト制御を含んでいる。
クラスMyFrame: 公共のwxFrame { 公衆: MyFrame (); 無効のOnNew (wxCommandEvent& evt); 無効のOnOpen (wxCommandEvent& evt); 無効のOnSave (wxCommandEvent& evt); 私用: 無効のCreateWidgets (); wxTextCtrl*のm_text; }; MyFrame:: MyFrame (): wxFrame (0、wxID_ANY、wxT (「サンプルプログラム」)) { CreateWidgets (); } 無効のMyFrame:: OnNew (wxCommandEvent& WXUNUSED (evt)) { m_text-> ゆとり(); } 無効のMyFrame:: OnOpen (wxCommandEvent& WXUNUSED (evt)) { wxStringファイル=:: wxFileSelector (wxT (「ファイルを開く」)); (偽file.IsEmptyの() ==) m_text-> LoadFile (ファイル); } 無効のMyFrame:: OnSave (wxCommandEvent& WXUNUSED (evt)) { wxStringファイル=:: wxFileSelector (wxT (「ファイルを除けば」)); (偽file.IsEmptyの() ==) m_text-> SaveFile (ファイル); } 無効のMyFrame:: CreateWidgets () { //はテキスト制御を作成する m_text =新しいwxTextCtrl (これのwxEmptyString wxID_ANY wxDefaultPositionは、wxTE_PROCESS_TAB wxDefaultSize | wxTE_PROCESS_ENTER | wxTE_MULTILINE); //はファイル・メニューを作成する wxMenu*のfileMenu =新しいwxMenu (); fileMenu-> 付けなさい(wxID_NEW、wxT (「新しいファイル」)); fileMenu-> 付けなさい(wxID_OPEN、wxT (「ファイルを開く」)); fileMenu-> 付けなさい(wxID_SAVE、wxT (「ファイルを除けば」)); //はメニューバーを作成する wxMenuBar*のmenuBar =新しいwxMenuBar (); menuBar-> 付けなさい(fileMenu、wxT (「ファイル」)); //はメニューを置いた SetMenuBar (menuBar); }
そのコードすべては窓を作成する。 コンストラクターMyFrame:: MyFrameは親コンストラクターwxFrameを呼ぶ:: フレームを作成し、それに名前を与えるwxFrameおよびそれからそれはフレームの内容を作成するためにCreateWidgetsを、呼ぶ。 MyFrame:: CreateWidgets機能はテキスト制御およびメニューバーを作成する。 さらに、機能MyFrame:: OnNew、MyFrame:: OnOpenおよびMyFrame:: OnSaveはファイルに、またはテキストを除けば開いたゆとりに実行される。 テキストの定数がwxT (...)で囲まれていることにまた気づきなさい。 wxWidgetsがUnicodeでまた編集できるのであることをこれが保障するようにテキストの定数を包んで正しく宣言しなさい。
wxWidgetsのでき事のテーブル
でき事のテーブルができ事を扱うために制御、メニュー、および何か他のものからの正しい機能にでき事を導くのに使用されている。 それらなしで、この適用はテキストに荷を積むか、または救えなかった。 次の変更は必要でき事のテーブルを実行するためにである。
クラスMyFrame: 公共のwxFrame { … … DECLARE_EVENT_TABLE (); }; BEGIN_EVENT_TABLE (MyFrameのwxFrame) EVT_MENU (wxID_NEW、MyFrame:: OnNew) EVT_MENU (wxID_OPEN、MyFrame:: OnOpen) EVT_MENU (wxID_SAVE、MyFrame:: OnSave) END_EVENT_TABLE ()
最初に、フレームのためのでき事のテーブルは宣言される。 それからテーブルは作成され、でき事のIDのwxID_NEWが、wxID_OPENおよびwxID_SAVEは機能MyFrameを呼ぶのに使用されている:: OnNew、MyFrame:: OnOpenおよびMyFrame:: OnSave。 メニューによって発生したでき事は正しい機能によって処理されて得る。
フレームの提示
フレームが完全であるので、小さな変更は必要適用である。 それは窓枠を作成し、示し、適用上の窓として置く必要がある。
bool MyApplication:: OnInit () { //はフレームを作成し、示す MyFrame*フレーム=新しいMyFrame (); フレーム> ショー(); SetTopWindow (フレーム); 帰り調整しなさい; }
完全なコード
#include 「wx/wx.h」 //メイン・ウィンドウフレーム クラスMyFrame: 公共のwxFrame { 公衆: MyFrame (); 無効のOnNew (wxCommandEvent& evt); 無効のOnOpen (wxCommandEvent& evt); 無効のOnSave (wxCommandEvent& evt); 私用: 無効のCreateWidgets (); wxTextCtrl*のm_text; DECLARE_EVENT_TABLE(); }; MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, wxT("Sample Program")) { CreateWidgets(); } void MyFrame::OnNew(wxCommandEvent& WXUNUSED(evt)) { m_text->Clear(); } void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Open file")); if(file.IsEmpty() == false) m_text->LoadFile(file); } void MyFrame::OnSave(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Save file")); if(file.IsEmpty() == false) m_text->SaveFile(file); } void MyFrame::CreateWidgets() { // Create a text control m_text = new wxTextCtrl(this, ID_TEXTCTRL, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB | wxTE_PROCESS_ENTER | wxTE_MULTILINE); // Create file menu wxMenu* fileMenu = new wxMenu(); fileMenu->Append(wxID_NEW, wxT("New File")); fileMenu->Append(wxID_OPEN, wxT("Open File")); fileMenu->Append(wxID_SAVE, wxT("Save File")); // Create menu bar wxMenuBar* menuBar = new wxMenuBar(); menuBar->Append(fileMenu, wxT("File")); // Set menu SetMenuBar(menuBar); } // Event table to route menu events to functions BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_NEW, MyFrame::OnNew) EVT_MENU(wxID_OPEN, MyFrame::OnOpen) EVT_MENU(wxID_SAVE, MyFrame::OnSave) END_EVENT_TABLE() // Application class MyApplication : public wxApp { public: bool OnInit(); }; bool MyApplication::OnInit() { // Create and show the frame MyFrame* frame = new MyFrame(); frame->Show(); SetTopWindow(frame); return true; } IMPLEMENT_APP(MyApplication);
Normally to write a cross-platform GUI application, separate code would be needed for each supported platform. For Windows, the code would use the Windows API or MFC. For Linux it would probably use GTK or QT, or directly use X11. wxWidgets allows a single code base to compile and work on multiple platforms with little to no changes in the code. In addition it provides a complete set of features for networking, file and database access, and much more. For any programmer, especially a programmer developing for different platforms, wxWidgets is surely a valuable toolkit.
I hope this tutorial on wxWidgets helps you in understanding what they are and how to do programming with them. For more info on them you can always check there Official website. All Non spam comments are as usual welcomed with open hands
I would love to know How did you find the article and your inputs to it.
Links you may find interesting -
- Highlight and show formatted C++, PHP, Perl, Ruby, Python, Java, HTML Code in Wordpress
- Comparison Of Linux GUI Toolkits - Which one Do you Want ?
- Tutorial for Dummies - How to install Ruby on Windows
- Beginners Tutorial on MinGW - Configuring a Programming Environment for MinGW/Msys
- Configure Eclipse for Python















August 1st, 2007 at 4:25 pm
I have formatted all the wxWidgets code with proper indentation to make it more readable now
August 1st, 2007 at 4:37 pm
[…] this plugin, I am also able to convert my ugly wxWidget tutorial page to much more good looking Share and Enjoy: These icons link to social bookmarking sites […]