1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample to demonstrate mixing MFC and wxWidgets code
4 // Author: Julian Smart
6 // Copyright: (c) Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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.
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.
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.
24 // This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below.
26 #define START_WITH_MFC_WINDOW 0
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
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
39 // nmake -f makefile.vc BUILD=debug SHARED=0 DEBUG_RUNTIME_LIBS=0 RUNTIME_LIBS=static all
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
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
55 // For compilers that support precompilation, includes "wx/wx.h".
56 #include "wx/wxprec.h"
66 #include "wx/evtloop.h"
72 /////////////////////////////////////////////////////////////////////////////
75 // Just creating this application object runs the whole application.
81 // Define a new application type
82 class MyApp
: public wxApp
85 virtual bool OnInit();
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();
91 wxFrame
*CreateFrame();
94 class MyCanvas
: public wxScrolledWindow
97 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
98 void OnPaint(wxPaintEvent
& event
);
99 void OnMouseEvent(wxMouseEvent
& event
);
100 DECLARE_EVENT_TABLE()
103 class MyChild
: public wxFrame
106 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
109 void OnQuit(wxCommandEvent
& event
);
110 void OnNew(wxCommandEvent
& event
);
111 void OnActivate(wxActivateEvent
& event
);
115 DECLARE_EVENT_TABLE()
118 // ID for the menu quit command
124 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
125 IMPLEMENT_APP_NO_MAIN(MyApp
)
128 // In Unicode build MFC normally requires to manually change the entry point to
129 // wWinMainCRTStartup() but to avoid having to modify the project options to do
130 // it we provide an adapter for it.
131 extern "C" int wWinMainCRTStartup();
133 int WINAPI
WinMain(HINSTANCE
, HINSTANCE
, char *, int)
135 wWinMainCRTStartup();
139 CMainWindow
::CMainWindow()
141 LoadAccelTable( wxT("MainAccelTable") );
142 Create( NULL
, wxT("Hello Foundation Application"),
143 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, wxT("MainMenu") );
146 void CMainWindow
::OnPaint()
148 CString s
= wxT("Hello, Windows!");
152 GetClientRect( rect
);
153 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
154 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
155 dc
.SetBkMode(TRANSPARENT
);
156 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
160 void CMainWindow
::OnAbout()
162 CDialog
about( wxT("AboutBox"), this );
166 void CMainWindow
::OnTest()
168 wxMessageBox(wxT("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), wxT("wxWidgets"), wxOK
);
169 wxGetApp().CreateFrame();
172 // CMainWindow message map:
173 // Associate messages with member functions.
175 // It is implied that the ON_WM_PAINT macro expects a member function
178 // It is implied that members connected with the ON_COMMAND macro
179 // receive no arguments and are void of return type, e.g., "void OnAbout()".
181 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
182 //{{AFX_MSG_MAP( CMainWindow )
184 ON_COMMAND( IDM_ABOUT
, OnAbout
)
185 ON_COMMAND( IDM_TEST
, OnTest
)
189 BOOL CTheApp
::InitInstance()
191 if ( !CWinApp
::InitInstance() )
194 // TODO: cmd line parsing
195 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
196 wxSetInstance(m_hInstance
);
197 wxApp
::m_nCmdShow
= m_nCmdShow
;
199 wxChar
**argv
= NULL
;
200 wxEntryStart(argc
, argv
);
201 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
204 #if START_WITH_MFC_WINDOW
205 // Demonstrate creation of an initial MFC main window.
206 m_pMainWnd
= new CMainWindow();
207 m_pMainWnd
->ShowWindow( m_nCmdShow
);
208 m_pMainWnd
->UpdateWindow();
210 // Demonstrate creation of an initial wxWidgets main window.
211 // Wrap wxWidgets window in a dummy MFC window and
212 // make the main window.
213 if (wxTheApp
&& wxTheApp
->GetTopWindow())
215 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
222 int CTheApp
::ExitInstance()
224 #if !START_WITH_MFC_WINDOW
232 return CWinApp
::ExitInstance();
235 // Override this to provide wxWidgets message loop compatibility
236 BOOL CTheApp
::PreTranslateMessage(MSG
*msg
)
239 evtLoop
= static_cast<wxEventLoop
*>(wxEventLoop
::GetActive());
240 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
243 return CWinApp
::PreTranslateMessage(msg
);
246 BOOL CTheApp
::OnIdle(LONG
WXUNUSED(lCount
))
248 return wxTheApp
&& wxTheApp
->ProcessIdle();
251 /*********************************************************************
253 ********************************************************************/
257 if ( !wxApp
::OnInit() )
260 #if !START_WITH_MFC_WINDOW
261 // as we're not inside wxWidgets main loop, the default logic doesn't work
262 // in our case and we need to do this explicitly
263 SetExitOnFrameDelete(true);
265 (void) CreateFrame();
271 void MyApp
::ExitMainLoop()
273 // instead of existing wxWidgets main loop, terminate the MFC one
274 ::PostQuitMessage(0);
277 wxFrame
*MyApp
::CreateFrame()
279 MyChild
*subframe
= new MyChild(NULL
, wxT("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300),
280 wxDEFAULT_FRAME_STYLE
);
282 subframe
->SetTitle(wxT("wxWidgets canvas frame"));
284 // Give it a status line
285 subframe
->CreateStatusBar();
288 wxMenu
*file_menu
= new wxMenu
;
290 file_menu
->Append(HELLO_NEW
, wxT("&New MFC Window"));
291 file_menu
->Append(HELLO_QUIT
, wxT("&Close"));
293 wxMenuBar
*menu_bar
= new wxMenuBar
;
295 menu_bar
->Append(file_menu
, wxT("&File"));
297 // Associate the menu bar with the frame
298 subframe
->SetMenuBar(menu_bar
);
301 subframe
->GetClientSize(&width
, &height
);
303 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
304 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
305 subframe
->canvas
= canvas
;
306 subframe
->Show(true);
308 // Return the main frame window
312 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
313 EVT_PAINT(MyCanvas
::OnPaint
)
314 EVT_MOUSE_EVENTS(MyCanvas
::OnMouseEvent
)
317 // Define a constructor for my canvas
318 MyCanvas
::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
319 : wxScrolledWindow(parent
, -1, pos
, size
)
323 // Define the repainting behaviour
324 void MyCanvas
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
328 dc
.SetFont(* wxSWISS_FONT
);
329 dc
.SetPen(* wxGREEN_PEN
);
330 dc
.DrawLine(0, 0, 200, 200);
331 dc
.DrawLine(200, 0, 0, 200);
333 dc
.SetBrush(* wxCYAN_BRUSH
);
334 dc
.SetPen(* wxRED_PEN
);
335 dc
.DrawRectangle(100, 100, 100, 50);
336 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
338 dc
.DrawEllipse(250, 250, 100, 50);
339 dc
.DrawLine(50, 230, 200, 230);
340 dc
.DrawText(wxT("This is a test string"), 50, 230);
343 // This implements a tiny doodling program! Drag the mouse using
345 void MyCanvas
::OnMouseEvent(wxMouseEvent
& event
)
347 static long s_xpos
= -1;
348 static long s_ypos
= -1;
351 dc
.SetPen(* wxBLACK_PEN
);
352 wxPoint pos
= event
.GetPosition();
353 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
355 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
362 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
363 EVT_MENU(HELLO_QUIT
, MyChild
::OnQuit
)
364 EVT_MENU(HELLO_NEW
, MyChild
::OnNew
)
365 EVT_ACTIVATE(MyChild
::OnActivate
)
368 MyChild
::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
)
369 : wxFrame(frame
, -1, title
, pos
, size
, style
)
376 if ( IsLastBeforeExit() )
380 void MyChild
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
385 void MyChild
::OnNew(wxCommandEvent
& WXUNUSED(event
))
387 CMainWindow
*mainWin
= new CMainWindow();
388 mainWin
->ShowWindow( TRUE
);
389 mainWin
->UpdateWindow();
392 void MyChild
::OnActivate(wxActivateEvent
& event
)
394 if (event
.GetActive() && canvas
)
398 // Dummy MFC window for specifying a valid main window to MFC, using
400 CDummyWindow
::CDummyWindow(HWND hWnd
)
405 // Don't let the CWnd destructor delete the HWND
406 CDummyWindow
::~CDummyWindow()