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
);
98 void OnQuit(wxCommandEvent
& event
);
99 void OnNew(wxCommandEvent
& event
);
100 void OnActivate(wxActivateEvent
& event
);
102 DECLARE_EVENT_TABLE()
105 // For drawing lines in a canvas
109 // ID for the menu quit command
116 /////////////////////////////////////////////////////////////////////////////
118 // CMainWindow constructor:
119 // Create the window with the appropriate style, size, menu, etc.
121 CMainWindow
::CMainWindow()
123 LoadAccelTable( "MainAccelTable" );
124 Create( NULL
, "Hello Foundation Application",
125 WS_OVERLAPPEDWINDOW
, rectDefault
, NULL
, "MainMenu" );
129 // This routine draws the string "Hello, Windows!" in the center of the
130 // client area. It is called whenever Windows sends a WM_PAINT message.
131 // Note that creating a CPaintDC automatically does a BeginPaint and
132 // an EndPaint call is done when it is destroyed at the end of this
133 // function. CPaintDC's constructor needs the window (this).
135 void CMainWindow
::OnPaint()
137 CString s
= "Hello, Windows!";
141 GetClientRect( rect
);
142 dc
.SetTextAlign( TA_BASELINE
| TA_CENTER
);
143 dc
.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT
) );
144 dc
.SetBkMode(TRANSPARENT
);
145 dc
.TextOut( ( rect
.right
/ 2 ), ( rect
.bottom
/ 2 ),
150 // This member function is called when a WM_COMMAND message with an
151 // IDM_ABOUT code is received by the CMainWindow class object. The
152 // message map below is responsible for this routing.
154 // We create a ClDialog object using the "AboutBox" resource (see
155 // hello.rc), and invoke it.
157 void CMainWindow
::OnAbout()
159 CDialog
about( "AboutBox", this );
163 void CMainWindow
::OnTest()
165 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK
);
166 wxGetApp().CreateFrame();
169 // CMainWindow message map:
170 // Associate messages with member functions.
172 // It is implied that the ON_WM_PAINT macro expects a member function
175 // It is implied that members connected with the ON_COMMAND macro
176 // receive no arguments and are void of return type, e.g., "void OnAbout()".
178 BEGIN_MESSAGE_MAP( CMainWindow
, CFrameWnd
)
179 //{{AFX_MSG_MAP( CMainWindow )
181 ON_COMMAND( IDM_ABOUT
, OnAbout
)
182 ON_COMMAND( IDM_TEST
, OnTest
)
186 /////////////////////////////////////////////////////////////////////////////
190 // When any CTheApp object is created, this member function is automatically
191 // called. Any data may be set up at this point.
193 // Also, the main window of the application should be created and shown here.
194 // Return TRUE if the initialization is successful.
196 BOOL CTheApp
::InitInstance()
198 TRACE( "HELLO WORLD\n" );
200 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
202 wxEntry((WXHINSTANCE
) m_hInstance
, (WXHINSTANCE
) m_hPrevInstance
, m_lpCmdLine
, m_nCmdShow
, FALSE
);
205 m_pMainWnd = new CMainWindow();
206 m_pMainWnd->ShowWindow( m_nCmdShow );
207 m_pMainWnd->UpdateWindow();
210 if (wxTheApp
&& wxTheApp
->GetTopWindow())
212 m_pMainWnd
= new CDummyWindow((HWND
) wxTheApp
->GetTopWindow()->GetHWND());
218 int CTheApp
::ExitInstance()
220 // OnExit isn't called by CleanUp so must be called explicitly.
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);
255 wxFrame
* frame
= CreateFrame();
259 wxFrame
*MyApp
::CreateFrame(void)
261 MyChild
*subframe
= new MyChild(NULL
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
262 wxDEFAULT_FRAME_STYLE
);
264 subframe
->SetTitle("wxWindows canvas frame");
266 // Give it a status line
267 subframe
->CreateStatusBar();
270 wxMenu
*file_menu
= new wxMenu
;
272 file_menu
->Append(HELLO_NEW
, "&New MFC Window");
273 file_menu
->Append(HELLO_QUIT
, "&Close");
275 wxMenuBar
*menu_bar
= new wxMenuBar
;
277 menu_bar
->Append(file_menu
, "&File");
279 // Associate the menu bar with the frame
280 subframe
->SetMenuBar(menu_bar
);
283 subframe
->GetClientSize(&width
, &height
);
285 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
286 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
287 subframe
->canvas
= canvas
;
289 // Give it scrollbars
290 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
292 subframe
->Show(TRUE
);
293 // Return the main frame window
297 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
298 EVT_PAINT(MyCanvas
::OnPaint
)
299 EVT_MOUSE_EVENTS(MyCanvas
::OnMouseEvent
)
302 // Define a constructor for my canvas
303 MyCanvas
::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
304 wxScrolledWindow(parent
, -1, pos
, size
)
308 // Define the repainting behaviour
309 void MyCanvas
::OnPaint(wxPaintEvent
& event
)
313 dc
.SetFont(* wxSWISS_FONT
);
314 dc
.SetPen(* wxGREEN_PEN
);
315 dc
.DrawLine(0, 0, 200, 200);
316 dc
.DrawLine(200, 0, 0, 200);
318 dc
.SetBrush(* wxCYAN_BRUSH
);
319 dc
.SetPen(* wxRED_PEN
);
320 dc
.DrawRectangle(100, 100, 100, 50);
321 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
323 dc
.DrawEllipse(250, 250, 100, 50);
324 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
325 dc
.DrawLine(50, 230, 200, 230);
326 dc
.DrawText("This is a test string", 50, 230);
329 // This implements a tiny doodling program! Drag the mouse using
331 void MyCanvas
::OnMouseEvent(wxMouseEvent
& event
)
334 dc
.SetPen(* wxBLACK_PEN
);
336 event
.Position(&x
, &y
);
337 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
339 dc
.DrawLine(xpos
, ypos
, x
, y
);
345 BEGIN_EVENT_TABLE(MyChild
, wxFrame
)
346 EVT_MENU(HELLO_QUIT
, MyChild
::OnQuit
)
347 EVT_MENU(HELLO_NEW
, MyChild
::OnNew
)
348 EVT_ACTIVATE(MyChild
::OnActivate
)
351 MyChild
::MyChild(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, const long style
):
352 wxFrame(frame
, -1, title
, pos
, size
, style
)
357 MyChild
::~MyChild(void)
361 void MyChild
::OnQuit(wxCommandEvent
& event
)
366 void MyChild
::OnNew(wxCommandEvent
& event
)
368 CMainWindow
*mainWin
= new CMainWindow();
369 mainWin
->ShowWindow( TRUE
);
370 mainWin
->UpdateWindow();
373 void MyChild
::OnActivate(wxActivateEvent
& event
)
375 if (event
.GetActive() && canvas
)
379 bool MyChild
::OnClose(void)
385 // Dummy MFC window for specifying a valid main window to MFC, using
387 CDummyWindow
::CDummyWindow(HWND hWnd
):CWnd()
392 // Don't let the CWnd destructor delete the HWND
393 CDummyWindow
::~CDummyWindow(void)