インターネットエクスプローラのバージョンを使用するようである。 より安全な拾い読みの経験のために、考慮しなさいそして

ここに新しければいれば、私達のRSSの供給を予約購読したいと思う場合もある非常に報知的な記事週間に…およびNOを得る私達の時事通信に、また(上の右メニューで)私達は決して無差別に送信しない!

wxWidgetsは多くの特徴を含んでいるオープンソース十字プラットホームC++のプログラミングのツールキットである。 それはGUIとの成長の適用のために最も頻繁に使用されが、またネットワーキング、ファイルアクセス、そして多くのために特徴を提供し。 Windows、Linux、macおよびある埋め込まれた装置のために利用できる版によって十字のプラットホームのプログラミングは大いにより容易になる。

私は自分自身wxWidgetsに非常に新しいが、まだの下でwxWidgetsで個人指導初心者をペンで書いてしまった-

この個人指導の間に、テキスト・ファイルに荷を積み、救うことができる非常に簡単なテキスト・エディタは組み立てられる。 個人指導は適用目的、メイン・ウィンドウおよびでき事のテーブルに焦点を合わせる。

適用
すべてのwxWidgetsの塗布はwxAppから単に得られ、単一のメンバー、wxAppを打ち消す必要がある:: OnInitは、窓を作成し。 窓が開いている限り、適用はまたある。 次のコードは必要とされるコードを説明する。

  1.  
  2. #include 「wx/wx.h」のクラスMyApplication: 公共のwxApp
  3. {
  4. 公衆:
  5. bool OnInit (); </code>
  6.  
  7. }; bool MyApplication:: OnInit () {
  8.  
  9. // TODO: ショーウィンドウ
  10.  
  11. 帰りの偽;
  12.  
  13. } IMPLEMENT_APP (MyApplication);
  14.  

最初のラインはwxWidgetsの塗布のために必要とされるヘッダー・ファイルのほとんどを含んでいる。 それから新しいクラスはwxApp呼ばれるMyApplicationから、およびMyApplicationによって得られる:: OnInit ()機能は作成される。 最終的に、IMPLEMENT_APPにより(MyApplication)、必要適用は動くである。 このマクロはある特定のプラットホームのための正しいコードに実際に適用を始めるために拡大する。

wxWidgetsのフレーム
適用を作成した後、窓枠はそれで作成される制御ニーズ作成される必要があり。 フレームの目的はwxFrameから下記に示されているように、得られ、複数の機能およびテキスト制御を含んでいる。

  1.  
  2. クラスMyFrame: 公共のwxFrame
  3. {
  4. 公衆: MyFrame (); 無効のOnNew (wxCommandEvent&amp; evt);
  5.  
  6. 無効のOnOpen (wxCommandEvent&amp; evt);
  7.  
  8. 無効のOnSave (wxCommandEvent&amp; evt); 私用:
  9.  
  10. 無効のCreateWidgets ();
  11.  
  12. wxTextCtrl*のm_text;
  13. };
  14.  
  15. MyFrame:: MyFrame ():
  16.  
  17. wxFrame (0、wxID_ANY、wxT (「サンプルプログラム」))
  18. {
  19. CreateWidgets ();
  20. }
  21.  
  22. 無効のMyFrame:: OnNew (wxCommandEvent&amp; WXUNUSED (evt))
  23. {
  24. m_text-&gt; ゆとり();
  25. }
  26.  
  27. 無効のMyFrame:: OnOpen (wxCommandEvent&amp; WXUNUSED (evt))
  28. {
  29. wxStringファイル=:: wxFileSelector (wxT (「ファイルを開く」));
  30. (偽file.IsEmptyの() ==)
  31. m_text-&gt; LoadFile (ファイル);
  32. }
  33.  
  34. 無効のMyFrame:: OnSave (wxCommandEvent&amp; WXUNUSED (evt))
  35. {
  36. wxStringファイル=:: wxFileSelector (wxT (「ファイルを除けば」));
  37. (偽file.IsEmptyの() ==)
  38. m_text-&gt; SaveFile (ファイル);
  39. }
  40. 無効のMyFrame:: CreateWidgets ()
  41. {
  42. //はテキスト制御を作成する
  43. m_text =新しいwxTextCtrl (これのwxEmptyString wxID_ANY
  44. wxDefaultPositionは、wxTE_PROCESS_TAB wxDefaultSize |
  45. wxTE_PROCESS_ENTER | wxTE_MULTILINE);
  46. //はファイル・メニューを作成する
  47.  
  48. wxMenu*のfileMenu =新しいwxMenu ();
  49. fileMenu-&gt; 付けなさい(wxID_NEW、wxT (「新しいファイル」));
  50. fileMenu-&gt; 付けなさい(wxID_OPEN、wxT (「ファイルを開く」));
  51. fileMenu-&gt; 付けなさい(wxID_SAVE、wxT (「ファイルを除けば」));
  52. //はメニューバーを作成する
  53. wxMenuBar*のmenuBar =新しいwxMenuBar ();
  54. menuBar-&gt; 付けなさい(fileMenu、wxT (「ファイル」));
  55. //はメニューを置いた
  56. SetMenuBar (menuBar);
  57. }

