1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE Automation wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "oleauto.cpp"
21 #pragma interface "oleauto.cpp"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWidgets headers
37 #include <wx/msw/ole/automtn.h>
40 #error "Sorry, this sample works under Windows only."
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
46 // the application icon
47 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
48 #include "mondrian.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // Define a new application type, each program should derive a class from wxApp
56 class MyApp
: public wxApp
59 // override base class virtuals
60 // ----------------------------
62 // this one is called on application startup and is a good place for the app
63 // initialization (doing it here and not in the ctor allows to have an error
64 // return: if OnInit() returns false, the application terminates)
65 virtual bool OnInit();
68 // Define a new frame type: this is going to be our main frame
69 class MyFrame
: public wxFrame
73 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
75 // event handlers (these functions should _not_ be virtual)
76 void OnQuit(wxCommandEvent
& event
);
77 void OnAbout(wxCommandEvent
& event
);
78 void OnTest(wxCommandEvent
& event
);
81 // any class wishing to process wxWidgets events must use this macro
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // IDs for the controls and the menu commands
97 // controls start here (the numbers are, of course, arbitrary)
101 // ----------------------------------------------------------------------------
102 // event tables and other macros for wxWidgets
103 // ----------------------------------------------------------------------------
105 // the event tables connect the wxWidgets events with the functions (event
106 // handlers) which process them. It can be also done at run-time, but for the
107 // simple menu events like this the static method is much simpler.
108 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
109 EVT_MENU(OleAuto_Quit
, MyFrame::OnQuit
)
110 EVT_MENU(OleAuto_About
, MyFrame::OnAbout
)
111 EVT_MENU(OleAuto_Test
, MyFrame::OnTest
)
114 // Create a new application object: this macro will allow wxWidgets to create
115 // the application object during program execution (it's better than using a
116 // static object for many reasons) and also declares the accessor function
117 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // the application class
127 // ----------------------------------------------------------------------------
129 // `Main program' equivalent: the program execution "starts" here
132 // Create the main application window
133 MyFrame
*frame
= new MyFrame(_T("OleAuto wxWidgets App"),
134 wxPoint(50, 50), wxSize(450, 340));
136 // Show it and tell the application that it's our main window
137 // @@@ what does it do exactly, in fact? is it necessary here?
141 // success: wxApp::OnRun() will be called which will enter the main message
142 // loop and the application will run. If we returned false here, the
143 // application would exit immediately.
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
153 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
155 // set the frame icon
156 SetIcon(wxICON(mondrian
));
159 wxMenu
*menuFile
= new wxMenu
;
161 menuFile
->Append(OleAuto_Test
, _T("&Test Excel Automation..."));
162 menuFile
->Append(OleAuto_About
, _T("&About..."));
163 menuFile
->AppendSeparator();
164 menuFile
->Append(OleAuto_Quit
, _T("E&xit"));
166 // now append the freshly created menu to the menu bar...
167 wxMenuBar
*menuBar
= new wxMenuBar
;
168 menuBar
->Append(menuFile
, _T("&File"));
170 // ... and attach this menu bar to the frame
173 // create a status bar just for fun (by default with 1 pane only)
175 SetStatusText(_T("Welcome to wxWidgets!"));
181 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
183 // true is to force the frame to close
187 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
189 wxMessageBox(_T("This is an OLE Automation sample"),
190 _T("About OleAuto"), wxOK
| wxICON_INFORMATION
, this);
193 /* Tests OLE automation by making the active Excel cell bold,
194 * and changing the text.
196 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
198 wxMessageBox(_T("Please ensure Excel is running, then press OK.\nThe active cell should then say 'wxWidgets automation test!' in bold."));
200 wxAutomationObject excelObject
, rangeObject
;
201 if (!excelObject
.GetInstance(_T("Excel.Application")))
203 if (!excelObject
.CreateInstance(_T("Excel.Application")))
205 wxMessageBox(_T("Could not create Excel object."));
209 if (!excelObject
.PutProperty(_T("ActiveCell.Value"), _T("wxWidgets automation test!")))
211 wxMessageBox(_T("Could not set active cell value."));
214 if (!excelObject
.PutProperty(_T("ActiveCell.Font.Bold"), wxVariant(true)) )
216 wxMessageBox(_T("Could not put Bold property to active cell."));