당신은 Internet Explorer의 버전을 사용하기 위하여 보인다. 더 안전한 찾아본 경험을 위해, 고려하십시오그리고

당신이 여기에서 새로운 경우에 있으면, 당신은 우리의 RSS 급식을 구독하고 싶을 수도 있다 높게 유익한 기사 주간에게… 및 아니오를 얻는 우리의 회보에 뿐 아니라 (최고 오른손 메뉴에), 우리는 결코 스팸하지 않는다!

wxWidgets는 많은 특징을 포함하는 오픈 소스 십자가 플래트홈 C++ 프로그램 툴키트이다. 그것은 GUI를 가진 발전 신청을 위해 자주 사용하고, 또한 네트워킹, 파일 접근, 더 많은 것을 위한 특징을 제공하고다. 창, 리눅스, 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 (, 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. (menuBar) SetMenuBar;
  57. }

저 부호 전부는 창을 창조한다. 건설자 MyFrame:: MyFrame는 부모 건설자 wxFrame를 부른다:: 구조를 창조하고 그것에게 이름을 주는 wxFrame, 그리고 그 때 그것은 구조의 내용을 창조하기 위하여 CreateWidgets를, 부른다. MyFrame:: CreateWidgets 기능은 원본 통제 및 메뉴 막대를 창조한다. 더하여, 기능MyFrame:: OnNew, MyFrame:: OnOpenMyFrame:: 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