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);
79 class MyCanvas
: public wxScrolledWindow
82 MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
);
83 void OnPaint(wxPaintEvent
& event
);
84 void OnMouseEvent(wxMouseEvent
& event
);
88 class MyChild
: public wxFrame
92 MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
);
96 void OnQuit(wxCommandEvent
& event
);
97 void OnNew(wxCommandEvent
& event
);
98 void OnActivate(wxActivateEvent
& event
);
100 DECLARE_EVENT_TABLE()
103 // For drawing lines in a canvas
107 // Initialise this in OnInit, not statically
111 // ID for the menu quit command
118 /////////////////////////////////////////////////////////////////////////////
120 // CMainWindow constructor:
121 // Create the window with the appropriate style, size, menu, etc.
123 CMainWindow
::CMainWindow()
125 LoadAccelTable( "MainAccelTable" );
126 Create( NULL
, "Hello Foundation Application",
127 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
131 // This routine draws the string "Hello, Windows!" in the center of the
132 // client area. It is called whenever Windows sends a WM_PAINT message.
133 // Note that creating a CPaintDC automatically does a BeginPaint and
134 // an EndPaint call is done when it is destroyed at the end of this
135 // function. CPaintDC's constructor needs the window (this).
137 void CMainWindow
::OnPaint()
139 CString s
= "Hello, Windows!";
143 GetClientRect( rect
);
144 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
145 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
146 dc
.SetBkMode(TRANSPARENT
);
147 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
152 // This member function is called when a WM_COMMAND message with an
153 // IDM_ABOUT code is received by the CMainWindow class object. The
154 // message map below is responsible for this routing.
156 // We create a ClDialog object using the "AboutBox" resource (see
157 // hello.rc), and invoke it.
159 void CMainWindow
::OnAbout()
161 CDialog
about( "AboutBox", this );
165 void CMainWindow
::OnTest()
167 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
168 wxGetApp().CreateFrame();
171 // CMainWindow message map:
172 // Associate messages with member functions.
174 // It is implied that the ON_WM_PAINT macro expects a member function
177 // It is implied that members connected with the ON_COMMAND macro
178 // receive no arguments and are void of return type, e.g., "void OnAbout()".
180 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
181 //{{AFX_MSG_MAP( CMainWindow )
183 ON_COMMAND( IDM_ABOUT
, OnAbout
)
184 ON_COMMAND( IDM_TEST
, OnTest
)
188 /////////////////////////////////////////////////////////////////////////////
192 // When any CTheApp object is created, this member function is automatically
193 // called. Any data may be set up at this point.
195 // Also, the main window of the application should be created and shown here.
196 // Return TRUE if the initialization is successful.
198 BOOL CTheApp
::InitInstance()
200 TRACE( "HELLO WORLD\n" );
202 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
204 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
207 m_pMainWnd = new CMainWindow();
208 m_pMainWnd->ShowWindow( m_nCmdShow );
209 m_pMainWnd->UpdateWindow();
212 if (wxTheApp
&& wxTheApp
->GetTopWindow())
214 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
220 int CTheApp
::ExitInstance()
224 return CWinApp
::ExitInstance();
227 // Override this to provide wxWindows message loop
230 BOOL CTheApp
::PreTranslateMessage(MSG
*msg
)
232 if (wxTheApp
&& wxTheApp
->ProcessMessage((WXMSG
*) msg
))
235 return CWinApp
::PreTranslateMessage(msg
);
238 BOOL CTheApp
::OnIdle(LONG lCount
)
241 return wxTheApp
->ProcessIdle();
246 /*********************************************************************
248 ********************************************************************/
250 bool MyApp
::OnInit(void)
252 // Don't exit app when the top level frame is deleted
253 // SetExitOnFrameDelete(FALSE);
256 red_pen
= new wxPen("RED", 3, wxSOLID
);
258 // Create a small font
259 small_font
= new wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
261 wxFrame
* frame
= CreateFrame();
265 wxFrame
*MyApp
::CreateFrame(void)
267 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
268 wxDEFAULT_FRAME_STYLE
);
270 subframe
->SetTitle("wxWindows canvas frame");
272 // Give it a status line
273 subframe
->CreateStatusBar();
276 wxMenu
*file_menu
= new wxMenu
;
278 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
279 file_menu
->Append(HELLO_QUIT
, "&Close");
281 wxMenuBar
*menu_bar
= new wxMenuBar
;
283 menu_bar
->Append(file_menu
, "&File");
285 // Associate the menu bar with the frame
286 subframe
->SetMenuBar(menu_bar
);
289 subframe
->GetClientSize(&width
, &height
);
291 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
292 wxCursor
*cursor
= new wxCursor(wxCURSOR_PENCIL
);
293 canvas
->SetCursor(*cursor
);
294 subframe
->canvas
= canvas
;
296 // Give it scrollbars
297 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
299 subframe
->Show(TRUE
);
300 // Return the main frame window
304 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
305 EVT_PAINT(MyCanvas
::OnPaint
)
306 EVT_MOUSE_EVENTS(MyCanvas
::OnMouseEvent
)
309 // Define a constructor for my canvas
310 MyCanvas
::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
311 wxScrolledWindow(parent
, -1, pos
, size
)
315 // Define the repainting behaviour
316 void MyCanvas
::OnPaint(wxPaintEvent
& event
)
320 dc
.SetFont(* small_font
);
321 dc
.SetPen(* wxGREEN_PEN
);
322 dc
.DrawLine(0, 0, 200, 200);
323 dc
.DrawLine(200, 0, 0, 200);
325 dc
.SetBrush(* wxCYAN_BRUSH
);
326 dc
.SetPen(* wxRED_PEN
);
327 dc
.DrawRectangle(100, 100, 100, 50);
328 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
330 dc
.DrawEllipse(250, 250, 100, 50);
331 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
332 dc
.DrawLine(50, 230, 200, 230);
333 dc
.DrawText("This is a test string", 50, 230);
336 // This implements a tiny doodling program! Drag the mouse using
338 void MyCanvas
::OnMouseEvent(wxMouseEvent
& event
)
341 dc
.SetPen(* wxBLACK_PEN
);
343 event
.Position(&x
, &y
);
344 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
346 dc
.DrawLine(xpos
, ypos
, x
, y
);
352 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
353 EVT_MENU(HELLO_QUIT
, MyChild
::OnQuit
)
354 EVT_MENU(HELLO_NEW
, MyChild
::OnNew
)
355 EVT_ACTIVATE(MyChild
::OnActivate
)
358 MyChild
::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
359 wxFrame(frame
, -1, title
, pos
, size
, style
)
364 MyChild
::~MyChild(void)
368 void MyChild
::OnQuit(wxCommandEvent
& event
)
373 void MyChild
::OnNew(wxCommandEvent
& event
)
375 CMainWindow
*mainWin
= new CMainWindow();
376 mainWin
->ShowWindow( TRUE
);
377 mainWin
->UpdateWindow();
380 void MyChild
::OnActivate(wxActivateEvent
& event
)
382 if (event
.GetActive() && canvas
)
386 bool MyChild
::OnClose(void)
392 // Dummy MFC window for specifying a valid main window to MFC, using
394 CDummyWindow
::CDummyWindow(HWND hWnd
):CWnd()
399 // Don't let the CWnd destructor delete the HWND
400 CDummyWindow
::~CDummyWindow(void)