]>
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
37 // suppress warning about WINVER not being defined from MFC
44 // For compilers that support precompilation, includes "wx/wx.h".
45 #include "wx/wxprec.h"
55 #include "wx/evtloop.h"
61 /////////////////////////////////////////////////////////////////////////////
64 // Just creating this application object runs the whole application.
70 // Define a new application type
71 class MyApp
: public wxApp
74 virtual bool OnInit();
76 wxFrame
*CreateFrame();
79 class MyCanvas
: public wxScrolledWindow
82 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
83 void OnPaint(wxPaintEvent
& event
);
84 void OnMouseEvent(wxMouseEvent
& event
);
88 class MyChild
: public wxFrame
91 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
94 void OnQuit(wxCommandEvent
& event
);
95 void OnNew(wxCommandEvent
& event
);
96 void OnActivate(wxActivateEvent
& event
);
100 DECLARE_EVENT_TABLE()
103 // ID for the menu quit command
109 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
110 IMPLEMENT_APP_NO_MAIN(MyApp
)
112 CMainWindow::CMainWindow()
114 LoadAccelTable( "MainAccelTable" );
115 Create( NULL
, "Hello Foundation Application",
116 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
119 void CMainWindow::OnPaint()
121 CString s
= "Hello, Windows!";
125 GetClientRect( rect
);
126 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
127 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
128 dc
.SetBkMode(TRANSPARENT
);
129 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
133 void CMainWindow::OnAbout()
135 CDialog
about( "AboutBox", this );
139 void CMainWindow::OnTest()
141 wxMessageBox("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame.", "wxWidgets", wxOK
);
142 wxGetApp().CreateFrame();
145 // CMainWindow message map:
146 // Associate messages with member functions.
148 // It is implied that the ON_WM_PAINT macro expects a member function
151 // It is implied that members connected with the ON_COMMAND macro
152 // receive no arguments and are void of return type, e.g., "void OnAbout()".
154 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
155 //{{AFX_MSG_MAP( CMainWindow )
157 ON_COMMAND( IDM_ABOUT
, OnAbout
)
158 ON_COMMAND( IDM_TEST
, OnTest
)
162 BOOL
CTheApp::InitInstance()
164 if ( !CWinApp::InitInstance() )
167 // TODO: cmd line parsing
168 WXDLLIMPEXP_BASE
void wxSetInstance(HINSTANCE hInst
);
169 wxSetInstance(m_hInstance
);
170 wxApp::m_nCmdShow
= m_nCmdShow
;
173 wxEntryStart(argc
, argv
);
174 if ( !wxTheApp
|| !wxTheApp
->CallOnInit() )
177 #if START_WITH_MFC_WINDOW
178 // Demonstrate creation of an initial MFC main window.
179 m_pMainWnd
= new CMainWindow();
180 m_pMainWnd
->ShowWindow( m_nCmdShow
);
181 m_pMainWnd
->UpdateWindow();
183 // Demonstrate creation of an initial wxWidgets main window.
184 // Wrap wxWidgets window in a dummy MFC window and
185 // make the main window.
186 if (wxTheApp
&& wxTheApp
->GetTopWindow())
188 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
195 int CTheApp::ExitInstance()
197 #if !START_WITH_MFC_WINDOW
205 return CWinApp::ExitInstance();
208 // Override this to provide wxWidgets message loop compatibility
209 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
211 wxEventLoop
*evtLoop
= wxEventLoop::GetActive();
212 if ( evtLoop
&& evtLoop
->PreProcessMessage(msg
) )
215 return CWinApp::PreTranslateMessage(msg
);
218 BOOL
CTheApp::OnIdle(LONG
WXUNUSED(lCount
))
220 return wxTheApp
&& wxTheApp
->ProcessIdle();
223 /*********************************************************************
225 ********************************************************************/
229 #if !START_WITH_MFC_WINDOW
231 // Exit app when the top level frame is deleted
232 SetExitOnFrameDelete(TRUE
);
234 (void) CreateFrame();
240 wxFrame
*MyApp::CreateFrame()
242 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
243 wxDEFAULT_FRAME_STYLE
);
245 subframe
->SetTitle("wxWidgets canvas frame");
247 // Give it a status line
248 subframe
->CreateStatusBar();
251 wxMenu
*file_menu
= new wxMenu
;
253 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
254 file_menu
->Append(HELLO_QUIT
, "&Close");
256 wxMenuBar
*menu_bar
= new wxMenuBar
;
258 menu_bar
->Append(file_menu
, "&File");
260 // Associate the menu bar with the frame
261 subframe
->SetMenuBar(menu_bar
);
264 subframe
->GetClientSize(&width
, &height
);
266 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
267 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
268 subframe
->canvas
= canvas
;
269 subframe
->Show(TRUE
);
271 // Return the main frame window
275 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
276 EVT_PAINT(MyCanvas::OnPaint
)
277 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
280 // Define a constructor for my canvas
281 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
282 : wxScrolledWindow(parent
, -1, pos
, size
)
286 // Define the repainting behaviour
287 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
291 dc
.SetFont(* wxSWISS_FONT
);
292 dc
.SetPen(* wxGREEN_PEN
);
293 dc
.DrawLine(0, 0, 200, 200);
294 dc
.DrawLine(200, 0, 0, 200);
296 dc
.SetBrush(* wxCYAN_BRUSH
);
297 dc
.SetPen(* wxRED_PEN
);
298 dc
.DrawRectangle(100, 100, 100, 50);
299 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
301 dc
.DrawEllipse(250, 250, 100, 50);
302 dc
.DrawLine(50, 230, 200, 230);
303 dc
.DrawText("This is a test string", 50, 230);
306 // This implements a tiny doodling program! Drag the mouse using
308 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
310 static long s_xpos
= -1;
311 static long s_ypos
= -1;
314 dc
.SetPen(* wxBLACK_PEN
);
315 wxPoint pos
= event
.GetPosition();
316 if (s_xpos
> -1 && s_ypos
> -1 && event
.Dragging())
318 dc
.DrawLine(s_xpos
, s_ypos
, pos
.x
, pos
.y
);
325 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
326 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
327 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
328 EVT_ACTIVATE(MyChild::OnActivate
)
331 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
)
332 : wxFrame(frame
, -1, title
, pos
, size
, style
)
339 if ( IsLastBeforeExit() )
343 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
348 void MyChild::OnNew(wxCommandEvent
& WXUNUSED(event
))
350 CMainWindow
*mainWin
= new CMainWindow();
351 mainWin
->ShowWindow( TRUE
);
352 mainWin
->UpdateWindow();
355 void MyChild::OnActivate(wxActivateEvent
& event
)
357 if (event
.GetActive() && canvas
)
361 // Dummy MFC window for specifying a valid main window to MFC, using
363 CDummyWindow::CDummyWindow(HWND hWnd
)
368 // Don't let the CWnd destructor delete the HWND
369 CDummyWindow::~CDummyWindow()