]>
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"
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);
83 class MyCanvas
: public wxScrolledWindow
86 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
87 void OnPaint(wxPaintEvent
& event
);
88 void OnMouseEvent(wxMouseEvent
& event
);
92 class MyChild
: public wxFrame
96 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
100 void OnQuit(wxCommandEvent
& event
);
101 void OnNew(wxCommandEvent
& event
);
102 void OnActivate(wxActivateEvent
& event
);
104 DECLARE_EVENT_TABLE()
107 // For drawing lines in a canvas
111 // Initialise this in OnInit, not statically
115 // ID for the menu quit command
122 /////////////////////////////////////////////////////////////////////////////
124 // CMainWindow constructor:
125 // Create the window with the appropriate style, size, menu, etc.
127 CMainWindow::CMainWindow()
129 LoadAccelTable( "MainAccelTable" );
130 Create( NULL
, "Hello Foundation Application",
131 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
135 // This routine draws the string "Hello, Windows!" in the center of the
136 // client area. It is called whenever Windows sends a WM_PAINT message.
137 // Note that creating a CPaintDC automatically does a BeginPaint and
138 // an EndPaint call is done when it is destroyed at the end of this
139 // function. CPaintDC's constructor needs the window (this).
141 void CMainWindow::OnPaint()
143 CString s
= "Hello, Windows!";
147 GetClientRect( rect
);
148 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
149 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
150 dc
.SetBkMode(TRANSPARENT
);
151 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
156 // This member function is called when a WM_COMMAND message with an
157 // IDM_ABOUT code is received by the CMainWindow class object. The
158 // message map below is responsible for this routing.
160 // We create a ClDialog object using the "AboutBox" resource (see
161 // hello.rc), and invoke it.
163 void CMainWindow::OnAbout()
165 CDialog
about( "AboutBox", this );
169 void CMainWindow::OnTest()
171 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
172 wxGetApp().CreateFrame();
175 // CMainWindow message map:
176 // Associate messages with member functions.
178 // It is implied that the ON_WM_PAINT macro expects a member function
181 // It is implied that members connected with the ON_COMMAND macro
182 // receive no arguments and are void of return type, e.g., "void OnAbout()".
184 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
185 //{{AFX_MSG_MAP( CMainWindow )
187 ON_COMMAND( IDM_ABOUT
, OnAbout
)
188 ON_COMMAND( IDM_TEST
, OnTest
)
192 /////////////////////////////////////////////////////////////////////////////
196 // When any CTheApp object is created, this member function is automatically
197 // called. Any data may be set up at this point.
199 // Also, the main window of the application should be created and shown here.
200 // Return TRUE if the initialization is successful.
202 BOOL
CTheApp::InitInstance()
204 TRACE( "HELLO WORLD\n" );
206 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
208 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
211 m_pMainWnd = new CMainWindow();
212 m_pMainWnd->ShowWindow( m_nCmdShow );
213 m_pMainWnd->UpdateWindow();
216 if (wxTheApp
&& wxTheApp
->GetTopWindow())
218 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
224 int CTheApp::ExitInstance()
228 return CWinApp::ExitInstance();
231 // Override this to provide wxWindows message loop
234 BOOL
CTheApp::PreTranslateMessage(MSG
*msg
)
236 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
239 return CWinApp::PreTranslateMessage(msg
);
242 BOOL
CTheApp::OnIdle(LONG lCount
)
245 return wxTheApp
->ProcessIdle();
250 /*********************************************************************
252 ********************************************************************/
254 bool MyApp::OnInit(void)
256 // Don't exit app when the top level frame is deleted
257 // SetExitOnFrameDelete(FALSE);
260 red_pen
= new wxPen("RED", 3, wxSOLID
);
262 // Create a small font
263 small_font
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
265 wxFrame
* frame
= CreateFrame();
269 wxFrame
*MyApp::CreateFrame(void)
271 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
274 subframe
->SetTitle("wxWindows canvas frame");
276 // Give it a status line
277 subframe
->CreateStatusBar();
280 wxMenu
*file_menu
= new wxMenu
;
282 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
283 file_menu
->Append(HELLO_QUIT
, "&Close");
285 wxMenuBar
*menu_bar
= new wxMenuBar
;
287 menu_bar
->Append(file_menu
, "&File");
289 // Associate the menu bar with the frame
290 subframe
->SetMenuBar(menu_bar
);
293 subframe
->GetClientSize(&width
, &height
);
295 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
296 wxCursor
*cursor
= new wxCursor(wxCURSOR_PENCIL
);
297 canvas
->SetCursor(cursor
);
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(small_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
);
347 event
.Position(&x
, &y
);
348 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
350 dc
.DrawLine(xpos
, ypos
, x
, y
);
356 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
357 EVT_MENU(HELLO_QUIT
, MyChild::OnQuit
)
358 EVT_MENU(HELLO_NEW
, MyChild::OnNew
)
359 EVT_ACTIVATE(MyChild::OnActivate
)
362 MyChild::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
363 wxFrame(frame
, -1, title
, pos
, size
, style
)
368 MyChild::~MyChild(void)
372 void MyChild::OnQuit(wxCommandEvent
& event
)
377 void MyChild::OnNew(wxCommandEvent
& event
)
379 CMainWindow
*mainWin
= new CMainWindow();
380 mainWin
->ShowWindow( TRUE
);
381 mainWin
->UpdateWindow();
384 void MyChild::OnActivate(wxActivateEvent
& event
)
386 if (event
.GetActive() && canvas
)
390 Bool
MyChild::OnClose(void)
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)