The RESOURC2 program, shown in Figure 8-2, is an upgraded version of RESOURC1 that includes a monochrome bitmap resource used to create a brush for the background of the client area. The bitmap was created in SDKPAINT with dimensions of 8 by 8, which is the minimum size for a brush.
RESOURC2.MAK
#------------------------
# RESOURC2.MAK make file
#------------------------
resourc2.exe : resourc2.obj resourc2.def resourc2.res
link resourc2, /align:16, NUL, /nod slibcew libw, resourc2
rc resourc2.res
resourc2.obj : resourc2.c
cl -c -Gsw -Ow -W2 -Zp resourc2.c
resourc2.res : resourc2.rc resourc2.ico resourc2.cur resourc2.bmp
rc -r resourc2.rc
RESOURC2.C
/*-----------------------------------------------------------
RESOURC2.C -- Icon and Cursor Demonstration Program No. 2
(c) Charles Petzold, 1990
-----------------------------------------------------------*/
#include <windows.h>
long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
char szAppName[] = "Resourc2" ;
HANDLE hInst ;
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HBITMAP hBitmap ;
HBRUSH hBrush ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
hBitmap = LoadBitmap (hInstance, szAppName) ;
hBrush = CreatePatternBrush (hBitmap) ;
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 (hInstance, szAppName) ;
wndclass.hbrBackground = hBrush ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hInst = hInstance ;
hwnd = CreateWindow (szAppName, "Icon and Cursor Demo",
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) ;
}
DeleteObject (hBrush) ; // clean up
DeleteObject (hBitmap) ;
return msg.wParam ;
}
long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
static HICON hIcon ;
static short cxIcon, cyIcon, cxClient, cyClient ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
short x, y ;
switch (message)
{
case WM_CREATE :
hIcon = LoadIcon (hInst, szAppName) ;
cxIcon = GetSystemMetrics (SM_CXICON) ;
cyIcon = GetSystemMetrics (SM_CYICON) ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
for (y = cyIcon ; y < cyClient ; y += 2 * cyIcon)
for (x = cxIcon ; x < cxClient ; x += 2 * cxIcon)
DrawIcon (hdc, x, y, hIcon) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
RESOURC2.RC
/*-----------------------------
RESOURC2.RC resource script
-----------------------------*/
resourc2 ICON resourc2.ico
resourc2 CURSOR resourc2.cur
resourc2 BITMAP resourc2.bmp
RESOURC2.ICO
RESOURC2.CUR
RESOURC2.BMP
RESOURC2.DEF
;-------------------------------------
; RESOURC2.DEF module definition file
;-------------------------------------
NAME RESOURC2
DESCRIPTION 'Icon and Cursor Demo Program No. 2 (c) Charles Petzold, 1990'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 8192
EXPORTS WndProc
The bitmap resource is included in the resource script in the same format as the icon and cursor:
resourc2 BITMAP resourc2.bmp
The LoadBitmap function used in WinMain is similar to the LoadIcon and LoadCursor calls. It returns a handle to a bitmap:
hBitmap = LoadBitmap (hInstance, szAppName) ;
This handle is then used to create a pattern brush. The brush is based on the bitmap:
hBrush = CreatePatternBrush (hBitmap) ;
When Windows fills an area of the display with this brush, the bitmap is repeated horizontally and vertically every eight pixels. We want to use this brush to color the background of the client area, which we accomplish when defining the window class:
wndclass.hbrBackground = hBrush ;
The major difference between bitmaps and other resources is of practical significance and can be simply stated: Bitmaps are GDI objects. They are not shared among instances of your program, and they are not automatically deleted from memory when your program terminates. Because bitmaps and brushes are GDI objects, they must be deleted before the program terminates. In RESOURC2 this is done at the end of WinMain:
DeleteObject (hBrush) ;
DeleteObject (hBitmap) ;