wx_exe.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx_exe.cpp
  3. // Purpose: Sample showing how to use wx from a DLL
  4. // Author: Vaclav Slavik
  5. // Created: 2009-12-03
  6. // Copyright: (c) 2009 Vaclav Slavik
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. // ============================================================================
  10. // declarations
  11. // ============================================================================
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #include "my_dll.h"
  20. #include "wx/app.h"
  21. #include "wx/frame.h"
  22. #include "wx/panel.h"
  23. #include "wx/sizer.h"
  24. #include "wx/stattext.h"
  25. #include "wx/button.h"
  26. #ifndef __WINDOWS__
  27. #error "This sample is Windows-only"
  28. #endif
  29. #ifdef WXUSINGDLL
  30. #error "This sample doesn't work with DLL build of wxWidgets"
  31. #endif
  32. // ----------------------------------------------------------------------------
  33. // GUI classes
  34. // ----------------------------------------------------------------------------
  35. static const int ID_RUN_DLL = wxNewId();
  36. class MainFrame : public wxFrame
  37. {
  38. public:
  39. MainFrame();
  40. void OnRunDLL(wxCommandEvent& event);
  41. wxDECLARE_EVENT_TABLE();
  42. };
  43. class MainApp : public wxApp
  44. {
  45. public:
  46. virtual bool OnInit();
  47. virtual int OnExit();
  48. };
  49. // ============================================================================
  50. // implementation
  51. // ============================================================================
  52. // ----------------------------------------------------------------------------
  53. // MainFrame
  54. // ----------------------------------------------------------------------------
  55. wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
  56. EVT_BUTTON(ID_RUN_DLL, MainFrame::OnRunDLL)
  57. wxEND_EVENT_TABLE()
  58. MainFrame::MainFrame()
  59. : wxFrame(NULL, wxID_ANY, "Main wx app",
  60. wxDefaultPosition, wxSize(400, 300))
  61. {
  62. wxPanel *p = new wxPanel(this, wxID_ANY);
  63. wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
  64. sizer->Add
  65. (
  66. new wxStaticText
  67. (
  68. p, wxID_ANY,
  69. wxString::Format
  70. (
  71. "Main wxApp instance is %p (%s),\n"
  72. "thread ID %ld.\n",
  73. wxApp::GetInstance(),
  74. wxVERSION_STRING,
  75. wxThread::GetCurrentId()
  76. )
  77. ),
  78. wxSizerFlags(1).Expand().Border(wxALL, 10)
  79. );
  80. sizer->Add
  81. (
  82. new wxButton(p, ID_RUN_DLL, "Run GUI from DLL"),
  83. wxSizerFlags(0).Right().Border(wxALL, 10)
  84. );
  85. p->SetSizerAndFit(sizer);
  86. wxSizer *fsizer = new wxBoxSizer(wxVERTICAL);
  87. fsizer->Add(p, wxSizerFlags(1).Expand());
  88. SetSizerAndFit(fsizer);
  89. }
  90. void MainFrame::OnRunDLL(wxCommandEvent& WXUNUSED(event))
  91. {
  92. run_wx_gui_from_dll("child instance");
  93. }
  94. // ----------------------------------------------------------------------------
  95. // MainApp
  96. // ----------------------------------------------------------------------------
  97. bool MainApp::OnInit()
  98. {
  99. if ( !wxApp::OnInit() )
  100. return false;
  101. wxFrame *f = new MainFrame();
  102. f->Show(true);
  103. return true;
  104. }
  105. int MainApp::OnExit()
  106. {
  107. wx_dll_cleanup();
  108. return wxApp::OnExit();
  109. }
  110. IMPLEMENT_APP(MainApp)