]>
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 // For compilers that support precompilation, includes "wx/wx.h".
51 #include "wx/wxprec.h"
60 #error Sorry, you need to edit include/wx/wxprec.h, comment out the windows.h inclusion, and recompile.
77 /////////////////////////////////////////////////////////////////////////////
80 // Just creating this application object runs the whole application.
86 // Define a new application type
87 class MyApp
: public wxApp
90 wxFrame
*CreateFrame(void);
93 class MyCanvas
: public wxScrolledWindow
96 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
97 void OnPaint(wxPaintEvent
& event
);
98 void OnMouseEvent(wxMouseEvent
& event
);
102 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
);
113 DECLARE_EVENT_TABLE()
116 // For drawing lines in a canvas
120 // ID for the menu quit command
127 /////////////////////////////////////////////////////////////////////////////
129 // CMainWindow constructor:
130 // Create the window with the appropriate style, size, menu, etc.
132 CMainWindow::CMainWindow()
134 LoadAccelTable( "MainAccelTable" );
135 Create( NULL
, "Hello Foundation Application",
136 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
140 // This routine draws the string "Hello, Windows!" in the center of the
141 // client area. It is called whenever Windows sends a WM_PAINT message.
142 // Note that creating a CPaintDC automatically does a BeginPaint and
143 // an EndPaint call is done when it is destroyed at the end of this
144 // function. CPaintDC's constructor needs the window (this).
146 void CMainWindow::OnPaint()
148 CString s
= "Hello, Windows!";
152 GetClientRect( rect
);
153 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
154 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
155 dc
.SetBkMode(TRANSPARENT
);
156 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
161 // This member function is called when a WM_COMMAND message with an
162 // IDM_ABOUT code is received by the CMainWindow class object. The
163 // message map below is responsible for this routing.
165 // We create a ClDialog object using the "AboutBox" resource (see
166 // hello.rc), and invoke it.
168 void CMainWindow::OnAbout()
170 CDialog
about( "AboutBox", this );
174 void CMainWindow::OnTest()
176 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
177 wxGetApp().CreateFrame();
180 // CMainWindow message map:
181 // Associate messages with member functions.
183 // It is implied that the ON_WM_PAINT macro expects a member function
186 // It is implied that members connected with the ON_COMMAND macro
187 // receive no arguments and are void of return type, e.g., "void OnAbout()".
189 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
190 //{{AFX_MSG_MAP( CMainWindow )
192 ON_COMMAND( IDM_ABOUT
, OnAbout
)
193 ON_COMMAND( IDM_TEST
, OnTest
)
197 /////////////////////////////////////////////////////////////////////////////
201 // When any CTheApp object is created, this member function is automatically
202 // called. Any data may be set up at this point.
204 // Also, the main window of the application should be created and shown here.
205 // Return TRUE if the initialization is successful.
207 BOOL
CTheApp::InitInstance()
209 TRACE( "HELLO WORLD\n" );
211 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
213 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
216 m_pMainWnd = new CMainWindow();
217 m_pMainWnd->ShowWindow( m_nCmdShow );
218 m_pMainWnd->UpdateWindow();
221 if (wxTheApp
&& wxTheApp
->GetTopWindow())
223 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
229 int CTheApp::ExitInstance()
231 // OnExit isn't called by CleanUp so must be called explicitly.
235 return CWinApp::ExitInstance();
238 // Override this to provide wxWindows message loop
241 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
243 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
246 return CWinApp::PreTranslateMessage(msg
);
249 BOOL
CTheApp::OnIdle(LONG lCount
)
252 return wxTheApp
->ProcessIdle();
257 /*********************************************************************
259 ********************************************************************/
261 bool MyApp::OnInit(void)
263 // Don't exit app when the top level frame is deleted
264 // SetExitOnFrameDelete(FALSE);
266 wxFrame
* frame
= CreateFrame();
270 wxFrame
*MyApp::CreateFrame(void)
272 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
273 wxDEFAULT_FRAME_STYLE
);
275 subframe
->SetTitle("wxWindows canvas frame");
277 // Give it a status line
278 subframe
->CreateStatusBar();
281 wxMenu
*file_menu
= new wxMenu
;
283 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
284 file_menu
->Append(HELLO_QUIT
, "&Close");
286 wxMenuBar
*menu_bar
= new wxMenuBar
;
288 menu_bar
->Append(file_menu
, "&File");
290 // Associate the menu bar with the frame
291 subframe
->SetMenuBar(menu_bar
);
294 subframe
->GetClientSize(&width
, &height
);
296 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
297 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
298 subframe
->canvas
= canvas
;
300 // Give it scrollbars
301 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
303 subframe
->Show(TRUE
);
304 // Return the main frame window
308 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
309 EVT_PAINT(MyCanvas::OnPaint
)
310 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
313 // Define a constructor for my canvas
314 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
315 wxScrolledWindow(parent
, -1, pos
, size
)
319 // Define the repainting behaviour
320 void MyCanvas::OnPaint(wxPaintEvent
& event
)
324 dc
.SetFont(* wxSWISS_FONT
);
325 dc
.SetPen(* wxGREEN_PEN
);
326 dc
.DrawLine(0, 0, 200, 200);
327 dc
.DrawLine(200, 0, 0, 200);
329 dc
.SetBrush(* wxCYAN_BRUSH
);
330 dc
.SetPen(* wxRED_PEN
);
331 dc
.DrawRectangle(100, 100, 100, 50);
332 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
334 dc
.DrawEllipse(250, 250, 100, 50);
335 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
336 dc
.DrawLine(50, 230, 200, 230);
337 dc
.DrawText("This is a test string", 50, 230);
340 // This implements a tiny doodling program! Drag the mouse using
342 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
345 dc
.SetPen(* wxBLACK_PEN
);
346 wxPoint pos
= event
.GetPosition();
347 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
349 dc
.DrawLine(xpos
, ypos
, pos
.x
, pos
.y
);
355 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
356 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
357 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
358 EVT_ACTIVATE(MyChild::OnActivate
)
361 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
362 wxFrame(frame
, -1, title
, pos
, size
, style
)
367 MyChild::~MyChild(void)
371 void MyChild::OnQuit(wxCommandEvent
& event
)
376 void MyChild::OnNew(wxCommandEvent
& event
)
378 CMainWindow
*mainWin
= new CMainWindow();
379 mainWin
->ShowWindow( TRUE
);
380 mainWin
->UpdateWindow();
383 void MyChild::OnActivate(wxActivateEvent
& event
)
385 if (event
.GetActive() && canvas
)
389 // Dummy MFC window for specifying a valid main window to MFC, using
391 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
396 // Don't let the CWnd destructor delete the HWND
397 CDummyWindow::~CDummyWindow(void)