そのコードすべては窓を作成する。 コンストラクターMyFrame:: MyFrameは親コンストラクターwxFrameを呼ぶ:: フレームを作成し、それに名前を与えるwxFrameおよびそれからそれはフレームの内容を作成するためにCreateWidgetsを、呼ぶ。 MyFrame:: CreateWidgets機能はテキスト制御およびメニューバーを作成する。 さらに、機能MyFrame:: OnNew、MyFrame:: OnOpenおよびMyFrame:: OnSaveはファイルに、またはテキストを除けば開いたゆとりに実行される。 テキストの定数がwxT (...)で囲まれていることにまた気づきなさい。 wxWidgetsがUnicodeでまた編集できるのであることをこれが保障するようにテキストの定数を包んで正しく宣言しなさい。

wxWidgetsのでき事のテーブル
でき事のテーブルができ事を扱うために制御、メニュー、および何か他のものからの正しい機能にでき事を導くのに使用されている。 それらなしで、この適用はテキストに荷を積むか、または救えなかった。 次の変更は必要でき事のテーブルを実行するためにである。

  1.  
  2. クラスMyFrame: 公共のwxFrame
  3.  
  4. {
  5.  
  6.  
  7. DECLARE_EVENT_TABLE ();
  8.  
  9. };
  10.  
  11. BEGIN_EVENT_TABLE (MyFrameのwxFrame)
  12.  
  13. EVT_MENU (wxID_NEW、MyFrame:: OnNew)
  14.  
  15. EVT_MENU (wxID_OPEN、MyFrame:: OnOpen)
  16.  
  17. EVT_MENU (wxID_SAVE、MyFrame:: OnSave)
  18.  
  19. END_EVENT_TABLE ()

最初に、フレームのためのでき事のテーブルは宣言される。 それからテーブルは作成され、でき事のIDのwxID_NEWが、wxID_OPENおよびwxID_SAVEは機能MyFrameを呼ぶのに使用されている:: OnNew、MyFrame:: OnOpenおよびMyFrame:: OnSave。 メニューによって発生したでき事は正しい機能によって処理されて得る。

フレームの提示
フレームが完全であるので、小さな変更は必要適用である。 それは窓枠を作成し、示し、適用上の窓として置く必要がある。

  1.  
  2. bool MyApplication:: OnInit ()
  3.  
  4. {
  5.  
  6. //はフレームを作成し、示す
  7.  
  8. MyFrame*フレーム=新しいMyFrame ();
  9.  
  10. フレーム&gt; ショー(); SetTopWindow (フレーム);
  11.  
  12. 帰り調整しなさい;
  13.  
  14. }

