3.12 File Listings

The code shown in listings 1-5 is available on your distribution disks as HELLO.H, HELLO.CPP, HELLO.DEF, HELLO.RC, and RESOURCE.H.

Listing 1

// HELLO.H - Declares the class interfaces for the Hello application.

#ifndef __HELLO_H__

#define __HELLO_H__

// CMainWindow:

// See hello.cpp for the code to the member functions

// and the message map.

//

class CMainWindow : public CFrameWnd

{

public:

CMainWindow();

afx_msg void OnPaint();

afx_msg void OnAbout();

DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////

// CTheApp:

// See hello.cpp for the code to the InitInstance member function.

//

class CTheApp : public CWinApp

{

public:

BOOL InitInstance();

};

Listing 2

// HELLO.CPP - Defines the class behaviors for the Hello application.

#include <afxwin.h>

#include "resource.h"

#include "hello.h"

/////////////////////////////////////////////////////////////////////////////

// theApp:

// Just creating this application object runs the whole application.

//

CTheApp theApp;

/////////////////////////////////////////////////////////////////////////////

// CMainWindow constructor:

// Create the window with the appropriate style, size, menu, etc.

//

CMainWindow::CMainWindow()

{

LoadAccelTable("MainAccelTable");

Create(NULL, "Hello Foundation Application",

WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu");

}

// OnPaint:

//

void CMainWindow::OnPaint()

{

CString s = "Hello, Windows!";

CPaintDC dc( this );

CRect rect;

GetClientRect( rect );

dc.SetTextAlign( TA_BASELINE | TA_CENTER );

dc.SetBkMode( TRANSPARENT );

dc.TextOut( (rect.right / 2), (rect.bottom / 2),

s, s.GetLength() );

}

// OnAbout:

//

void CMainWindow::OnAbout()

{

CModalDialog about( "AboutBox", this );

about.DoModal();

}

// CMainWindow message map:

//

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)

ON_WM_PAINT()

ON_COMMAND(IDM_ABOUT, OnAbout)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CTheApp

// InitInstance:

//

BOOL CTheApp::InitInstance()

{

TRACE( "HELLO WORLD\n" );

m_pMainWnd = new CMainWindow();

m_pMainWnd -> ShowWindow( m_nCmdShow );

m_pMainWnd -> UpdateWindow();

return TRUE;

}

Listing 3

NAME Hello

DESCRIPTION 'Hello Microsoft Foundation Classes Windows Application'

EXETYPE WINDOWS

STUB 'WINSTUB.EXE'

CODE PRELOAD FIXED DISCARDABLE

;than once concurrently

DATA PRELOAD FIXED MULTIPLE

HEAPSIZE 1024

STACKSIZE 4096

Listing 4

/* HELLO.RC : Defines the resources for the Hello application. */

#include <windows.h>

#include <afxres.h>

#include "resource.h"

AFX_IDI_STD_FRAME ICON hello.ico

MainMenu MENU

{

POPUP "&Help"

{

MENUITEM "&About Hello...\tF1", IDM_ABOUT

}

}

MainAccelTable ACCELERATORS

{

VK_F1, IDM_ABOUT, VIRTKEY

}

rcinclude hello.dlg

Listing 5

/* RESOURCE.H - Defines resource constants for Hello */

#define IDM_ABOUT 100