oleauto.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: oleauto.cpp
  3. // Purpose: OLE Automation wxWidgets sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 08/12/98
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include "wx/msw/ole/automtn.h"
  27. #ifndef __WINDOWS__
  28. #error "Sorry, this sample works under Windows only."
  29. #endif
  30. // ----------------------------------------------------------------------------
  31. // ressources
  32. // ----------------------------------------------------------------------------
  33. // the application icon
  34. #ifndef wxHAS_IMAGES_IN_RESOURCES
  35. #include "../sample.xpm"
  36. #endif
  37. // ----------------------------------------------------------------------------
  38. // private classes
  39. // ----------------------------------------------------------------------------
  40. // Define a new application type, each program should derive a class from wxApp
  41. class MyApp : public wxApp
  42. {
  43. public:
  44. // override base class virtuals
  45. // ----------------------------
  46. // this one is called on application startup and is a good place for the app
  47. // initialization (doing it here and not in the ctor allows to have an error
  48. // return: if OnInit() returns false, the application terminates)
  49. virtual bool OnInit();
  50. };
  51. // Define a new frame type: this is going to be our main frame
  52. class MyFrame : public wxFrame
  53. {
  54. public:
  55. // ctor(s)
  56. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  57. // event handlers (these functions should _not_ be virtual)
  58. void OnQuit(wxCommandEvent& event);
  59. void OnAbout(wxCommandEvent& event);
  60. void OnTest(wxCommandEvent& event);
  61. private:
  62. // any class wishing to process wxWidgets events must use this macro
  63. wxDECLARE_EVENT_TABLE();
  64. };
  65. // ----------------------------------------------------------------------------
  66. // constants
  67. // ----------------------------------------------------------------------------
  68. // IDs for the controls and the menu commands
  69. enum
  70. {
  71. // menu items
  72. OleAuto_Quit = 1,
  73. OleAuto_About,
  74. OleAuto_Test,
  75. // controls start here (the numbers are, of course, arbitrary)
  76. OleAuto_Text = 1000
  77. };
  78. // ----------------------------------------------------------------------------
  79. // event tables and other macros for wxWidgets
  80. // ----------------------------------------------------------------------------
  81. // the event tables connect the wxWidgets events with the functions (event
  82. // handlers) which process them. It can be also done at run-time, but for the
  83. // simple menu events like this the static method is much simpler.
  84. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  85. EVT_MENU(OleAuto_Quit, MyFrame::OnQuit)
  86. EVT_MENU(OleAuto_About, MyFrame::OnAbout)
  87. EVT_MENU(OleAuto_Test, MyFrame::OnTest)
  88. wxEND_EVENT_TABLE()
  89. // Create a new application object: this macro will allow wxWidgets to create
  90. // the application object during program execution (it's better than using a
  91. // static object for many reasons) and also declares the accessor function
  92. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  93. // not wxApp)
  94. IMPLEMENT_APP(MyApp)
  95. // ============================================================================
  96. // implementation
  97. // ============================================================================
  98. // ----------------------------------------------------------------------------
  99. // the application class
  100. // ----------------------------------------------------------------------------
  101. // `Main program' equivalent: the program execution "starts" here
  102. bool MyApp::OnInit()
  103. {
  104. if ( !wxApp::OnInit() )
  105. return false;
  106. // Create the main application window
  107. MyFrame *frame = new MyFrame(wxT("OleAuto wxWidgets App"),
  108. wxPoint(50, 50), wxSize(450, 340));
  109. // Show it
  110. frame->Show(true);
  111. // success: wxApp::OnRun() will be called which will enter the main message
  112. // loop and the application will run. If we returned false here, the
  113. // application would exit immediately.
  114. return true;
  115. }
  116. // ----------------------------------------------------------------------------
  117. // main frame
  118. // ----------------------------------------------------------------------------
  119. // frame constructor
  120. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  121. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
  122. {
  123. // set the frame icon
  124. SetIcon(wxICON(sample));
  125. // create a menu bar
  126. wxMenu *menuFile = new wxMenu;
  127. menuFile->Append(OleAuto_Test, wxT("&Test Excel Automation..."));
  128. menuFile->Append(OleAuto_About, wxT("&About"));
  129. menuFile->AppendSeparator();
  130. menuFile->Append(OleAuto_Quit, wxT("E&xit"));
  131. // now append the freshly created menu to the menu bar...
  132. wxMenuBar *menuBar = new wxMenuBar;
  133. menuBar->Append(menuFile, wxT("&File"));
  134. // ... and attach this menu bar to the frame
  135. SetMenuBar(menuBar);
  136. #if wxUSE_STATUSBAR
  137. // create a status bar just for fun (by default with 1 pane only)
  138. CreateStatusBar(2);
  139. SetStatusText(wxT("Welcome to wxWidgets!"));
  140. #endif // wxUSE_STATUSBAR
  141. }
  142. // event handlers
  143. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  144. {
  145. // true is to force the frame to close
  146. Close(true);
  147. }
  148. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  149. {
  150. wxMessageBox(wxT("This is an OLE Automation sample"),
  151. wxT("About OleAuto"), wxOK | wxICON_INFORMATION, this);
  152. }
  153. /* Tests OLE automation by making the active Excel cell bold,
  154. * and changing the text.
  155. */
  156. void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
  157. {
  158. wxMessageBox(wxT("Excel will be started if it is not running after you have pressed OK button.")
  159. wxT("\nThe active cell should then say 'wxWidgets automation test!' in bold."),
  160. wxT("Excel start"));
  161. wxAutomationObject excelObject;
  162. if ( !excelObject.GetInstance(wxT("Excel.Application")) )
  163. {
  164. wxLogError(wxT("Could not create Excel object."));
  165. return;
  166. }
  167. // Ensure that Excel is visible
  168. if (!excelObject.PutProperty(wxT("Visible"), true))
  169. {
  170. wxLogError(wxT("Could not make Excel object visible"));
  171. }
  172. const wxVariant workbooksCountVariant = excelObject.GetProperty(wxT("Workbooks.Count"));
  173. if (workbooksCountVariant.IsNull())
  174. {
  175. wxLogError(wxT("Could not get workbooks count"));
  176. return;
  177. }
  178. const long workbooksCount = workbooksCountVariant;
  179. if (workbooksCount == 0)
  180. {
  181. const wxVariant workbook = excelObject.CallMethod(wxT("Workbooks.Add"));
  182. if (workbook.IsNull())
  183. {
  184. wxLogError(wxT("Could not create new Workbook"));
  185. return;
  186. }
  187. }
  188. if (!excelObject.PutProperty(wxT("ActiveCell.Value"), wxT("wxWidgets automation test!")))
  189. {
  190. wxLogError(wxT("Could not set active cell value."));
  191. return;
  192. }
  193. if (!excelObject.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true)) )
  194. {
  195. wxLogError(wxT("Could not put Bold property to active cell."));
  196. return;
  197. }
  198. }