]>
git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
1 // hello.cpp : Defines the class behaviors for the application.
2 // Hello is a simple program which consists of a main window
3 // and an "About" dialog which can be invoked by a menu choice.
4 // It is intended to serve as a starting-point for new
7 // This is a part of the Microsoft Foundation Classes C++ library.
8 // Copyright (C) 1992 Microsoft Corporation
9 // All rights reserved.
11 // This source code is only intended as a supplement to the
12 // Microsoft Foundation Classes Reference and Microsoft
13 // WinHelp documentation provided with the library.
14 // See these sources for detailed information regarding the
15 // Microsoft Foundation Classes product.
17 // *** MODIFIED BY JULIAN SMART TO DEMONSTRATE CO-EXISTANCE WITH wxWINDOWS ***
19 // This sample pops up an initial wxWindows frame, with a menu item
20 // that allows a new MFC window to be created. Note that CDummyWindow
21 // is a class that allows a wxWindows window to be seen as a CWnd
22 // for the purposes of specifying a valid main window to the
23 // MFC initialisation.
25 // You can easily modify this code so that an MFC window pops up
26 // initially as the main frame, and allows wxWindows frames to be
27 // created subsequently:
29 // (1) Make MyApp::OnInit return FALSE, not creating a window.
30 // (2) Restore the MFC code to create a window in InitInstance, and remove
31 // creation of CDummyWindow.
35 // (1) You need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches
36 // off some debugging features and also removes the windows.h inclusion
37 // in wxprec.h (MFC headers don't like this to have been included previously).
38 // Then recompile wxWindows and this sample.
40 // (2) 10/3/2000, wxWindows 2.1.14: unfortunately there is an assert when
41 // the sample tries to create an MFC window. Any suggestions welcome. It may be
42 // a problem with conflicting project settings. Ignoring the assert (several times)
43 // allows the sample to continue. In release mode the asserts don't happen.
45 // (3) I can't get the sample to link using a static MFC library, only the DLL
46 // version. Perhaps someone else is a wizard at working out the required settings
47 // in the wxWin library and the sample; then debugging the assert problem may be
50 // (4) Compiling wxWindows in DLL mode currently includes windows.h, so you must only
51 // try linking wxWindows statically.
53 // For compilers that support precompilation, includes "wx/wx.h".
54 #include "wx/wxprec.h"
62 #if defined(_WINDOWS_) || !wxUSE_MFC
63 #error Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile.
80 /////////////////////////////////////////////////////////////////////////////
83 // Just creating this application object runs the whole application.
89 // Define a new application type
90 class MyApp
: public wxApp
93 wxFrame
*CreateFrame(void);
96 class MyCanvas
: public wxScrolledWindow
99 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
100 void OnPaint(wxPaintEvent
& event
);
101 void OnMouseEvent(wxMouseEvent
& event
);
102 DECLARE_EVENT_TABLE()
105 class MyChild
: public wxFrame
109 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
112 void OnQuit(wxCommandEvent
& event
);
113 void OnNew(wxCommandEvent
& event
);
114 void OnActivate(wxActivateEvent
& event
);
116 DECLARE_EVENT_TABLE()
119 // For drawing lines in a canvas
123 // ID for the menu quit command
130 /////////////////////////////////////////////////////////////////////////////
132 // CMainWindow constructor:
133 // Create the window with the appropriate style, size, menu, etc.
135 CMainWindow::CMainWindow()
137 LoadAccelTable( "MainAccelTable" );
138 Create( NULL
, "Hello Foundation Application",
139 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
143 // This routine draws the string "Hello, Windows!" in the center of the
144 // client area. It is called whenever Windows sends a WM_PAINT message.
145 // Note that creating a CPaintDC automatically does a BeginPaint and
146 // an EndPaint call is done when it is destroyed at the end of this
147 // function. CPaintDC's constructor needs the window (this).
149 void CMainWindow::OnPaint()
151 CString s
= "Hello, Windows!";
155 GetClientRect( rect
);
156 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
157 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
158 dc
.SetBkMode(TRANSPARENT
);
159 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
164 // This member function is called when a WM_COMMAND message with an
165 // IDM_ABOUT code is received by the CMainWindow class object. The
166 // message map below is responsible for this routing.
168 // We create a ClDialog object using the "AboutBox" resource (see
169 // hello.rc), and invoke it.
171 void CMainWindow::OnAbout()
173 CDialog
about( "AboutBox", this );
177 void CMainWindow::OnTest()
179 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
180 wxGetApp().CreateFrame();
183 // CMainWindow message map:
184 // Associate messages with member functions.
186 // It is implied that the ON_WM_PAINT macro expects a member function
189 // It is implied that members connected with the ON_COMMAND macro
190 // receive no arguments and are void of return type, e.g., "void OnAbout()".
192 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
193 //{{AFX_MSG_MAP( CMainWindow )
195 ON_COMMAND( IDM_ABOUT
, OnAbout
)
196 ON_COMMAND( IDM_TEST
, OnTest
)
200 /////////////////////////////////////////////////////////////////////////////
204 // When any CTheApp object is created, this member function is automatically
205 // called. Any data may be set up at this point.
207 // Also, the main window of the application should be created and shown here.
208 // Return TRUE if the initialization is successful.
210 BOOL
CTheApp::InitInstance()
212 TRACE( "HELLO WORLD\n" );
214 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
216 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
219 m_pMainWnd = new CMainWindow();
220 m_pMainWnd->ShowWindow( m_nCmdShow );
221 m_pMainWnd->UpdateWindow();
224 if (wxTheApp
&& wxTheApp
->GetTopWindow())
226 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
232 int CTheApp::ExitInstance()
234 // OnExit isn't called by CleanUp so must be called explicitly.
238 return CWinApp::ExitInstance();
241 // Override this to provide wxWindows message loop
244 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
246 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
249 return CWinApp::PreTranslateMessage(msg
);
252 BOOL
CTheApp::OnIdle(LONG lCount
)
255 return wxTheApp
->ProcessIdle();
260 /*********************************************************************
262 ********************************************************************/
264 bool MyApp::OnInit(void)
266 // Don't exit app when the top level frame is deleted
267 // SetExitOnFrameDelete(FALSE);
269 wxFrame
* frame
= CreateFrame();
273 wxFrame
*MyApp::CreateFrame(void)
275 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
276 wxDEFAULT_FRAME_STYLE
);
278 subframe
->SetTitle("wxWindows canvas frame");
280 // Give it a status line
281 subframe
->CreateStatusBar();
284 wxMenu
*file_menu
= new wxMenu
;
286 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
287 file_menu
->Append(HELLO_QUIT
, "&Close");
289 wxMenuBar
*menu_bar
= new wxMenuBar
;
291 menu_bar
->Append(file_menu
, "&File");
293 // Associate the menu bar with the frame
294 subframe
->SetMenuBar(menu_bar
);
297 subframe
->GetClientSize(&width
, &height
);
299 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
300 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
301 subframe
->canvas
= canvas
;
303 // Give it scrollbars
304 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
306 subframe
->Show(TRUE
);
307 // Return the main frame window
311 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
312 EVT_PAINT(MyCanvas::OnPaint
)
313 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
316 // Define a constructor for my canvas
317 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
318 wxScrolledWindow(parent
, -1, pos
, size
)
322 // Define the repainting behaviour
323 void MyCanvas::OnPaint(wxPaintEvent
& event
)
327 dc
.SetFont(* wxSWISS_FONT
);
328 dc
.SetPen(* wxGREEN_PEN
);
329 dc
.DrawLine(0, 0, 200, 200);
330 dc
.DrawLine(200, 0, 0, 200);
332 dc
.SetBrush(* wxCYAN_BRUSH
);
333 dc
.SetPen(* wxRED_PEN
);
334 dc
.DrawRectangle(100, 100, 100, 50);
335 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
337 dc
.DrawEllipse(250, 250, 100, 50);
338 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
339 dc
.DrawLine(50, 230, 200, 230);
340 dc
.DrawText("This is a test string", 50, 230);
343 // This implements a tiny doodling program! Drag the mouse using
345 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
348 dc
.SetPen(* wxBLACK_PEN
);
349 wxPoint pos
= event
.GetPosition();
350 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
352 dc
.DrawLine(xpos
, ypos
, pos
.x
, pos
.y
);
358 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
359 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
360 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
361 EVT_ACTIVATE(MyChild::OnActivate
)
364 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
365 wxFrame(frame
, -1, title
, pos
, size
, style
)
370 MyChild::~MyChild(void)
374 void MyChild::OnQuit(wxCommandEvent
& event
)
379 void MyChild::OnNew(wxCommandEvent
& event
)
381 CMainWindow
*mainWin
= new CMainWindow();
382 mainWin
->ShowWindow( TRUE
);
383 mainWin
->UpdateWindow();
386 void MyChild::OnActivate(wxActivateEvent
& event
)
388 if (event
.GetActive() && canvas
)
392 // Dummy MFC window for specifying a valid main window to MFC, using
394 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
399 // Don't let the CWnd destructor delete the HWND
400 CDummyWindow::~CDummyWindow(void)