]>
git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample to demonstrate mixing MFC and wxWindows code
4 // Author: Julian Smart
6 // Copyright: (c) Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // This sample pops up an initial wxWindows 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 wxWindows 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 wxWindows 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 need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches
32 // off some debugging features and also removes the windows.h inclusion
33 // in wxprec.h (MFC headers don't like this to have been included previously).
34 // Set to 'Use MFC in a shared DLL' or add _AFXDLL to preprocessor settings.
35 // Then recompile wxWindows and this sample.
37 // (2) I can't get the sample to link and run using a static MFC library, only the DLL
38 // version. Perhaps someone else is a wizard at working out the required settings
39 // in the wxWin library and the sample; then debugging the assert problem may be
42 // (3) Compiling wxWindows in DLL mode currently includes windows.h, so you must only
43 // try linking wxWindows statically.
45 // For compilers that support precompilation, includes "wx/wx.h".
46 #include "wx/wxprec.h"
54 #if defined(_WINDOWS_) || !wxUSE_MFC
55 #error "Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile the library."
72 /////////////////////////////////////////////////////////////////////////////
75 // Just creating this application object runs the whole application.
81 // Define a new application type
82 class MyApp
: public wxApp
85 wxFrame
*CreateFrame(void);
88 class MyCanvas
: public wxScrolledWindow
91 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
92 void OnPaint(wxPaintEvent
& event
);
93 void OnMouseEvent(wxMouseEvent
& event
);
97 class MyChild
: public wxFrame
101 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
104 void OnQuit(wxCommandEvent
& event
);
105 void OnNew(wxCommandEvent
& event
);
106 void OnActivate(wxActivateEvent
& event
);
108 DECLARE_EVENT_TABLE()
111 // For drawing lines in a canvas
115 // ID for the menu quit command
122 /////////////////////////////////////////////////////////////////////////////
124 // CMainWindow constructor:
125 // Create the window with the appropriate style, size, menu, etc.
127 CMainWindow::CMainWindow()
129 LoadAccelTable( "MainAccelTable" );
130 Create( NULL
, "Hello Foundation Application",
131 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
135 // This routine draws the string "Hello, Windows!" in the center of the
136 // client area. It is called whenever Windows sends a WM_PAINT message.
137 // Note that creating a CPaintDC automatically does a BeginPaint and
138 // an EndPaint call is done when it is destroyed at the end of this
139 // function. CPaintDC's constructor needs the window (this).
141 void CMainWindow::OnPaint()
143 CString s
= "Hello, Windows!";
147 GetClientRect( rect
);
148 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
149 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
150 dc
.SetBkMode(TRANSPARENT
);
151 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
156 // This member function is called when a WM_COMMAND message with an
157 // IDM_ABOUT code is received by the CMainWindow class object. The
158 // message map below is responsible for this routing.
160 // We create a ClDialog object using the "AboutBox" resource (see
161 // hello.rc), and invoke it.
163 void CMainWindow::OnAbout()
165 CDialog
about( "AboutBox", this );
169 void CMainWindow::OnTest()
171 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
172 wxGetApp().CreateFrame();
175 // CMainWindow message map:
176 // Associate messages with member functions.
178 // It is implied that the ON_WM_PAINT macro expects a member function
181 // It is implied that members connected with the ON_COMMAND macro
182 // receive no arguments and are void of return type, e.g., "void OnAbout()".
184 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
185 //{{AFX_MSG_MAP( CMainWindow )
187 ON_COMMAND( IDM_ABOUT
, OnAbout
)
188 ON_COMMAND( IDM_TEST
, OnTest
)
192 /////////////////////////////////////////////////////////////////////////////
196 // When any CTheApp object is created, this member function is automatically
197 // called. Any data may be set up at this point.
199 // Also, the main window of the application should be created and shown here.
200 // Return TRUE if the initialization is successful.
202 BOOL
CTheApp::InitInstance()
204 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
206 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
208 #if START_WITH_MFC_WINDOW
209 // Demonstrate creation of an initial MFC main window.
210 m_pMainWnd
= new CMainWindow();
211 m_pMainWnd
->ShowWindow( m_nCmdShow
);
212 m_pMainWnd
->UpdateWindow();
214 // Demonstrate creation of an initial wxWindows main window.
215 // Wrap wxWindows window in a dummy MFC window and
216 // make the main window.
217 if (wxTheApp
&& wxTheApp
->GetTopWindow())
219 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
226 int CTheApp::ExitInstance()
228 // OnExit isn't called by CleanUp so must be called explicitly.
232 return CWinApp::ExitInstance();
235 // Override this to provide wxWindows message loop
238 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
240 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
243 return CWinApp::PreTranslateMessage(msg
);
246 BOOL
CTheApp::OnIdle(LONG lCount
)
249 return wxTheApp
->ProcessIdle();
254 /*********************************************************************
256 ********************************************************************/
258 bool MyApp::OnInit(void)
260 #if !START_WITH_MFC_WINDOW
262 // Exit app when the top level frame is deleted
263 SetExitOnFrameDelete(TRUE
);
265 (void) CreateFrame();
271 wxFrame
*MyApp::CreateFrame(void)
273 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
274 wxDEFAULT_FRAME_STYLE
);
276 subframe
->SetTitle("wxWindows canvas frame");
278 // Give it a status line
279 subframe
->CreateStatusBar();
282 wxMenu
*file_menu
= new wxMenu
;
284 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
285 file_menu
->Append(HELLO_QUIT
, "&Close");
287 wxMenuBar
*menu_bar
= new wxMenuBar
;
289 menu_bar
->Append(file_menu
, "&File");
291 // Associate the menu bar with the frame
292 subframe
->SetMenuBar(menu_bar
);
295 subframe
->GetClientSize(&width
, &height
);
297 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
298 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
299 subframe
->canvas
= canvas
;
300 subframe
->Show(TRUE
);
302 // Return the main frame window
306 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
307 EVT_PAINT(MyCanvas::OnPaint
)
308 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
311 // Define a constructor for my canvas
312 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
313 wxScrolledWindow(parent
, -1, pos
, size
)
317 // Define the repainting behaviour
318 void MyCanvas::OnPaint(wxPaintEvent
& event
)
322 dc
.SetFont(* wxSWISS_FONT
);
323 dc
.SetPen(* wxGREEN_PEN
);
324 dc
.DrawLine(0, 0, 200, 200);
325 dc
.DrawLine(200, 0, 0, 200);
327 dc
.SetBrush(* wxCYAN_BRUSH
);
328 dc
.SetPen(* wxRED_PEN
);
329 dc
.DrawRectangle(100, 100, 100, 50);
330 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
332 dc
.DrawEllipse(250, 250, 100, 50);
333 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
334 dc
.DrawLine(50, 230, 200, 230);
335 dc
.DrawText("This is a test string", 50, 230);
338 // This implements a tiny doodling program! Drag the mouse using
340 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
343 dc
.SetPen(* wxBLACK_PEN
);
344 wxPoint pos
= event
.GetPosition();
345 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
347 dc
.DrawLine(xpos
, ypos
, pos
.x
, pos
.y
);
353 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
354 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
355 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
356 EVT_ACTIVATE(MyChild::OnActivate
)
359 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
360 wxFrame(frame
, -1, title
, pos
, size
, style
)
365 MyChild::~MyChild(void)
369 void MyChild::OnQuit(wxCommandEvent
& event
)
374 void MyChild::OnNew(wxCommandEvent
& event
)
376 CMainWindow
*mainWin
= new CMainWindow();
377 mainWin
->ShowWindow( TRUE
);
378 mainWin
->UpdateWindow();
381 void MyChild::OnActivate(wxActivateEvent
& event
)
383 if (event
.GetActive() && canvas
)
387 // Dummy MFC window for specifying a valid main window to MFC, using
389 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
394 // Don't let the CWnd destructor delete the HWND
395 CDummyWindow::~CDummyWindow(void)