Creating an "About" Dialog Box

Even if a Windows program requires no user input, it will often have a dialog box that is invoked by an About option on the menu. This dialog box displays the name and icon of the program, a copyright notice, a push button labeled OK, and perhaps other information. The first program we'll look at does nothing except display an About dialog box. The ABOUT1 program is shown in Figure 10-1.

ABOUT1.MAK

#----------------------

# ABOUT1.MAK make file

#----------------------

about1.exe : about1.obj about1.def about1.res

link about1, /align:16, NUL, /nod slibcew libw, about1

rc about1.res

about1.obj : about1.c about1.h

cl -c -Gsw -Ow -W2 -Zp about1.c

about1.res : about1.rc about1.h about1.ico

rc -r about1.rc

ABOUT1.C

/*------------------------------------------

ABOUT1.C -- About Box Demo Program No. 1

(c) Charles Petzold, 1990

------------------------------------------*/

#include <windows.h>

#include "about1.h"

long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,

LPSTR lpszCmdLine, int nCmdShow)

{

static char szAppName [] = "About1" ;

MSG msg ;

HWND hwnd ;

WNDCLASS wndclass ;

if (!hPrevInstance)

{

wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (hInstance, szAppName) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = szAppName ;

wndclass.lpszClassName = szAppName ;

RegisterClass (&wndclass) ;

}

hwnd = CreateWindow (szAppName, "About Box Demo Program",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT,

NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, nCmdShow) ;

UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}

BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)

{

switch (message)

{

case WM_INITDIALOG :

return TRUE ;

case WM_COMMAND :

switch (wParam)

{

case IDOK :

case IDCANCEL :

EndDialog (hDlg, 0) ;

return TRUE ;

}

break ;

}

return FALSE ;

}

long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)

{

static FARPROC lpfnAboutDlgProc ;

static HANDLE hInstance ;

switch (message)

{

case WM_CREATE :

hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;

lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance) ;

return 0 ;

case WM_COMMAND :

switch (wParam)

{

case IDM_ABOUT :

DialogBox (hInstance, "AboutBox", hwnd,

lpfnAboutDlgProc) ;

return 0 ;

}

break ;

case WM_DESTROY :

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (hwnd, message, wParam, lParam) ;

}

ABOUT1.RC

/*---------------------------

ABOUT1.RC resource script

---------------------------*/

#include <windows.h>

#include "about1.h"

About1 ICON about1.ico

About1 MENU

{

POPUP "&Help"

{

MENUITEM "&About About1...", IDM_ABOUT

}

}

AboutBox DIALOG 20, 20, 160, 80

STYLE WS_POPUP | WS_DLGFRAME

{

CTEXT "About1" -1, 0, 12, 160, 8

ICON "About1" -1, 8, 8, 0, 0

CTEXT "About Box Demo Program" -1, 0, 36, 160, 8

CTEXT "(c) Charles Petzold, 1990" -1, 0, 48, 160, 8

DEFPUSHBUTTON "OK" IDOK, 64, 60, 32, 14, WS_GROUP

}

ABOUT1.H

/*----------------------

ABOUT1.H header file

----------------------*/

#define IDM_ABOUT 1

ABOUT1.ICO

FIG 10-1 partial screen dump

ABOUT1.DEF

;-----------------------------------

; ABOUT1.DEF module definition file

;-----------------------------------

NAME ABOUT1

DESCRIPTION 'About Box Demo No. 1 (c) Charles Petzold, 1990'

EXETYPE WINDOWS

STUB 'WINSTUB.EXE'

CODE PRELOAD MOVEABLE DISCARDABLE

DATA PRELOAD MOVEABLE MULTIPLE

HEAPSIZE 1024

STACKSIZE 8192

EXPORTS WndProc

AboutDlgProc