]>
git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
740bfbbbc1585c1c434a1b17b18807ef80b684d7
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 NULL, not create a window.
30 // (2) Restore the MFC code to create a window in InitInstance, and remove
31 // creation of CDummyWindow.
33 // IMPORTANT NOTE: to compile this sample, you must first edit
34 // wx/src/msw/wx_main.cc, set NOWINMAIN to 1, and remake wxWindows
35 // (it only needs to recompile wx_main.cc).
36 // This eliminates the duplicate WinMain function which MFC implements.
38 // For compilers that support precompilation, includes "wx/wx.h".
39 #include "wx/wxprec.h"
61 /////////////////////////////////////////////////////////////////////////////
64 // Just creating this application object runs the whole application.
70 // Define a new application type
71 class MyApp
: public wxApp
74 wxFrame
*CreateFrame(void);
77 class MyCanvas
: public wxScrolledWindow
80 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
81 void OnPaint(wxPaintEvent
& event
);
82 void OnMouseEvent(wxMouseEvent
& event
);
86 class MyChild
: public wxFrame
90 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
);
101 // For drawing lines in a canvas
105 // ID for the menu quit command
112 /////////////////////////////////////////////////////////////////////////////
114 // CMainWindow constructor:
115 // Create the window with the appropriate style, size, menu, etc.
117 CMainWindow::CMainWindow()
119 LoadAccelTable( "MainAccelTable" );
120 Create( NULL
, "Hello Foundation Application",
121 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
125 // This routine draws the string "Hello, Windows!" in the center of the
126 // client area. It is called whenever Windows sends a WM_PAINT message.
127 // Note that creating a CPaintDC automatically does a BeginPaint and
128 // an EndPaint call is done when it is destroyed at the end of this
129 // function. CPaintDC's constructor needs the window (this).
131 void CMainWindow::OnPaint()
133 CString s
= "Hello, Windows!";
137 GetClientRect( rect
);
138 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
139 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
140 dc
.SetBkMode(TRANSPARENT
);
141 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
146 // This member function is called when a WM_COMMAND message with an
147 // IDM_ABOUT code is received by the CMainWindow class object. The
148 // message map below is responsible for this routing.
150 // We create a ClDialog object using the "AboutBox" resource (see
151 // hello.rc), and invoke it.
153 void CMainWindow::OnAbout()
155 CDialog
about( "AboutBox", this );
159 void CMainWindow::OnTest()
161 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
162 wxGetApp().CreateFrame();
165 // CMainWindow message map:
166 // Associate messages with member functions.
168 // It is implied that the ON_WM_PAINT macro expects a member function
171 // It is implied that members connected with the ON_COMMAND macro
172 // receive no arguments and are void of return type, e.g., "void OnAbout()".
174 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
175 //{{AFX_MSG_MAP( CMainWindow )
177 ON_COMMAND( IDM_ABOUT
, OnAbout
)
178 ON_COMMAND( IDM_TEST
, OnTest
)
182 /////////////////////////////////////////////////////////////////////////////
186 // When any CTheApp object is created, this member function is automatically
187 // called. Any data may be set up at this point.
189 // Also, the main window of the application should be created and shown here.
190 // Return TRUE if the initialization is successful.
192 BOOL
CTheApp::InitInstance()
194 TRACE( "HELLO WORLD\n" );
196 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
198 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
201 m_pMainWnd = new CMainWindow();
202 m_pMainWnd->ShowWindow( m_nCmdShow );
203 m_pMainWnd->UpdateWindow();
206 if (wxTheApp
&& wxTheApp
->GetTopWindow())
208 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
214 int CTheApp::ExitInstance()
216 // OnExit isn't called by CleanUp so must be called explicitly.
220 return CWinApp::ExitInstance();
223 // Override this to provide wxWindows message loop
226 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
228 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
231 return CWinApp::PreTranslateMessage(msg
);
234 BOOL
CTheApp::OnIdle(LONG lCount
)
237 return wxTheApp
->ProcessIdle();
242 /*********************************************************************
244 ********************************************************************/
246 bool MyApp::OnInit(void)
248 // Don't exit app when the top level frame is deleted
249 // SetExitOnFrameDelete(FALSE);
251 wxFrame
* frame
= CreateFrame();
255 wxFrame
*MyApp::CreateFrame(void)
257 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
258 wxDEFAULT_FRAME_STYLE
);
260 subframe
->SetTitle("wxWindows canvas frame");
262 // Give it a status line
263 subframe
->CreateStatusBar();
266 wxMenu
*file_menu
= new wxMenu
;
268 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
269 file_menu
->Append(HELLO_QUIT
, "&Close");
271 wxMenuBar
*menu_bar
= new wxMenuBar
;
273 menu_bar
->Append(file_menu
, "&File");
275 // Associate the menu bar with the frame
276 subframe
->SetMenuBar(menu_bar
);
279 subframe
->GetClientSize(&width
, &height
);
281 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
282 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
283 subframe
->canvas
= canvas
;
285 // Give it scrollbars
286 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
288 subframe
->Show(TRUE
);
289 // Return the main frame window
293 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
294 EVT_PAINT(MyCanvas::OnPaint
)
295 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
298 // Define a constructor for my canvas
299 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
300 wxScrolledWindow(parent
, -1, pos
, size
)
304 // Define the repainting behaviour
305 void MyCanvas::OnPaint(wxPaintEvent
& event
)
309 dc
.SetFont(* wxSWISS_FONT
);
310 dc
.SetPen(* wxGREEN_PEN
);
311 dc
.DrawLine(0, 0, 200, 200);
312 dc
.DrawLine(200, 0, 0, 200);
314 dc
.SetBrush(* wxCYAN_BRUSH
);
315 dc
.SetPen(* wxRED_PEN
);
316 dc
.DrawRectangle(100, 100, 100, 50);
317 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
319 dc
.DrawEllipse(250, 250, 100, 50);
320 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
321 dc
.DrawLine(50, 230, 200, 230);
322 dc
.DrawText("This is a test string", 50, 230);
325 // This implements a tiny doodling program! Drag the mouse using
327 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
330 dc
.SetPen(* wxBLACK_PEN
);
332 event
.Position(&x
, &y
);
333 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
335 dc
.DrawLine(xpos
, ypos
, x
, y
);
341 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
342 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
343 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
344 EVT_ACTIVATE(MyChild::OnActivate
)
347 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
348 wxFrame(frame
, -1, title
, pos
, size
, style
)
353 MyChild::~MyChild(void)
357 void MyChild::OnQuit(wxCommandEvent
& event
)
362 void MyChild::OnNew(wxCommandEvent
& event
)
364 CMainWindow
*mainWin
= new CMainWindow();
365 mainWin
->ShowWindow( TRUE
);
366 mainWin
->UpdateWindow();
369 void MyChild::OnActivate(wxActivateEvent
& event
)
371 if (event
.GetActive() && canvas
)
375 bool MyChild::OnClose(void)
381 // Dummy MFC window for specifying a valid main window to MFC, using
383 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
388 // Don't let the CWnd destructor delete the HWND
389 CDummyWindow::~CDummyWindow(void)