The HELLOWIN Files

The three files necessary to create the ”Hello, Windows“ program are shown in Figure 1-5:

HELLOWIN.MAK is a ”make“ file.

HELLOWIN.C is the C source code file.

HELLOWIN.DEF is a module definition file.

HELLOWIN.MAK

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

# HELLOWIN.MAK make file

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

hellowin.exe : hellowin.obj hellowin.def

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

rc hellowin.exe

hellowin.obj : hellowin.c

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

HELLOWIN.C

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

HELLOWIN.C -- Displays "Hello, Windows!" in client area

(c) Charles Petzold, 1990

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

#include <windows.h>

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

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,

LPSTR lpszCmdParam, int nCmdShow)

{

static char szAppName[] = "HelloWin" ;

HWND hwnd ;

MSG msg ;

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 (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

RegisterClass (&wndclass) ;

}

hwnd = CreateWindow (szAppName, // window class name

"The Hello Program", // window caption

WS_OVERLAPPEDWINDOW, // window style

CW_USEDEFAULT, // initial x position

CW_USEDEFAULT, // initial y position

CW_USEDEFAULT, // initial x size

CW_USEDEFAULT, // initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters

ShowWindow (hwnd, nCmdShow) ;

UpdateWindow (hwnd) ;

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

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}

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

{

HDC hdc ;

PAINTSTRUCT ps ;

RECT rect ;

switch (message)

{

case WM_PAINT :

hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

DrawText (hdc, "Hello, Windows!", -1, &rect,

DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint (hwnd, &ps) ;

return 0 ;

case WM_DESTROY :

PostQuitMessage (0) ;

return 0 ;

}

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

}

HELLOWIN.DEF

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

; HELLOWIN.DEF module definition file

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

NAME HELLOWIN

DESCRIPTION 'Hello Windows Program (c) Charles Petzold, 1990'

EXETYPE WINDOWS

STUB 'WINSTUB.EXE'

CODE PRELOAD MOVEABLE DISCARDABLE

DATA PRELOAD MOVEABLE MULTIPLE

HEAPSIZE 1024

STACKSIZE 8192

EXPORTS WndProc

These are standard files that you'll create for every Windows program you write. Generally when you begin a new Windows program, you'll copy the standard files from an existing program and then make appropriate changes to them.

Most Windows programmers do all their program development and compiling outside of Windows under MS-DOS, and then load Windows to test the program. You can also create and compile a Windows program in the Microsoft C 6 Programmer's WorkBench. I'll be discussing the source code files as if you create them in a text editor of your choice and then compile the program from the MS-DOS command line outside of Windows.

If you have Windows, the Windows Software Development Kit, and the Microsoft C Professional Development System (the C 6 compiler) properly installed, you can create HELLOWIN.EXE from the three files shown in Figure 1-5 by executing:

NMAKE HELLOWIN.MAK

on the MS-DOS command line. You can then run Windows and the HELLOWIN.EXE program by executing:

WIN HELLOWIN

The program creates a normal application window as shown in Figure 1-6 on the following page. The window displays ”Hello, Windows!“ in the center of its client area.

When you think about it, this window has an amazing amount of functionality in its mere 80 lines of code. You can grab the title bar with the mouse pointer and move the window around the screen. You can grab the sizing borders and resize the window. When the window changes size, the program will automatically reposition the ”Hello, Windows!“ text string in the new center of the client area. You can press the maximize button and zoom HELLOWIN to fill the screen. You can press the minimize button and compress the program into an icon. You can invoke all these options from the system menu and, in addition, close the window to terminate the program.

While you may be pleased to see that HELLOWIN has all the functionality of a normal Windows program, you may not look so pleasant-faced when you see the source code required to create this program. But let's be brave while I proceed to dissect this program piece by piece and analyze it to death.