]>
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 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"
48 #error Sorry, you need to edit include/wx/wxprec.h, comment out the windows.h inclusion, and recompile.
65 /////////////////////////////////////////////////////////////////////////////
68 // Just creating this application object runs the whole application.
74 // Define a new application type
75 class MyApp
: public wxApp
78 wxFrame
*CreateFrame(void);
81 class MyCanvas
: public wxScrolledWindow
84 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
85 void OnPaint(wxPaintEvent
& event
);
86 void OnMouseEvent(wxMouseEvent
& event
);
90 class MyChild
: public wxFrame
94 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
97 void OnQuit(wxCommandEvent
& event
);
98 void OnNew(wxCommandEvent
& event
);
99 void OnActivate(wxActivateEvent
& event
);
101 DECLARE_EVENT_TABLE()
104 // For drawing lines in a canvas
108 // ID for the menu quit command
115 /////////////////////////////////////////////////////////////////////////////
117 // CMainWindow constructor:
118 // Create the window with the appropriate style, size, menu, etc.
120 CMainWindow::CMainWindow()
122 LoadAccelTable( "MainAccelTable" );
123 Create( NULL
, "Hello Foundation Application",
124 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
128 // This routine draws the string "Hello, Windows!" in the center of the
129 // client area. It is called whenever Windows sends a WM_PAINT message.
130 // Note that creating a CPaintDC automatically does a BeginPaint and
131 // an EndPaint call is done when it is destroyed at the end of this
132 // function. CPaintDC's constructor needs the window (this).
134 void CMainWindow::OnPaint()
136 CString s
= "Hello, Windows!";
140 GetClientRect( rect
);
141 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
142 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
143 dc
.SetBkMode(TRANSPARENT
);
144 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
149 // This member function is called when a WM_COMMAND message with an
150 // IDM_ABOUT code is received by the CMainWindow class object. The
151 // message map below is responsible for this routing.
153 // We create a ClDialog object using the "AboutBox" resource (see
154 // hello.rc), and invoke it.
156 void CMainWindow::OnAbout()
158 CDialog
about( "AboutBox", this );
162 void CMainWindow::OnTest()
164 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
165 wxGetApp().CreateFrame();
168 // CMainWindow message map:
169 // Associate messages with member functions.
171 // It is implied that the ON_WM_PAINT macro expects a member function
174 // It is implied that members connected with the ON_COMMAND macro
175 // receive no arguments and are void of return type, e.g., "void OnAbout()".
177 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
178 //{{AFX_MSG_MAP( CMainWindow )
180 ON_COMMAND( IDM_ABOUT
, OnAbout
)
181 ON_COMMAND( IDM_TEST
, OnTest
)
185 /////////////////////////////////////////////////////////////////////////////
189 // When any CTheApp object is created, this member function is automatically
190 // called. Any data may be set up at this point.
192 // Also, the main window of the application should be created and shown here.
193 // Return TRUE if the initialization is successful.
195 BOOL
CTheApp::InitInstance()
197 TRACE( "HELLO WORLD\n" );
199 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
201 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
204 m_pMainWnd = new CMainWindow();
205 m_pMainWnd->ShowWindow( m_nCmdShow );
206 m_pMainWnd->UpdateWindow();
209 if (wxTheApp
&& wxTheApp
->GetTopWindow())
211 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
217 int CTheApp::ExitInstance()
219 // OnExit isn't called by CleanUp so must be called explicitly.
223 return CWinApp::ExitInstance();
226 // Override this to provide wxWindows message loop
229 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
231 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
234 return CWinApp::PreTranslateMessage(msg
);
237 BOOL
CTheApp::OnIdle(LONG lCount
)
240 return wxTheApp
->ProcessIdle();
245 /*********************************************************************
247 ********************************************************************/
249 bool MyApp::OnInit(void)
251 // Don't exit app when the top level frame is deleted
252 // SetExitOnFrameDelete(FALSE);
254 wxFrame
* frame
= CreateFrame();
258 wxFrame
*MyApp::CreateFrame(void)
260 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
261 wxDEFAULT_FRAME_STYLE
);
263 subframe
->SetTitle("wxWindows canvas frame");
265 // Give it a status line
266 subframe
->CreateStatusBar();
269 wxMenu
*file_menu
= new wxMenu
;
271 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
272 file_menu
->Append(HELLO_QUIT
, "&Close");
274 wxMenuBar
*menu_bar
= new wxMenuBar
;
276 menu_bar
->Append(file_menu
, "&File");
278 // Associate the menu bar with the frame
279 subframe
->SetMenuBar(menu_bar
);
282 subframe
->GetClientSize(&width
, &height
);
284 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
285 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
286 subframe
->canvas
= canvas
;
288 // Give it scrollbars
289 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
291 subframe
->Show(TRUE
);
292 // Return the main frame window
296 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
297 EVT_PAINT(MyCanvas::OnPaint
)
298 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
301 // Define a constructor for my canvas
302 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
303 wxScrolledWindow(parent
, -1, pos
, size
)
307 // Define the repainting behaviour
308 void MyCanvas::OnPaint(wxPaintEvent
& event
)
312 dc
.SetFont(* wxSWISS_FONT
);
313 dc
.SetPen(* wxGREEN_PEN
);
314 dc
.DrawLine(0, 0, 200, 200);
315 dc
.DrawLine(200, 0, 0, 200);
317 dc
.SetBrush(* wxCYAN_BRUSH
);
318 dc
.SetPen(* wxRED_PEN
);
319 dc
.DrawRectangle(100, 100, 100, 50);
320 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
322 dc
.DrawEllipse(250, 250, 100, 50);
323 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
324 dc
.DrawLine(50, 230, 200, 230);
325 dc
.DrawText("This is a test string", 50, 230);
328 // This implements a tiny doodling program! Drag the mouse using
330 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
333 dc
.SetPen(* wxBLACK_PEN
);
335 event
.Position(&x
, &y
);
336 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
338 dc
.DrawLine(xpos
, ypos
, x
, y
);
344 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
345 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
346 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
347 EVT_ACTIVATE(MyChild::OnActivate
)
350 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
351 wxFrame(frame
, -1, title
, pos
, size
, style
)
356 MyChild::~MyChild(void)
360 void MyChild::OnQuit(wxCommandEvent
& event
)
365 void MyChild::OnNew(wxCommandEvent
& event
)
367 CMainWindow
*mainWin
= new CMainWindow();
368 mainWin
->ShowWindow( TRUE
);
369 mainWin
->UpdateWindow();
372 void MyChild::OnActivate(wxActivateEvent
& event
)
374 if (event
.GetActive() && canvas
)
378 // Dummy MFC window for specifying a valid main window to MFC, using
380 CDummyWindow::CDummyWindow(HWND hWnd
):CWnd()
385 // Don't let the CWnd destructor delete the HWND
386 CDummyWindow::~CDummyWindow(void)