]>
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 not create a main window.
30 // (2) Make MFC's InitInstance create a main window, and remove
31 // creation of CDummyWindow.
33 // This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below.
35 #define START_WITH_MFC_WINDOW 0
40 // (1) You need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches
41 // off some debugging features and also removes the windows.h inclusion
42 // in wxprec.h (MFC headers don't like this to have been included previously).
43 // Set to 'Use MFC in a shared DLL' or add _AFXDLL to preprocessor settings.
44 // Then recompile wxWindows and this sample.
46 // (2) I can't get the sample to link and run using a static MFC library, only the DLL
47 // version. Perhaps someone else is a wizard at working out the required settings
48 // in the wxWin library and the sample; then debugging the assert problem may be
51 // (3) Compiling wxWindows in DLL mode currently includes windows.h, so you must only
52 // try linking wxWindows statically.
54 // For compilers that support precompilation, includes "wx/wx.h".
55 #include "wx/wxprec.h"
63 #if defined(_WINDOWS_) || !wxUSE_MFC
64 #error Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile.
81 /////////////////////////////////////////////////////////////////////////////
84 // Just creating this application object runs the whole application.
90 // Define a new application type
91 class MyApp
: public wxApp
94 wxFrame
*CreateFrame(void);
97 class MyCanvas
: public wxScrolledWindow
100 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
101 void OnPaint(wxPaintEvent
& event
);
102 void OnMouseEvent(wxMouseEvent
& event
);
103 DECLARE_EVENT_TABLE()
106 class MyChild
: public wxFrame
110 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
113 void OnQuit(wxCommandEvent
& event
);
114 void OnNew(wxCommandEvent
& event
);
115 void OnActivate(wxActivateEvent
& event
);
117 DECLARE_EVENT_TABLE()
120 // For drawing lines in a canvas
124 // ID for the menu quit command
131 /////////////////////////////////////////////////////////////////////////////
133 // CMainWindow constructor:
134 // Create the window with the appropriate style, size, menu, etc.
136 CMainWindow::CMainWindow()
138 LoadAccelTable( "MainAccelTable" );
139 Create( NULL
, "Hello Foundation Application",
140 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
144 // This routine draws the string "Hello, Windows!" in the center of the
145 // client area. It is called whenever Windows sends a WM_PAINT message.
146 // Note that creating a CPaintDC automatically does a BeginPaint and
147 // an EndPaint call is done when it is destroyed at the end of this
148 // function. CPaintDC's constructor needs the window (this).
150 void CMainWindow::OnPaint()
152 CString s
= "Hello, Windows!";
156 GetClientRect( rect
);
157 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
158 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
159 dc
.SetBkMode(TRANSPARENT
);
160 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
165 // This member function is called when a WM_COMMAND message with an
166 // IDM_ABOUT code is received by the CMainWindow class object. The
167 // message map below is responsible for this routing.
169 // We create a ClDialog object using the "AboutBox" resource (see
170 // hello.rc), and invoke it.
172 void CMainWindow::OnAbout()
174 CDialog
about( "AboutBox", this );
178 void CMainWindow::OnTest()
180 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
181 wxGetApp().CreateFrame();
184 // CMainWindow message map:
185 // Associate messages with member functions.
187 // It is implied that the ON_WM_PAINT macro expects a member function
190 // It is implied that members connected with the ON_COMMAND macro
191 // receive no arguments and are void of return type, e.g., "void OnAbout()".
193 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
194 //{{AFX_MSG_MAP( CMainWindow )
196 ON_COMMAND( IDM_ABOUT
, OnAbout
)
197 ON_COMMAND( IDM_TEST
, OnTest
)
201 /////////////////////////////////////////////////////////////////////////////
205 // When any CTheApp object is created, this member function is automatically
206 // called. Any data may be set up at this point.
208 // Also, the main window of the application should be created and shown here.
209 // Return TRUE if the initialization is successful.
211 BOOL
CTheApp::InitInstance()
213 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
215 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
217 #if START_WITH_MFC_WINDOW
218 // Demonstrate creation of an initial MFC main window.
219 m_pMainWnd
= new CMainWindow();
220 m_pMainWnd
->ShowWindow( m_nCmdShow
);
221 m_pMainWnd
->UpdateWindow();
223 // Demonstrate creation of an initial wxWindows main window.
224 // Wrap wxWindows window in a dummy MFC window and
225 // make the main window.
226 if (wxTheApp
&& wxTheApp
->GetTopWindow())
228 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
235 int CTheApp::ExitInstance()
237 // OnExit isn't called by CleanUp so must be called explicitly.
241 return CWinApp::ExitInstance();
244 // Override this to provide wxWindows message loop
247 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
249 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
252 return CWinApp::PreTranslateMessage(msg
);
255 BOOL
CTheApp::OnIdle(LONG lCount
)
258 return wxTheApp
->ProcessIdle();
263 /*********************************************************************
265 ********************************************************************/
267 bool MyApp::OnInit(void)
269 #if !START_WITH_MFC_WINDOW
271 // Exit app when the top level frame is deleted
272 SetExitOnFrameDelete(TRUE
);
274 (void) CreateFrame();
280 wxFrame
*MyApp::CreateFrame(void)
282 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
283 wxDEFAULT_FRAME_STYLE
);
285 subframe
->SetTitle("wxWindows canvas frame");
287 // Give it a status line
288 subframe
->CreateStatusBar();
291 wxMenu
*file_menu
= new wxMenu
;
293 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
294 file_menu
->Append(HELLO_QUIT
, "&Close");
296 wxMenuBar
*menu_bar
= new wxMenuBar
;
298 menu_bar
->Append(file_menu
, "&File");
300 // Associate the menu bar with the frame
301 subframe
->SetMenuBar(menu_bar
);
304 subframe
->GetClientSize(&width
, &height
);
306 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
307 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
308 subframe
->canvas
= canvas
;
309 subframe
->Show(TRUE
);
311 // Return the main frame window
315 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
316 EVT_PAINT(MyCanvas::OnPaint
)
317 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
320 // Define a constructor for my canvas
321 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
322 wxScrolledWindow(parent
, -1, pos
, size
)
326 // Define the repainting behaviour
327 void MyCanvas::OnPaint(wxPaintEvent
& event
)
331 dc
.SetFont(* wxSWISS_FONT
);
332 dc
.SetPen(* wxGREEN_PEN
);
333 dc
.DrawLine(0, 0, 200, 200);
334 dc
.DrawLine(200, 0, 0, 200);
336 dc
.SetBrush(* wxCYAN_BRUSH
);
337 dc
.SetPen(* wxRED_PEN
);
338 dc
.DrawRectangle(100, 100, 100, 50);
339 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
341 dc
.DrawEllipse(250, 250, 100, 50);
342 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
343 dc
.DrawLine(50, 230, 200, 230);
344 dc
.DrawText("This is a test string", 50, 230);
347 // This implements a tiny doodling program! Drag the mouse using
349 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
352 dc
.SetPen(* wxBLACK_PEN
);
353 wxPoint pos
= event
.GetPosition();
354 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
356 dc
.DrawLine(xpos
, ypos
, pos
.x
, pos
.y
);
362 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
363 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
364 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
365 EVT_ACTIVATE(MyChild::OnActivate
)
368 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
369 wxFrame(frame
, -1, title
, pos
, size
, style
)
374 MyChild::~MyChild(void)
378 void MyChild::OnQuit(wxCommandEvent
& event
)
383 void MyChild::OnNew(wxCommandEvent
& event
)
385 CMainWindow
*mainWin
= new CMainWindow();
386 mainWin
->ShowWindow( TRUE
);
387 mainWin
->UpdateWindow();
390 void MyChild::OnActivate(wxActivateEvent
& event
)
392 if (event
.GetActive() && canvas
)
396 // Dummy MFC window for specifying a valid main window to MFC, using
398 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
403 // Don't let the CWnd destructor delete the HWND
404 CDummyWindow::~CDummyWindow(void)