]>
git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample to demonstrate mixing MFC and wxWidgets code
4 // Author: Julian Smart
5 // Copyright: (c) Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // This sample pops up an initial wxWidgets frame, with a menu item
10 // that allows a new MFC window to be created. Note that CDummyWindow
11 // is a class that allows a wxWidgets window to be seen as a CWnd
12 // for the purposes of specifying a valid main window to the
13 // MFC initialisation.
15 // You can easily modify this code so that an MFC window pops up
16 // initially as the main frame, and allows wxWidgets frames to be
17 // created subsequently.
19 // (1) Make MyApp::OnInit not create a main window.
20 // (2) Make MFC's InitInstance create a main window, and remove
21 // creation of CDummyWindow.
23 // This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below.
25 #define START_WITH_MFC_WINDOW 0
30 // (1) You may need to set wxUSE_MFC to 1 in include/wx/msw/setup.h but
31 // normally this shouldn't be needed any longer, i.e. it works without
34 // (2) You should link with MFC DLL, not static libraries: or, to use static
35 // run-time libraries, use this command for both building wxWidgets and
38 // nmake -f makefile.vc BUILD=debug SHARED=0 DEBUG_RUNTIME_LIBS=0 RUNTIME_LIBS=static all
40 // Unless the run-time library settings match for wxWidgets and MFC, you
41 // will get link errors for symbols such as __mbctype, __argc, and __argv
43 // (3) If you see bogus memory leaks within the MSVC IDE on exit, in this
44 // sample or in your own project, you must be using __WXDEBUG__ +
45 // WXUSINGDLL + _AFXDLL
46 // Unfortunately this confuses the MSVC/MFC leak detector. To do away with
47 // these bogus memory leaks, add this to the list of link objects, make it
48 // first: mfc[version][u]d.lib
49 // - [version] -> 42 or 70 or 80 etc
50 // - u if using Unicode
52 // Disable deprecation warnings from headers included from stdafx.h for VC8+
53 #ifndef _CRT_SECURE_NO_WARNINGS
54 #define _CRT_SECURE_NO_WARNINGS
57 // Also define WINVER to avoid warnings about it being undefined from the
58 // platform SDK headers (usually this is not necessary because it is done by wx
59 // headers but here we include the system ones before them)
66 // For compilers that support precompilation, includes "wx/wx.h".
67 #include "wx/wxprec.h"
77 #include "wx/evtloop.h"
83 /////////////////////////////////////////////////////////////////////////////
86 // Just creating this application object runs the whole application.
92 // Define a new application type
93 class MyApp
: public wxApp
96 virtual bool OnInit();
98 // we need to override this as the default behaviour only works when we're
99 // running wxWidgets main loop, not MFC one
100 virtual void ExitMainLoop();
102 wxFrame
*CreateFrame();
105 class MyCanvas
: public wxScrolledWindow
108 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
109 void OnPaint(wxPaintEvent
& event
);
110 void OnMouseEvent(wxMouseEvent
& event
);
111 DECLARE_EVENT_TABLE()
114 class MyChild
: public wxFrame
117 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
120 void OnQuit(wxCommandEvent
& event
);
121 void OnNew(wxCommandEvent
& event
);
122 void OnActivate(wxActivateEvent
& event
);
126 DECLARE_EVENT_TABLE()
129 // ID for the menu quit command
135 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
136 IMPLEMENT_APP_NO_MAIN(MyApp
)
139 // In Unicode build MFC normally requires to manually change the entry point to
140 // wWinMainCRTStartup() but to avoid having to modify the project options to do
141 // it we provide an adapter for it.
142 extern "C" int wWinMainCRTStartup();
144 int WINAPI
WinMain(HINSTANCE
, HINSTANCE
, char *, int)
146 wWinMainCRTStartup();
150 CMainWindow::CMainWindow()
152 LoadAccelTable( wxT("MainAccelTable") );
153 Create( NULL
, wxT("Hello Foundation Application"),
154 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, wxT("MainMenu") );
157 void CMainWindow::OnPaint()
159 CString s
= wxT("Hello, Windows!");
163 GetClientRect( rect
);
164 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
165 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
166 dc
.SetBkMode(TRANSPARENT
);
167 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
171 void CMainWindow::OnAbout()
173 CDialog
about( wxT("AboutBox"), this );
177 void CMainWindow::OnTest()
179 wxMessageBox(wxT("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), wxT("wxWidgets"), wxOK
);
180 wxGetApp().CreateFrame();
183 // CMainWindow message map:
184 // Associate messages with member functions.
186 // It is implied that the ON_WM_PAINT macro expects a member function
189 // It is implied that members connected with the ON_COMMAND macro
190 // receive no arguments and are void of return type, e.g., "void OnAbout()".
192 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
193 //{{AFX_MSG_MAP( CMainWindow )
195 ON_COMMAND( IDM_ABOUT
, OnAbout
)
196 ON_COMMAND( IDM_TEST
, OnTest
)
200 BOOL
CTheApp::InitInstance()
202 if ( !CWinApp::InitInstance() )
205 // TODO: cmd line parsing
206 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
207 wxSetInstance(m_hInstance
);
208 wxApp::m_nCmdShow
= m_nCmdShow
;
210 wxChar
**argv
= NULL
;
211 wxEntryStart(argc
, argv
);
212 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
215 #if START_WITH_MFC_WINDOW
216 // Demonstrate creation of an initial MFC main window.
217 m_pMainWnd
= new CMainWindow();
218 m_pMainWnd
->ShowWindow( m_nCmdShow
);
219 m_pMainWnd
->UpdateWindow();
221 // Demonstrate creation of an initial wxWidgets main window.
222 // Wrap wxWidgets window in a dummy MFC window and
223 // make the main window.
224 if (wxTheApp
&& wxTheApp
->GetTopWindow())
226 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
233 int CTheApp::ExitInstance()
235 #if !START_WITH_MFC_WINDOW
243 return CWinApp::ExitInstance();
246 // Override this to provide wxWidgets message loop compatibility
247 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
250 evtLoop
= static_cast<wxEventLoop
*>(wxEventLoop::GetActive());
251 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
254 return CWinApp::PreTranslateMessage(msg
);
257 BOOL
CTheApp::OnIdle(LONG
WXUNUSED(lCount
))
259 return wxTheApp
&& wxTheApp
->ProcessIdle();
262 /*********************************************************************
264 ********************************************************************/
268 if ( !wxApp::OnInit() )
271 #if !START_WITH_MFC_WINDOW
272 // as we're not inside wxWidgets main loop, the default logic doesn't work
273 // in our case and we need to do this explicitly
274 SetExitOnFrameDelete(true);
276 (void) CreateFrame();
282 void MyApp::ExitMainLoop()
284 // instead of existing wxWidgets main loop, terminate the MFC one
285 ::PostQuitMessage(0);
288 wxFrame
*MyApp::CreateFrame()
290 MyChild
*subframe
= new MyChild(NULL
, wxT("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300),
291 wxDEFAULT_FRAME_STYLE
);
293 subframe
->SetTitle(wxT("wxWidgets canvas frame"));
295 // Give it a status line
296 subframe
->CreateStatusBar();
299 wxMenu
*file_menu
= new wxMenu
;
301 file_menu
->Append(HELLO_NEW
, wxT("&New MFC Window"));
302 file_menu
->Append(HELLO_QUIT
, wxT("&Close"));
304 wxMenuBar
*menu_bar
= new wxMenuBar
;
306 menu_bar
->Append(file_menu
, wxT("&File"));
308 // Associate the menu bar with the frame
309 subframe
->SetMenuBar(menu_bar
);
312 subframe
->GetClientSize(&width
, &height
);
314 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
315 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
316 subframe
->canvas
= canvas
;
317 subframe
->Show(true);
319 // Return the main frame window
323 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
324 EVT_PAINT(MyCanvas::OnPaint
)
325 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
328 // Define a constructor for my canvas
329 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
330 : wxScrolledWindow(parent
, -1, pos
, size
)
334 // Define the repainting behaviour
335 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
339 dc
.SetFont(* wxSWISS_FONT
);
340 dc
.SetPen(* wxGREEN_PEN
);
341 dc
.DrawLine(0, 0, 200, 200);
342 dc
.DrawLine(200, 0, 0, 200);
344 dc
.SetBrush(* wxCYAN_BRUSH
);
345 dc
.SetPen(* wxRED_PEN
);
346 dc
.DrawRectangle(100, 100, 100, 50);
347 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
349 dc
.DrawEllipse(250, 250, 100, 50);
350 dc
.DrawLine(50, 230, 200, 230);
351 dc
.DrawText(wxT("This is a test string"), 50, 230);
354 // This implements a tiny doodling program! Drag the mouse using
356 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
358 static long s_xpos
= -1;
359 static long s_ypos
= -1;
362 dc
.SetPen(* wxBLACK_PEN
);
363 wxPoint pos
= event
.GetPosition();
364 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
366 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
373 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
374 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
375 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
376 EVT_ACTIVATE(MyChild::OnActivate
)
379 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
)
380 : wxFrame(frame
, -1, title
, pos
, size
, style
)
387 if ( IsLastBeforeExit() )
391 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
396 void MyChild::OnNew(wxCommandEvent
& WXUNUSED(event
))
398 CMainWindow
*mainWin
= new CMainWindow();
399 mainWin
->ShowWindow( TRUE
);
400 mainWin
->UpdateWindow();
403 void MyChild::OnActivate(wxActivateEvent
& event
)
405 if (event
.GetActive() && canvas
)
409 // Dummy MFC window for specifying a valid main window to MFC, using
411 CDummyWindow::CDummyWindow(HWND hWnd
)
416 // Don't let the CWnd destructor delete the HWND
417 CDummyWindow::~CDummyWindow()