]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: mfctest.cpp | |
3 | // Purpose: Sample to demonstrate mixing MFC and wxWidgets code | |
4 | // Author: Julian Smart | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) Julian Smart | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // This sample pops up an initial wxWidgets frame, with a menu item | |
11 | // that allows a new MFC window to be created. Note that CDummyWindow | |
12 | // is a class that allows a wxWidgets window to be seen as a CWnd | |
13 | // for the purposes of specifying a valid main window to the | |
14 | // MFC initialisation. | |
15 | // | |
16 | // You can easily modify this code so that an MFC window pops up | |
17 | // initially as the main frame, and allows wxWidgets frames to be | |
18 | // created subsequently. | |
19 | // | |
20 | // (1) Make MyApp::OnInit not create a main window. | |
21 | // (2) Make MFC's InitInstance create a main window, and remove | |
22 | // creation of CDummyWindow. | |
23 | // | |
24 | // This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below. | |
25 | ||
26 | #define START_WITH_MFC_WINDOW 0 | |
27 | ||
28 | // | |
29 | // IMPORTANT NOTES: | |
30 | // | |
31 | // (1) You may need to set wxUSE_MFC to 1 in include/wx/msw/setup.h but | |
32 | // normally this shouldn't be needed any longer, i.e. it works without | |
33 | // it for me (VZ) | |
34 | // | |
35 | // (2) You should link with MFC DLL, not static libraries: or, to use static | |
36 | // run-time libraries, use this command for both building wxWidgets and | |
37 | // the sample: | |
38 | // | |
39 | // nmake -f makefile.vc BUILD=debug SHARED=0 DEBUG_RUNTIME_LIBS=0 RUNTIME_LIBS=static all | |
40 | // | |
41 | // Unless the run-time library settings match for wxWidgets and MFC, you | |
42 | // will get link errors for symbols such as __mbctype, __argc, and __argv | |
43 | // | |
44 | // (3) If you see bogus memory leaks within the MSVC IDE on exit, in this | |
45 | // sample or in your own project, you must be using __WXDEBUG__ + | |
46 | // WXUSINGDLL + _AFXDLL | |
47 | // Unfortunately this confuses the MSVC/MFC leak detector. To do away with | |
48 | // these bogus memory leaks, add this to the list of link objects, make it | |
49 | // first: mfc[version][u]d.lib | |
50 | // - [version] -> 42 or 70 or 80 etc | |
51 | // - u if using Unicode | |
52 | ||
53 | #include "stdafx.h" | |
54 | ||
55 | // For compilers that support precompilation, includes "wx/wx.h". | |
56 | #include "wx/wxprec.h" | |
57 | ||
58 | #ifdef __BORLANDC__ | |
59 | #pragma hdrstop | |
60 | #endif | |
61 | ||
62 | #ifndef WX_PRECOMP | |
63 | #include "wx/wx.h" | |
64 | #endif | |
65 | ||
66 | #include "wx/evtloop.h" | |
67 | ||
68 | #include "resource.h" | |
69 | ||
70 | #include "mfctest.h" | |
71 | ||
72 | ///////////////////////////////////////////////////////////////////////////// | |
73 | ||
74 | // theApp: | |
75 | // Just creating this application object runs the whole application. | |
76 | // | |
77 | CTheApp theApp; | |
78 | ||
79 | // wxWidgets elements | |
80 | ||
81 | // Define a new application type | |
82 | class MyApp: public wxApp | |
83 | { | |
84 | public: | |
85 | virtual bool OnInit(); | |
86 | ||
87 | // we need to override this as the default behaviour only works when we're | |
88 | // running wxWidgets main loop, not MFC one | |
89 | virtual void ExitMainLoop(); | |
90 | ||
91 | wxFrame *CreateFrame(); | |
92 | }; | |
93 | ||
94 | class MyCanvas: public wxScrolledWindow | |
95 | { | |
96 | public: | |
97 | MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size); | |
98 | void OnPaint(wxPaintEvent& event); | |
99 | void OnMouseEvent(wxMouseEvent& event); | |
100 | DECLARE_EVENT_TABLE() | |
101 | }; | |
102 | ||
103 | class MyChild: public wxFrame | |
104 | { | |
105 | public: | |
106 | MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style); | |
107 | virtual ~MyChild(); | |
108 | ||
109 | void OnQuit(wxCommandEvent& event); | |
110 | void OnNew(wxCommandEvent& event); | |
111 | void OnActivate(wxActivateEvent& event); | |
112 | ||
113 | MyCanvas *canvas; | |
114 | ||
115 | DECLARE_EVENT_TABLE() | |
116 | }; | |
117 | ||
118 | // ID for the menu quit command | |
119 | #define HELLO_QUIT 1 | |
120 | #define HELLO_NEW 2 | |
121 | ||
122 | DECLARE_APP(MyApp) | |
123 | ||
124 | // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP! | |
125 | IMPLEMENT_APP_NO_MAIN(MyApp) | |
126 | ||
127 | CMainWindow::CMainWindow() | |
128 | { | |
129 | LoadAccelTable( _T("MainAccelTable") ); | |
130 | Create( NULL, _T("Hello Foundation Application"), | |
131 | WS_OVERLAPPEDWINDOW, rectDefault, NULL, _T("MainMenu") ); | |
132 | } | |
133 | ||
134 | void CMainWindow::OnPaint() | |
135 | { | |
136 | CString s = _T("Hello, Windows!"); | |
137 | CPaintDC dc( this ); | |
138 | CRect rect; | |
139 | ||
140 | GetClientRect( rect ); | |
141 | dc.SetTextAlign( TA_BASELINE | TA_CENTER ); | |
142 | dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) ); | |
143 | dc.SetBkMode(TRANSPARENT); | |
144 | dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ), | |
145 | s, s.GetLength() ); | |
146 | } | |
147 | ||
148 | void CMainWindow::OnAbout() | |
149 | { | |
150 | CDialog about( _T("AboutBox"), this ); | |
151 | about.DoModal(); | |
152 | } | |
153 | ||
154 | void CMainWindow::OnTest() | |
155 | { | |
156 | wxMessageBox(_T("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), _T("wxWidgets"), wxOK); | |
157 | wxGetApp().CreateFrame(); | |
158 | } | |
159 | ||
160 | // CMainWindow message map: | |
161 | // Associate messages with member functions. | |
162 | // | |
163 | // It is implied that the ON_WM_PAINT macro expects a member function | |
164 | // "void OnPaint()". | |
165 | // | |
166 | // It is implied that members connected with the ON_COMMAND macro | |
167 | // receive no arguments and are void of return type, e.g., "void OnAbout()". | |
168 | // | |
169 | BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd ) | |
170 | //{{AFX_MSG_MAP( CMainWindow ) | |
171 | ON_WM_PAINT() | |
172 | ON_COMMAND( IDM_ABOUT, OnAbout ) | |
173 | ON_COMMAND( IDM_TEST, OnTest ) | |
174 | //}}AFX_MSG_MAP | |
175 | END_MESSAGE_MAP() | |
176 | ||
177 | BOOL CTheApp::InitInstance() | |
178 | { | |
179 | if ( !CWinApp::InitInstance() ) | |
180 | return FALSE; | |
181 | ||
182 | // TODO: cmd line parsing | |
183 | WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst); | |
184 | wxSetInstance(m_hInstance); | |
185 | wxApp::m_nCmdShow = m_nCmdShow; | |
186 | int argc = 0; | |
187 | wxChar **argv = NULL; | |
188 | wxEntryStart(argc, argv); | |
189 | if ( !wxTheApp || !wxTheApp->CallOnInit() ) | |
190 | return FALSE; | |
191 | ||
192 | #if START_WITH_MFC_WINDOW | |
193 | // Demonstrate creation of an initial MFC main window. | |
194 | m_pMainWnd = new CMainWindow(); | |
195 | m_pMainWnd->ShowWindow( m_nCmdShow ); | |
196 | m_pMainWnd->UpdateWindow(); | |
197 | #else | |
198 | // Demonstrate creation of an initial wxWidgets main window. | |
199 | // Wrap wxWidgets window in a dummy MFC window and | |
200 | // make the main window. | |
201 | if (wxTheApp && wxTheApp->GetTopWindow()) | |
202 | { | |
203 | m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND()); | |
204 | } | |
205 | #endif | |
206 | ||
207 | return TRUE; | |
208 | } | |
209 | ||
210 | int CTheApp::ExitInstance() | |
211 | { | |
212 | #if !START_WITH_MFC_WINDOW | |
213 | delete m_pMainWnd; | |
214 | #endif | |
215 | ||
216 | if ( wxTheApp ) | |
217 | wxTheApp->OnExit(); | |
218 | wxEntryCleanup(); | |
219 | ||
220 | return CWinApp::ExitInstance(); | |
221 | } | |
222 | ||
223 | // Override this to provide wxWidgets message loop compatibility | |
224 | BOOL CTheApp::PreTranslateMessage(MSG *msg) | |
225 | { | |
226 | wxEventLoop * const | |
227 | evtLoop = wx_static_cast(wxEventLoop *, wxEventLoop::GetActive()); | |
228 | if ( evtLoop && evtLoop->PreProcessMessage(msg) ) | |
229 | return TRUE; | |
230 | ||
231 | return CWinApp::PreTranslateMessage(msg); | |
232 | } | |
233 | ||
234 | BOOL CTheApp::OnIdle(LONG WXUNUSED(lCount)) | |
235 | { | |
236 | return wxTheApp && wxTheApp->ProcessIdle(); | |
237 | } | |
238 | ||
239 | /********************************************************************* | |
240 | * wxWidgets elements | |
241 | ********************************************************************/ | |
242 | ||
243 | bool MyApp::OnInit() | |
244 | { | |
245 | if ( !wxApp::OnInit() ) | |
246 | return false; | |
247 | ||
248 | #if !START_WITH_MFC_WINDOW | |
249 | // as we're not inside wxWidgets main loop, the default logic doesn't work | |
250 | // in our case and we need to do this explicitly | |
251 | SetExitOnFrameDelete(true); | |
252 | ||
253 | (void) CreateFrame(); | |
254 | #endif | |
255 | ||
256 | return true; | |
257 | } | |
258 | ||
259 | void MyApp::ExitMainLoop() | |
260 | { | |
261 | // instead of existing wxWidgets main loop, terminate the MFC one | |
262 | ::PostQuitMessage(0); | |
263 | } | |
264 | ||
265 | wxFrame *MyApp::CreateFrame() | |
266 | { | |
267 | MyChild *subframe = new MyChild(NULL, _T("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300), | |
268 | wxDEFAULT_FRAME_STYLE); | |
269 | ||
270 | subframe->SetTitle(_T("wxWidgets canvas frame")); | |
271 | ||
272 | // Give it a status line | |
273 | subframe->CreateStatusBar(); | |
274 | ||
275 | // Make a menubar | |
276 | wxMenu *file_menu = new wxMenu; | |
277 | ||
278 | file_menu->Append(HELLO_NEW, _T("&New MFC Window")); | |
279 | file_menu->Append(HELLO_QUIT, _T("&Close")); | |
280 | ||
281 | wxMenuBar *menu_bar = new wxMenuBar; | |
282 | ||
283 | menu_bar->Append(file_menu, _T("&File")); | |
284 | ||
285 | // Associate the menu bar with the frame | |
286 | subframe->SetMenuBar(menu_bar); | |
287 | ||
288 | int width, height; | |
289 | subframe->GetClientSize(&width, &height); | |
290 | ||
291 | MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height)); | |
292 | canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); | |
293 | subframe->canvas = canvas; | |
294 | subframe->Show(true); | |
295 | ||
296 | // Return the main frame window | |
297 | return subframe; | |
298 | } | |
299 | ||
300 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
301 | EVT_PAINT(MyCanvas::OnPaint) | |
302 | EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent) | |
303 | END_EVENT_TABLE() | |
304 | ||
305 | // Define a constructor for my canvas | |
306 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size) | |
307 | : wxScrolledWindow(parent, -1, pos, size) | |
308 | { | |
309 | } | |
310 | ||
311 | // Define the repainting behaviour | |
312 | void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
313 | { | |
314 | wxPaintDC dc(this); | |
315 | ||
316 | dc.SetFont(* wxSWISS_FONT); | |
317 | dc.SetPen(* wxGREEN_PEN); | |
318 | dc.DrawLine(0, 0, 200, 200); | |
319 | dc.DrawLine(200, 0, 0, 200); | |
320 | ||
321 | dc.SetBrush(* wxCYAN_BRUSH); | |
322 | dc.SetPen(* wxRED_PEN); | |
323 | dc.DrawRectangle(100, 100, 100, 50); | |
324 | dc.DrawRoundedRectangle(150, 150, 100, 50, 20); | |
325 | ||
326 | dc.DrawEllipse(250, 250, 100, 50); | |
327 | dc.DrawLine(50, 230, 200, 230); | |
328 | dc.DrawText(_T("This is a test string"), 50, 230); | |
329 | } | |
330 | ||
331 | // This implements a tiny doodling program! Drag the mouse using | |
332 | // the left button. | |
333 | void MyCanvas::OnMouseEvent(wxMouseEvent& event) | |
334 | { | |
335 | static long s_xpos = -1; | |
336 | static long s_ypos = -1; | |
337 | ||
338 | wxClientDC dc(this); | |
339 | dc.SetPen(* wxBLACK_PEN); | |
340 | wxPoint pos = event.GetPosition(); | |
341 | if (s_xpos > -1 && s_ypos > -1 && event.Dragging()) | |
342 | { | |
343 | dc.DrawLine(s_xpos, s_ypos, pos.x, pos.y); | |
344 | } | |
345 | ||
346 | s_xpos = pos.x; | |
347 | s_ypos = pos.y; | |
348 | } | |
349 | ||
350 | BEGIN_EVENT_TABLE(MyChild, wxFrame) | |
351 | EVT_MENU(HELLO_QUIT, MyChild::OnQuit) | |
352 | EVT_MENU(HELLO_NEW, MyChild::OnNew) | |
353 | EVT_ACTIVATE(MyChild::OnActivate) | |
354 | END_EVENT_TABLE() | |
355 | ||
356 | MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style) | |
357 | : wxFrame(frame, -1, title, pos, size, style) | |
358 | { | |
359 | canvas = NULL; | |
360 | } | |
361 | ||
362 | MyChild::~MyChild() | |
363 | { | |
364 | if ( IsLastBeforeExit() ) | |
365 | PostQuitMessage(0); | |
366 | } | |
367 | ||
368 | void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
369 | { | |
370 | Close(true); | |
371 | } | |
372 | ||
373 | void MyChild::OnNew(wxCommandEvent& WXUNUSED(event)) | |
374 | { | |
375 | CMainWindow *mainWin = new CMainWindow(); | |
376 | mainWin->ShowWindow( TRUE ); | |
377 | mainWin->UpdateWindow(); | |
378 | } | |
379 | ||
380 | void MyChild::OnActivate(wxActivateEvent& event) | |
381 | { | |
382 | if (event.GetActive() && canvas) | |
383 | canvas->SetFocus(); | |
384 | } | |
385 | ||
386 | // Dummy MFC window for specifying a valid main window to MFC, using | |
387 | // a wxWidgets HWND. | |
388 | CDummyWindow::CDummyWindow(HWND hWnd) | |
389 | { | |
390 | Attach(hWnd); | |
391 | } | |
392 | ||
393 | // Don't let the CWnd destructor delete the HWND | |
394 | CDummyWindow::~CDummyWindow() | |
395 | { | |
396 | Detach(); | |
397 | } | |
398 |