]>
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 will get
42 // link errors for symbols such as __mbctype, __argc, and __argv
46 // For compilers that support precompilation, includes "wx/wx.h".
47 #include "wx/wxprec.h"
57 #include "wx/evtloop.h"
63 /////////////////////////////////////////////////////////////////////////////
66 // Just creating this application object runs the whole application.
72 // Define a new application type
73 class MyApp
: public wxApp
76 virtual bool OnInit();
78 wxFrame
*CreateFrame();
81 class MyCanvas
: public wxScrolledWindow
84 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
85 void OnPaint(wxPaintEvent
& event
);
86 void OnMouseEvent(wxMouseEvent
& event
);
90 class MyChild
: public wxFrame
93 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
96 void OnQuit(wxCommandEvent
& event
);
97 void OnNew(wxCommandEvent
& event
);
98 void OnActivate(wxActivateEvent
& event
);
102 DECLARE_EVENT_TABLE()
105 // ID for the menu quit command
111 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
112 IMPLEMENT_APP_NO_MAIN(MyApp
)
114 CMainWindow::CMainWindow()
116 LoadAccelTable( _T("MainAccelTable") );
117 Create( NULL
, _T("Hello Foundation Application"),
118 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, _T("MainMenu") );
121 void CMainWindow::OnPaint()
123 CString s
= _T("Hello, Windows!");
127 GetClientRect( rect
);
128 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
129 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
130 dc
.SetBkMode(TRANSPARENT
);
131 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
135 void CMainWindow::OnAbout()
137 CDialog
about( _T("AboutBox"), this );
141 void CMainWindow::OnTest()
143 wxMessageBox(_T("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), _T("wxWidgets"), wxOK
);
144 wxGetApp().CreateFrame();
147 // CMainWindow message map:
148 // Associate messages with member functions.
150 // It is implied that the ON_WM_PAINT macro expects a member function
153 // It is implied that members connected with the ON_COMMAND macro
154 // receive no arguments and are void of return type, e.g., "void OnAbout()".
156 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
157 //{{AFX_MSG_MAP( CMainWindow )
159 ON_COMMAND( IDM_ABOUT
, OnAbout
)
160 ON_COMMAND( IDM_TEST
, OnTest
)
164 BOOL
CTheApp::InitInstance()
166 if ( !CWinApp::InitInstance() )
169 // TODO: cmd line parsing
170 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
171 wxSetInstance(m_hInstance
);
172 wxApp::m_nCmdShow
= m_nCmdShow
;
174 wxChar
**argv
= NULL
;
175 wxEntryStart(argc
, argv
);
176 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
179 #if START_WITH_MFC_WINDOW
180 // Demonstrate creation of an initial MFC main window.
181 m_pMainWnd
= new CMainWindow();
182 m_pMainWnd
->ShowWindow( m_nCmdShow
);
183 m_pMainWnd
->UpdateWindow();
185 // Demonstrate creation of an initial wxWidgets main window.
186 // Wrap wxWidgets window in a dummy MFC window and
187 // make the main window.
188 if (wxTheApp
&& wxTheApp
->GetTopWindow())
190 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
197 int CTheApp::ExitInstance()
199 #if !START_WITH_MFC_WINDOW
207 return CWinApp::ExitInstance();
210 // Override this to provide wxWidgets message loop compatibility
211 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
213 wxEventLoop
*evtLoop
= wxEventLoop::GetActive();
214 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
217 return CWinApp::PreTranslateMessage(msg
);
220 BOOL
CTheApp::OnIdle(LONG
WXUNUSED(lCount
))
222 return wxTheApp
&& wxTheApp
->ProcessIdle();
225 /*********************************************************************
227 ********************************************************************/
231 #if !START_WITH_MFC_WINDOW
233 // Exit app when the top level frame is deleted
234 SetExitOnFrameDelete(TRUE
);
236 (void) CreateFrame();
242 wxFrame
*MyApp::CreateFrame()
244 MyChild
*subframe
= new MyChild(NULL
, _T("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300),
245 wxDEFAULT_FRAME_STYLE
);
247 subframe
->SetTitle(_T("wxWidgets canvas frame"));
249 // Give it a status line
250 subframe
->CreateStatusBar();
253 wxMenu
*file_menu
= new wxMenu
;
255 file_menu
->Append(HELLO_NEW
, _T("&New MFC Window"));
256 file_menu
->Append(HELLO_QUIT
, _T("&Close"));
258 wxMenuBar
*menu_bar
= new wxMenuBar
;
260 menu_bar
->Append(file_menu
, _T("&File"));
262 // Associate the menu bar with the frame
263 subframe
->SetMenuBar(menu_bar
);
266 subframe
->GetClientSize(&width
, &height
);
268 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
269 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
270 subframe
->canvas
= canvas
;
271 subframe
->Show(TRUE
);
273 // Return the main frame window
277 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
278 EVT_PAINT(MyCanvas::OnPaint
)
279 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
282 // Define a constructor for my canvas
283 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
284 : wxScrolledWindow(parent
, -1, pos
, size
)
288 // Define the repainting behaviour
289 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
293 dc
.SetFont(* wxSWISS_FONT
);
294 dc
.SetPen(* wxGREEN_PEN
);
295 dc
.DrawLine(0, 0, 200, 200);
296 dc
.DrawLine(200, 0, 0, 200);
298 dc
.SetBrush(* wxCYAN_BRUSH
);
299 dc
.SetPen(* wxRED_PEN
);
300 dc
.DrawRectangle(100, 100, 100, 50);
301 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
303 dc
.DrawEllipse(250, 250, 100, 50);
304 dc
.DrawLine(50, 230, 200, 230);
305 dc
.DrawText(_T("This is a test string"), 50, 230);
308 // This implements a tiny doodling program! Drag the mouse using
310 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
312 static long s_xpos
= -1;
313 static long s_ypos
= -1;
316 dc
.SetPen(* wxBLACK_PEN
);
317 wxPoint pos
= event
.GetPosition();
318 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
320 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
327 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
328 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
329 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
330 EVT_ACTIVATE(MyChild::OnActivate
)
333 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
)
334 : wxFrame(frame
, -1, title
, pos
, size
, style
)
341 if ( IsLastBeforeExit() )
345 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
350 void MyChild::OnNew(wxCommandEvent
& WXUNUSED(event
))
352 CMainWindow
*mainWin
= new CMainWindow();
353 mainWin
->ShowWindow( TRUE
);
354 mainWin
->UpdateWindow();
357 void MyChild::OnActivate(wxActivateEvent
& event
)
359 if (event
.GetActive() && canvas
)
363 // Dummy MFC window for specifying a valid main window to MFC, using
365 CDummyWindow::CDummyWindow(HWND hWnd
)
370 // Don't let the CWnd destructor delete the HWND
371 CDummyWindow::~CDummyWindow()