]>
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
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
53 // (4) Unicode builds may produce the linker error "unresolved external symbol _WinMain@16".
54 // MFC requires you to manually add the Unicode entry point to the linker settings,
55 // Entry point symbol -> wWinMainCRTStartup
59 // For compilers that support precompilation, includes "wx/wx.h".
60 #include "wx/wxprec.h"
70 #include "wx/evtloop.h"
76 /////////////////////////////////////////////////////////////////////////////
79 // Just creating this application object runs the whole application.
85 // Define a new application type
86 class MyApp
: public wxApp
89 virtual bool OnInit();
91 // we need to override this as the default behaviour only works when we're
92 // running wxWidgets main loop, not MFC one
93 virtual void ExitMainLoop();
95 wxFrame
*CreateFrame();
98 class MyCanvas
: public wxScrolledWindow
101 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
102 void OnPaint(wxPaintEvent
& event
);
103 void OnMouseEvent(wxMouseEvent
& event
);
104 DECLARE_EVENT_TABLE()
107 class MyChild
: public wxFrame
110 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
113 void OnQuit(wxCommandEvent
& event
);
114 void OnNew(wxCommandEvent
& event
);
115 void OnActivate(wxActivateEvent
& event
);
119 DECLARE_EVENT_TABLE()
122 // ID for the menu quit command
128 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
129 IMPLEMENT_APP_NO_MAIN(MyApp
)
131 CMainWindow::CMainWindow()
133 LoadAccelTable( wxT("MainAccelTable") );
134 Create( NULL
, wxT("Hello Foundation Application"),
135 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, wxT("MainMenu") );
138 void CMainWindow::OnPaint()
140 CString s
= wxT("Hello, Windows!");
144 GetClientRect( rect
);
145 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
146 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
147 dc
.SetBkMode(TRANSPARENT
);
148 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
152 void CMainWindow::OnAbout()
154 CDialog
about( wxT("AboutBox"), this );
158 void CMainWindow::OnTest()
160 wxMessageBox(wxT("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), wxT("wxWidgets"), wxOK
);
161 wxGetApp().CreateFrame();
164 // CMainWindow message map:
165 // Associate messages with member functions.
167 // It is implied that the ON_WM_PAINT macro expects a member function
170 // It is implied that members connected with the ON_COMMAND macro
171 // receive no arguments and are void of return type, e.g., "void OnAbout()".
173 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
174 //{{AFX_MSG_MAP( CMainWindow )
176 ON_COMMAND( IDM_ABOUT
, OnAbout
)
177 ON_COMMAND( IDM_TEST
, OnTest
)
181 BOOL
CTheApp::InitInstance()
183 if ( !CWinApp::InitInstance() )
186 // TODO: cmd line parsing
187 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
188 wxSetInstance(m_hInstance
);
189 wxApp::m_nCmdShow
= m_nCmdShow
;
191 wxChar
**argv
= NULL
;
192 wxEntryStart(argc
, argv
);
193 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
196 #if START_WITH_MFC_WINDOW
197 // Demonstrate creation of an initial MFC main window.
198 m_pMainWnd
= new CMainWindow();
199 m_pMainWnd
->ShowWindow( m_nCmdShow
);
200 m_pMainWnd
->UpdateWindow();
202 // Demonstrate creation of an initial wxWidgets main window.
203 // Wrap wxWidgets window in a dummy MFC window and
204 // make the main window.
205 if (wxTheApp
&& wxTheApp
->GetTopWindow())
207 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
214 int CTheApp::ExitInstance()
216 #if !START_WITH_MFC_WINDOW
224 return CWinApp::ExitInstance();
227 // Override this to provide wxWidgets message loop compatibility
228 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
231 evtLoop
= static_cast<wxEventLoop
*>(wxEventLoop::GetActive());
232 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
235 return CWinApp::PreTranslateMessage(msg
);
238 BOOL
CTheApp::OnIdle(LONG
WXUNUSED(lCount
))
240 return wxTheApp
&& wxTheApp
->ProcessIdle();
243 /*********************************************************************
245 ********************************************************************/
249 if ( !wxApp::OnInit() )
252 #if !START_WITH_MFC_WINDOW
253 // as we're not inside wxWidgets main loop, the default logic doesn't work
254 // in our case and we need to do this explicitly
255 SetExitOnFrameDelete(true);
257 (void) CreateFrame();
263 void MyApp::ExitMainLoop()
265 // instead of existing wxWidgets main loop, terminate the MFC one
266 ::PostQuitMessage(0);
269 wxFrame
*MyApp::CreateFrame()
271 MyChild
*subframe
= new MyChild(NULL
, wxT("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300),
272 wxDEFAULT_FRAME_STYLE
);
274 subframe
->SetTitle(wxT("wxWidgets canvas frame"));
276 // Give it a status line
277 subframe
->CreateStatusBar();
280 wxMenu
*file_menu
= new wxMenu
;
282 file_menu
->Append(HELLO_NEW
, wxT("&New MFC Window"));
283 file_menu
->Append(HELLO_QUIT
, wxT("&Close"));
285 wxMenuBar
*menu_bar
= new wxMenuBar
;
287 menu_bar
->Append(file_menu
, wxT("&File"));
289 // Associate the menu bar with the frame
290 subframe
->SetMenuBar(menu_bar
);
293 subframe
->GetClientSize(&width
, &height
);
295 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
296 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
297 subframe
->canvas
= canvas
;
298 subframe
->Show(true);
300 // Return the main frame window
304 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
305 EVT_PAINT(MyCanvas::OnPaint
)
306 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
309 // Define a constructor for my canvas
310 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
311 : wxScrolledWindow(parent
, -1, pos
, size
)
315 // Define the repainting behaviour
316 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
320 dc
.SetFont(* wxSWISS_FONT
);
321 dc
.SetPen(* wxGREEN_PEN
);
322 dc
.DrawLine(0, 0, 200, 200);
323 dc
.DrawLine(200, 0, 0, 200);
325 dc
.SetBrush(* wxCYAN_BRUSH
);
326 dc
.SetPen(* wxRED_PEN
);
327 dc
.DrawRectangle(100, 100, 100, 50);
328 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
330 dc
.DrawEllipse(250, 250, 100, 50);
331 dc
.DrawLine(50, 230, 200, 230);
332 dc
.DrawText(wxT("This is a test string"), 50, 230);
335 // This implements a tiny doodling program! Drag the mouse using
337 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
339 static long s_xpos
= -1;
340 static long s_ypos
= -1;
343 dc
.SetPen(* wxBLACK_PEN
);
344 wxPoint pos
= event
.GetPosition();
345 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
347 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
354 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
355 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
356 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
357 EVT_ACTIVATE(MyChild::OnActivate
)
360 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
)
361 : wxFrame(frame
, -1, title
, pos
, size
, style
)
368 if ( IsLastBeforeExit() )
372 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
377 void MyChild::OnNew(wxCommandEvent
& WXUNUSED(event
))
379 CMainWindow
*mainWin
= new CMainWindow();
380 mainWin
->ShowWindow( TRUE
);
381 mainWin
->UpdateWindow();
384 void MyChild::OnActivate(wxActivateEvent
& event
)
386 if (event
.GetActive() && canvas
)
390 // Dummy MFC window for specifying a valid main window to MFC, using
392 CDummyWindow::CDummyWindow(HWND hWnd
)
397 // Don't let the CWnd destructor delete the HWND
398 CDummyWindow::~CDummyWindow()