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."
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
46 // the application icon
47 #if defined(__WXGTK__) || 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 wxWindows 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 wxWindows
103 // ----------------------------------------------------------------------------
105 // the event tables connect the wxWindows 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 wxWindows 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("OleAuto wxWindows 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
, -1, title
, pos
, size
)
155 // set the frame icon
156 SetIcon(wxICON(mondrian
));
159 wxMenu
*menuFile
= new wxMenu
;
161 menuFile
->Append(OleAuto_Test
, "&Test Excel Automation...");
162 menuFile
->Append(OleAuto_About
, "&About...");
163 menuFile
->AppendSeparator();
164 menuFile
->Append(OleAuto_Quit
, "E&xit");
166 // now append the freshly created menu to the menu bar...
167 wxMenuBar
*menuBar
= new wxMenuBar
;
168 menuBar
->Append(menuFile
, "&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("Welcome to wxWindows!");
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("This is an OLE Automation sample",
190 "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("Please ensure Excel is running, then press OK.\nThe active cell should then say 'wxWindows automation test!' in bold.");
200 wxAutomationObject excelObject
, rangeObject
;
201 if (!excelObject
.GetInstance("Excel.Application"))
203 if (!excelObject
.CreateInstance("Excel.Application"))
205 wxMessageBox("Could not create Excel object.");
209 if (!excelObject
.PutProperty("ActiveCell.Value", "wxWindows automation test!"))
211 wxMessageBox("Could not set active cell value.");
214 if (!excelObject
.PutProperty("ActiveCell.Font.Bold", (bool) TRUE
))
216 wxMessageBox("Could not put Bold property to active cell.");