完全なコード

  1.  
  2. #include 「wx/wx.h」 //メイン・ウィンドウフレーム
  3. クラスMyFrame: 公共のwxFrame
  4. {
  5. 公衆:
  6. MyFrame ();
  7.  
  8. 無効のOnNew (wxCommandEvent&amp; evt);
  9.  
  10. 無効のOnOpen (wxCommandEvent&amp; evt);
  11.  
  12. 無効のOnSave (wxCommandEvent&amp; evt);
  13.  
  14. 私用:
  15. 無効のCreateWidgets ();
  16.  
  17. wxTextCtrl*のm_text;
  18.  
  19. DECLARE_EVENT_TABLE();
  20. };
  21.  
  22. MyFrame::MyFrame() :
  23. wxFrame(NULL, wxID_ANY, wxT("Sample Program"))
  24. {
  25. CreateWidgets();
  26. }
  27.  
  28. void MyFrame::OnNew(wxCommandEvent&amp; WXUNUSED(evt))
  29. {
  30.  
  31. m_text-&gt;Clear();
  32. }
  33.  
  34. void MyFrame::OnOpen(wxCommandEvent&amp; WXUNUSED(evt))
  35. {
  36.  
  37. wxString file = ::wxFileSelector(wxT("Open file"));
  38.  
  39. if(file.IsEmpty() == false)
  40.  
  41. m_text-&gt;LoadFile(file);
  42. }
  43.  
  44. void MyFrame::OnSave(wxCommandEvent&amp; WXUNUSED(evt))
  45. {
  46. wxString file = ::wxFileSelector(wxT("Save file"));
  47.  
  48. if(file.IsEmpty() == false)
  49.  
  50. m_text-&gt;SaveFile(file);
  51.  
  52. }
  53.  
  54. void MyFrame::CreateWidgets()
  55. {
  56. // Create a text control
  57.  
  58. m_text = new wxTextCtrl(this, ID_TEXTCTRL, wxEmptyString,
  59.  
  60. wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB |
  61.  
  62. wxTE_PROCESS_ENTER | wxTE_MULTILINE);
  63.  
  64. // Create file menu
  65.  
  66. wxMenu* fileMenu = new wxMenu();
  67.  
  68. fileMenu-&gt;Append(wxID_NEW, wxT("New File"));
  69.  
  70. fileMenu-&gt;Append(wxID_OPEN, wxT("Open File"));
  71.  
  72. fileMenu-&gt;Append(wxID_SAVE, wxT("Save File"));
  73.  
  74. // Create menu bar
  75.  
  76. wxMenuBar* menuBar = new wxMenuBar();
  77.  
  78. menuBar-&gt;Append(fileMenu, wxT("File"));
  79.  
  80. // Set menu
  81.  
  82. SetMenuBar(menuBar);
  83.  
  84. }
  85.  
  86. // Event table to route menu events to functions
  87.  
  88. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  89.  
  90. EVT_MENU(wxID_NEW, MyFrame::OnNew)
  91.  
  92. EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
  93.  
  94. EVT_MENU(wxID_SAVE, MyFrame::OnSave)
  95.  
  96. END_EVENT_TABLE()
  97.  
  98. // Application
  99.  
  100. class MyApplication : public wxApp
  101. {
  102. public:
  103.  
  104. bool OnInit();
  105. };
  106.  
  107. bool MyApplication::OnInit()
  108. {
  109.  
  110. // Create and show the frame
  111.  
  112. MyFrame* frame = new MyFrame();
  113.  
  114. frame-&gt;Show();
  115.  
  116. SetTopWindow(frame);
  117.  
  118. return true;
  119. }
  120.  
  121. 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.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Subscribe in a reader |

Links you may find interesting - Yes ! Comments are FOLLOW So feel free to link back to related and meaningful posts.

2 Responses to “Programming with wxWidgets”

  1. MyAvatars 0.2 TheCredence Says:

    I have formatted all the wxWidgets code with proper indentation to make it more readable now :)

  2. MyAvatars 0.2 Highlight and show formatted C++, PHP, Perl, Ruby, Python, Java, HTML Code in Wordpress Says:

    […] 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 […]

Post A Comment