1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE Automation wxWindows 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" wxWindows headers
37 #include <wx/msw/ole/automtn.h>
40 #error "Sorry, this sample works under Windows only."
44 #error "Sorry, Watcom C++ does not support wxAutomationObject."
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
50 // the application icon
51 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
52 #include "mondrian.xpm"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
72 // Define a new frame type: this is going to be our main frame
73 class MyFrame
: public wxFrame
77 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
79 // event handlers (these functions should _not_ be virtual)
80 void OnQuit(wxCommandEvent
& event
);
81 void OnAbout(wxCommandEvent
& event
);
82 void OnTest(wxCommandEvent
& event
);
85 // any class wishing to process wxWindows events must use this macro
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // IDs for the controls and the menu commands
101 // controls start here (the numbers are, of course, arbitrary)
105 // ----------------------------------------------------------------------------
106 // event tables and other macros for wxWindows
107 // ----------------------------------------------------------------------------
109 // the event tables connect the wxWindows events with the functions (event
110 // handlers) which process them. It can be also done at run-time, but for the
111 // simple menu events like this the static method is much simpler.
112 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
113 EVT_MENU(OleAuto_Quit
, MyFrame::OnQuit
)
114 EVT_MENU(OleAuto_About
, MyFrame::OnAbout
)
115 EVT_MENU(OleAuto_Test
, MyFrame::OnTest
)
118 // Create a new application object: this macro will allow wxWindows to create
119 // the application object during program execution (it's better than using a
120 // static object for many reasons) and also declares the accessor function
121 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
130 // the application class
131 // ----------------------------------------------------------------------------
133 // `Main program' equivalent: the program execution "starts" here
136 // Create the main application window
137 MyFrame
*frame
= new MyFrame(_T("OleAuto wxWindows App"),
138 wxPoint(50, 50), wxSize(450, 340));
140 // Show it and tell the application that it's our main window
141 // @@@ what does it do exactly, in fact? is it necessary here?
145 // success: wxApp::OnRun() will be called which will enter the main message
146 // loop and the application will run. If we returned FALSE here, the
147 // application would exit immediately.
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
156 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
157 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
159 // set the frame icon
160 SetIcon(wxICON(mondrian
));
163 wxMenu
*menuFile
= new wxMenu
;
165 menuFile
->Append(OleAuto_Test
, _T("&Test Excel Automation..."));
166 menuFile
->Append(OleAuto_About
, _T("&About..."));
167 menuFile
->AppendSeparator();
168 menuFile
->Append(OleAuto_Quit
, _T("E&xit"));
170 // now append the freshly created menu to the menu bar...
171 wxMenuBar
*menuBar
= new wxMenuBar
;
172 menuBar
->Append(menuFile
, _T("&File"));
174 // ... and attach this menu bar to the frame
177 // create a status bar just for fun (by default with 1 pane only)
179 SetStatusText(_T("Welcome to wxWindows!"));
185 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
187 // TRUE is to force the frame to close
191 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
193 wxMessageBox(_T("This is an OLE Automation sample"),
194 _T("About OleAuto"), wxOK
| wxICON_INFORMATION
, this);
197 /* Tests OLE automation by making the active Excel cell bold,
198 * and changing the text.
200 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
202 wxMessageBox(_T("Please ensure Excel is running, then press OK.\nThe active cell should then say 'wxWindows automation test!' in bold."));
204 wxAutomationObject excelObject
, rangeObject
;
205 if (!excelObject
.GetInstance(_T("Excel.Application")))
207 if (!excelObject
.CreateInstance(_T("Excel.Application")))
209 wxMessageBox(_T("Could not create Excel object."));
213 if (!excelObject
.PutProperty(_T("ActiveCell.Value"), _T("wxWindows automation test!")))
215 wxMessageBox(_T("Could not set active cell value."));
219 if (!excelObject
.PutProperty(_T("ActiveCell.Font.Bold"), wxVariant((bool) TRUE
)) )
221 wxMessageBox(_T("Could not put Bold property to active cell."));