]>
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
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
)
127 CMainWindow::CMainWindow()
129 LoadAccelTable( _T("MainAccelTable") );
130 Create( NULL
, _T("Hello Foundation Application"),
131 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, _T("MainMenu") );
134 void CMainWindow::OnPaint()
136 CString s
= _T("Hello, Windows!");
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 ),
148 void CMainWindow::OnAbout()
150 CDialog
about( _T("AboutBox"), this );
154 void CMainWindow::OnTest()
156 wxMessageBox(_T("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), _T("wxWidgets"), wxOK
);
157 wxGetApp().CreateFrame();
160 // CMainWindow message map:
161 // Associate messages with member functions.
163 // It is implied that the ON_WM_PAINT macro expects a member function
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()".
169 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
170 //{{AFX_MSG_MAP( CMainWindow )
172 ON_COMMAND( IDM_ABOUT
, OnAbout
)
173 ON_COMMAND( IDM_TEST
, OnTest
)
177 BOOL
CTheApp::InitInstance()
179 if ( !CWinApp::InitInstance() )
182 // TODO: cmd line parsing
183 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
184 wxSetInstance(m_hInstance
);
185 wxApp::m_nCmdShow
= m_nCmdShow
;
187 wxChar
**argv
= NULL
;
188 wxEntryStart(argc
, argv
);
189 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
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();
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())
203 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
210 int CTheApp::ExitInstance()
212 #if !START_WITH_MFC_WINDOW
220 return CWinApp::ExitInstance();
223 // Override this to provide wxWidgets message loop compatibility
224 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
227 evtLoop
= wx_static_cast(wxEventLoop
*, wxEventLoop::GetActive());
228 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
231 return CWinApp::PreTranslateMessage(msg
);
234 BOOL
CTheApp::OnIdle(LONG
WXUNUSED(lCount
))
236 return wxTheApp
&& wxTheApp
->ProcessIdle();
239 /*********************************************************************
241 ********************************************************************/
245 if ( !wxApp::OnInit() )
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);
253 (void) CreateFrame();
259 void MyApp::ExitMainLoop()
261 // instead of existing wxWidgets main loop, terminate the MFC one
262 ::PostQuitMessage(0);
265 wxFrame
*MyApp::CreateFrame()
267 MyChild
*subframe
= new MyChild(NULL
, _T("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300),
268 wxDEFAULT_FRAME_STYLE
);
270 subframe
->SetTitle(_T("wxWidgets canvas frame"));
272 // Give it a status line
273 subframe
->CreateStatusBar();
276 wxMenu
*file_menu
= new wxMenu
;
278 file_menu
->Append(HELLO_NEW
, _T("&New MFC Window"));
279 file_menu
->Append(HELLO_QUIT
, _T("&Close"));
281 wxMenuBar
*menu_bar
= new wxMenuBar
;
283 menu_bar
->Append(file_menu
, _T("&File"));
285 // Associate the menu bar with the frame
286 subframe
->SetMenuBar(menu_bar
);
289 subframe
->GetClientSize(&width
, &height
);
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);
296 // Return the main frame window
300 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
301 EVT_PAINT(MyCanvas::OnPaint
)
302 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
305 // Define a constructor for my canvas
306 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
307 : wxScrolledWindow(parent
, -1, pos
, size
)
311 // Define the repainting behaviour
312 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
316 dc
.SetFont(* wxSWISS_FONT
);
317 dc
.SetPen(* wxGREEN_PEN
);
318 dc
.DrawLine(0, 0, 200, 200);
319 dc
.DrawLine(200, 0, 0, 200);
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);
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);
331 // This implements a tiny doodling program! Drag the mouse using
333 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
335 static long s_xpos
= -1;
336 static long s_ypos
= -1;
339 dc
.SetPen(* wxBLACK_PEN
);
340 wxPoint pos
= event
.GetPosition();
341 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
343 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
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
)
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
)
364 if ( IsLastBeforeExit() )
368 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
373 void MyChild::OnNew(wxCommandEvent
& WXUNUSED(event
))
375 CMainWindow
*mainWin
= new CMainWindow();
376 mainWin
->ShowWindow( TRUE
);
377 mainWin
->UpdateWindow();
380 void MyChild::OnActivate(wxActivateEvent
& event
)
382 if (event
.GetActive() && canvas
)
386 // Dummy MFC window for specifying a valid main window to MFC, using
388 CDummyWindow::CDummyWindow(HWND hWnd
)
393 // Don't let the CWnd destructor delete the HWND
394 CDummyWindow::~CDummyWindow()