Windows程序设计(第五版)源代码A(chap14)

更新时间:2024-04-19 22:02:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

chap14-BitBlt

/*--------------------------------------- BITBLT.C -- BitBlt Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_INFORMATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static int cxClient, cyClient, cxSource, cySource ; HDC hdcClient, hdcWindow ; int x, y ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

cxSource = GetSystemMetrics (SM_CXSIZEFRAME) + GetSystemMetrics (SM_CXSIZE) ;

cySource = GetSystemMetrics (SM_CYSIZEFRAME) + GetSystemMetrics (SM_CYCAPTION) ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_PAINT:

hdcClient = BeginPaint (hwnd, &ps) ; hdcWindow = GetWindowDC (hwnd) ;

for (y = 0 ; y < cyClient ; y += cySource) for (x = 0 ; x < cxClient ; x += cxSource) {

BitBlt (hdcClient, x, y, cxSource, cySource, hdcWindow, 0, 0, SRCCOPY) ; }

ReleaseDC (hwnd, hdcWindow) ;

EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

PostQuitMessage (0) ; return 0 ; }

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

chap14-BitMask

/*------------------------------------------- BITMASK.C -- Bitmap Masking Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (LTGRAY_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ;

}

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static HBITMAP hBitmapImag, hBitmapMask ; static HINSTANCE hInstance ;

static int cxClient, cyClient, cxBitmap, cyBitmap ; BITMAP bitmap ;

HDC hdc, hdcMemImag, hdcMemMask ; int x, y ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

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

// Load the original image and get its size

hBitmapImag = LoadBitmap (hInstance, TEXT (\ GetObject (hBitmapImag, sizeof (BITMAP), &bitmap) ; cxBitmap = bitmap.bmWidth ; cyBitmap = bitmap.bmHeight ;

// Select the original image into a memory DC

hdcMemImag = CreateCompatibleDC (NULL) ; SelectObject (hdcMemImag, hBitmapImag) ;

// Create the monochrome mask bitmap and memory DC

hBitmapMask = CreateBitmap (cxBitmap, cyBitmap, 1, 1, NULL) ; hdcMemMask = CreateCompatibleDC (NULL) ; SelectObject (hdcMemMask, hBitmapMask) ;

// Color the mask bitmap black with a white ellipse

SelectObject (hdcMemMask, GetStockObject (BLACK_BRUSH)) ; Rectangle (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;

SelectObject (hdcMemMask, GetStockObject (WHITE_BRUSH)) ; Ellipse (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;

// Mask the original image

BitBlt (hdcMemImag, 0, 0, cxBitmap, cyBitmap, hdcMemMask, 0, 0, SRCAND) ;

DeleteDC (hdcMemImag) ; DeleteDC (hdcMemMask) ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

// Select bitmaps into memory DCs

hdcMemImag = CreateCompatibleDC (hdc) ; SelectObject (hdcMemImag, hBitmapImag) ;

hdcMemMask = CreateCompatibleDC (hdc) ; SelectObject (hdcMemMask, hBitmapMask) ;

// Center image

x = (cxClient - cxBitmap) / 2 ;

y = (cyClient - cyBitmap) / 2 ;

// Do the bitblts

BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemMask, 0, 0, 0x220326) ;

BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemImag, 0, 0, SRCPAINT) ;

DeleteDC (hdcMemImag) ; DeleteDC (hdcMemMask) ; EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

DeleteObject (hBitmapImag) ; DeleteObject (hBitmapMask) ; PostQuitMessage (0) ; return 0 ; }

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

chap14-BitMaskí·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file. // Used by BitMask.rc //

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif

chap14-Blowup

/*--------------------------------------- BLOWUP.C -- Video Magnifier Program (c) Charles Petzold, 1998

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

#include

#include // for abs definition #include \

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HACCEL hAccel ; HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

hAccel = LoadAccelerators (hInstance, szAppName) ;

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

if (!TranslateAccelerator (hwnd, hAccel, &msg)) {

TranslateMessage (&msg) ; DispatchMessage (&msg) ; } }

return msg.wParam ; }

void InvertBlock (HWND hwndScr, HWND hwnd, POINT ptBeg, POINT ptEnd) {

HDC hdc ;

hdc = GetDCEx (hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ; ClientToScreen (hwnd, &ptBeg) ; ClientToScreen (hwnd, &ptEnd) ;

PatBlt (hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y,

DSTINVERT) ;

ReleaseDC (hwndScr, hdc) ; }

HBITMAP CopyBitmap (HBITMAP hBitmapSrc) {

BITMAP bitmap ;

HBITMAP hBitmapDst ; HDC hdcSrc, hdcDst ;

GetObject (hBitmapSrc, sizeof (BITMAP), &bitmap) ; hBitmapDst = CreateBitmapIndirect (&bitmap) ;

hdcSrc = CreateCompatibleDC (NULL) ; hdcDst = CreateCompatibleDC (NULL) ;

SelectObject (hdcSrc, hBitmapSrc) ; SelectObject (hdcDst, hBitmapDst) ;

BitBlt (hdcDst, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcSrc, 0, 0, SRCCOPY) ;

DeleteDC (hdcSrc) ; DeleteDC (hdcDst) ;

return hBitmapDst ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static BOOL bCapturing, bBlocking ; static HBITMAP hBitmap ; static HWND hwndScr ;

static POINT ptBeg, ptEnd ; BITMAP bm ;

HBITMAP hBitmapClip ; HDC hdc, hdcMem ; int iEnable ; PAINTSTRUCT ps ; RECT rect ;

switch (message) {

case WM_LBUTTONDOWN: if (!bCapturing) {

if (LockWindowUpdate (hwndScr = GetDesktopWindow ())) {

bCapturing = TRUE ; SetCapture (hwnd) ;

SetCursor (LoadCursor (NULL, IDC_CROSS)) ; } else

MessageBeep (0) ; }

return 0 ;

case WM_RBUTTONDOWN: if (bCapturing) {

bBlocking = TRUE ;

ptBeg.x = LOWORD (lParam) ; ptBeg.y = HIWORD (lParam) ; ptEnd = ptBeg ;

InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ;

}

return 0 ;

case WM_MOUSEMOVE: if (bBlocking) {

InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ; ptEnd.x = LOWORD (lParam) ; ptEnd.y = HIWORD (lParam) ;

InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ; }

return 0 ;

case WM_LBUTTONUP: case WM_RBUTTONUP: if (bBlocking) {

InvertBlock (hwndScr, hwnd, ptBeg, ptEnd) ; ptEnd.x = LOWORD (lParam) ; ptEnd.y = HIWORD (lParam) ;

if (hBitmap) {

DeleteObject (hBitmap) ; hBitmap = NULL ; }

hdc = GetDC (hwnd) ;

hdcMem = CreateCompatibleDC (hdc) ;

hBitmap = CreateCompatibleBitmap (hdc, abs (ptEnd.x - ptBeg.x), abs (ptEnd.y - ptBeg.y)) ;

SelectObject (hdcMem, hBitmap) ;

StretchBlt (hdcMem, 0, 0, abs (ptEnd.x - ptBeg.x), abs (ptEnd.y - ptBeg.y),

hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y, SRCCOPY) ;

DeleteDC (hdcMem) ;

ReleaseDC (hwnd, hdc) ;

InvalidateRect (hwnd, NULL, TRUE) ;

}

if (bBlocking || bCapturing) {

bBlocking = bCapturing = FALSE ;

SetCursor (LoadCursor (NULL, IDC_ARROW)) ; ReleaseCapture () ;

LockWindowUpdate (NULL) ; }

return 0 ;

case WM_INITMENUPOPUP:

iEnable = IsClipboardFormatAvailable (CF_BITMAP) ? MF_ENABLED : MF_GRAYED ;

EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE, iEnable) ;

iEnable = hBitmap ? MF_ENABLED : MF_GRAYED ;

EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT, iEnable) ; EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY, iEnable) ; EnableMenuItem ((HMENU) wParam, IDM_EDIT_DELETE, iEnable) ; return 0 ;

case WM_COMMAND:

switch (LOWORD (wParam)) {

case IDM_EDIT_CUT: case IDM_EDIT_COPY: if (hBitmap) {

hBitmapClip = CopyBitmap (hBitmap) ; OpenClipboard (hwnd) ; EmptyClipboard () ;

SetClipboardData (CF_BITMAP, hBitmapClip) ; }

if (LOWORD (wParam) == IDM_EDIT_COPY) return 0 ;

// fall through for IDM_EDIT_CUT case IDM_EDIT_DELETE: if (hBitmap) {

DeleteObject (hBitmap) ; hBitmap = NULL ; }

InvalidateRect (hwnd, NULL, TRUE) ; return 0 ;

case IDM_EDIT_PASTE: if (hBitmap) {

DeleteObject (hBitmap) ; hBitmap = NULL ; }

OpenClipboard (hwnd) ;

hBitmapClip = GetClipboardData (CF_BITMAP) ;

if (hBitmapClip)

hBitmap = CopyBitmap (hBitmapClip) ;

CloseClipboard () ;

InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; }

break ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

if (hBitmap) {

GetClientRect (hwnd, &rect) ;

hdcMem = CreateCompatibleDC (hdc) ; SelectObject (hdcMem, hBitmap) ;

GetObject (hBitmap, sizeof (BITMAP), (PSTR) &bm) ; SetStretchBltMode (hdc, COLORONCOLOR) ;

StretchBlt (hdc, 0, 0, rect.right, rect.bottom,

hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY) ;

DeleteDC (hdcMem) ; }

EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY: if (hBitmap)

DeleteObject (hBitmap) ;

PostQuitMessage (0) ; return 0 ; }

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

chap14-Blowupí·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file. // Used by Blowup.rc //

#define IDM_EDIT_CUT 40001 #define IDM_EDIT_COPY 40002 #define IDM_EDIT_PASTE 40003 #define IDM_EDIT_DELETE 40004

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 103 #define _APS_NEXT_COMMAND_VALUE 40005 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif

chap14-Bounce

/*--------------------------------------- BOUNCE.C -- Bouncing Ball Program (c) Charles Petzold, 1998

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

#include #define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName[] = TEXT (\

HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {

static HBITMAP hBitmap ;

static int cxClient, cyClient, xCenter, yCenter, cxTotal,

cyTotal,

cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel ; HBRUSH hBrush ;

HDC hdc, hdcMem ; int iScale ;

switch (iMsg) {

case WM_CREATE:

hdc = GetDC (hwnd) ;

xPixel = GetDeviceCaps (hdc, ASPECTX) ; yPixel = GetDeviceCaps (hdc, ASPECTY) ; ReleaseDC (hwnd, hdc) ;

SetTimer (hwnd, ID_TIMER, 50, NULL) ; return 0 ;

case WM_SIZE:

xCenter = (cxClient = LOWORD (lParam)) / 2 ; yCenter = (cyClient = HIWORD (lParam)) / 2 ;

iScale = min (cxClient * xPixel, cyClient * yPixel) / 16 ;

cxRadius = iScale / xPixel ; cyRadius = iScale / yPixel ;

cxMove = max (1, cxRadius / 2) ; cyMove = max (1, cyRadius / 2) ;

cxTotal = 2 * (cxRadius + cxMove) ; cyTotal = 2 * (cyRadius + cyMove) ;

if (hBitmap)

DeleteObject (hBitmap) ;

hdc = GetDC (hwnd) ;

hdcMem = CreateCompatibleDC (hdc) ;

hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ; ReleaseDC (hwnd, hdc) ;

SelectObject (hdcMem, hBitmap) ;

Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ;

hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ;

SelectObject (hdcMem, hBrush) ;

SetBkColor (hdcMem, RGB (255, 0, 255)) ;

Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove) ;

DeleteDC (hdcMem) ;

DeleteObject (hBrush) ; return 0 ;

case WM_TIMER: if (!hBitmap) break ;

hdc = GetDC (hwnd) ;

hdcMem = CreateCompatibleDC (hdc) ; SelectObject (hdcMem, hBitmap) ;

BitBlt (hdc, xCenter - cxTotal / 2,

yCenter - cyTotal / 2, cxTotal, cyTotal, hdcMem, 0, 0, SRCCOPY) ;

ReleaseDC (hwnd, hdc) ; DeleteDC (hdcMem) ;

xCenter += cxMove ; yCenter += cyMove ;

if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0))

cxMove = -cxMove ;

if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0))

cyMove = -cyMove ;

return 0 ;

case WM_DESTROY: if (hBitmap)

DeleteObject (hBitmap) ;

KillTimer (hwnd, ID_TIMER) ; PostQuitMessage (0) ; return 0 ; }

return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }

chap14-Bricks1

/*---------------------------------------- BRICKS1.C -- LoadBitmap Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static HBITMAP hBitmap ;

static int cxClient, cyClient, cxSource, cySource ; BITMAP bitmap ;

HDC hdc, hdcMem ; HINSTANCE hInstance ; int x, y ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

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

hBitmap = LoadBitmap (hInstance, TEXT (\

GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;

cxSource = bitmap.bmWidth ; cySource = bitmap.bmHeight ;

return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

hdcMem = CreateCompatibleDC (hdc) ; SelectObject (hdcMem, hBitmap) ;

for (y = 0 ; y < cyClient ; y += cySource) for (x = 0 ; x < cxClient ; x += cxSource) {

BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY) ; }

DeleteDC (hdcMem) ; EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

DeleteObject (hBitmap) ; PostQuitMessage (0) ; return 0 ; }

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

chap14-Bricks1í·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file. // Used by Bricks1.rc //

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif

chap14-Bricks2

/*----------------------------------------- BRICKS2.C -- CreateBitmap Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ;

DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static BITMAP bitmap = { 0, 8, 8, 2, 1, 1 } ;

static BYTE bits [8][2] = { 0xFF, 0, 0x0C, 0, 0x0C, 0, 0x0C, 0, 0xFF, 0, 0xC0, 0, 0xC0, 0, 0xC0, 0 } ; static HBITMAP hBitmap ;

static int cxClient, cyClient, cxSource, cySource ; HDC hdc, hdcMem ; int x, y ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

bitmap.bmBits = bits ;

hBitmap = CreateBitmapIndirect (&bitmap) ; cxSource = bitmap.bmWidth ; cySource = bitmap.bmHeight ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

hdcMem = CreateCompatibleDC (hdc) ; SelectObject (hdcMem, hBitmap) ;

for (y = 0 ; y < cyClient ; y += cySource) for (x = 0 ; x < cxClient ; x += cxSource) {

BitBlt (hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY) ; }

DeleteDC (hdcMem) ; EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

DeleteObject (hBitmap) ; PostQuitMessage (0) ; return 0 ; }

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

chap14-Bricks3

/*----------------------------------------------- BRICKS3.C -- CreatePatternBrush Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HBITMAP hBitmap ; HBRUSH hBrush ; HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

hBitmap = LoadBitmap (hInstance, TEXT (\ hBrush = CreatePatternBrush (hBitmap) ; DeleteObject (hBitmap) ;

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 = hBrush ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\Demo\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

DeleteObject (hBrush) ; return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

switch (message) {

case WM_DESTROY:

PostQuitMessage (0) ; return 0 ; }

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

chap14-Bricks3í·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

// Used by Bricks3.rc //

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif

chap14-GrafMenu

/*---------------------------------------------- GRAFMENU.C -- Demonstrates Bitmap Menu Items (c) Charles Petzold, 1998

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

#include #include \

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; void AddHelpToSys (HINSTANCE, HWND) ; HMENU CreateMyMenu (HINSTANCE) ; HBITMAP StretchBitmap (HBITMAP) ; HBITMAP GetBitmapFont (int) ; void DeleteAllBitmaps (HWND) ;

TCHAR szAppName[] = TEXT (\

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\Menu Demonstration\

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {

HMENU hMenu ;

static int iCurrentFont = IDM_FONT_COUR ;

switch (iMsg) {

case WM_CREATE:

AddHelpToSys (((LPCREATESTRUCT) lParam)->hInstance, hwnd) ; hMenu = CreateMyMenu (((LPCREATESTRUCT) lParam)->hInstance) ; SetMenu (hwnd, hMenu) ;

CheckMenuItem (hMenu, iCurrentFont, MF_CHECKED) ;

return 0 ;

case WM_SYSCOMMAND:

switch (LOWORD (wParam)) {

case IDM_HELP:

MessageBox (hwnd, TEXT (\ szAppName, MB_OK | MB_ICONEXCLAMATION) ; return 0 ; }

break ;

case WM_COMMAND:

switch (LOWORD (wParam)) {

case IDM_FILE_NEW: case IDM_FILE_OPEN: case IDM_FILE_SAVE: case IDM_FILE_SAVE_AS: case IDM_EDIT_UNDO: case IDM_EDIT_CUT: case IDM_EDIT_COPY: case IDM_EDIT_PASTE: case IDM_EDIT_CLEAR: MessageBeep (0) ; return 0 ;

case IDM_FONT_COUR: case IDM_FONT_ARIAL: case IDM_FONT_TIMES:

hMenu = GetMenu (hwnd) ;

CheckMenuItem (hMenu, iCurrentFont, MF_UNCHECKED) ; iCurrentFont = LOWORD (wParam) ;

CheckMenuItem (hMenu, iCurrentFont, MF_CHECKED) ; return 0 ; }

break ;

case WM_DESTROY:

DeleteAllBitmaps (hwnd) ; PostQuitMessage (0) ; return 0 ; }

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

}

/*---------------------------------------------------- AddHelpToSys: Adds bitmap Help item to system menu ----------------------------------------------------*/

void AddHelpToSys (HINSTANCE hInstance, HWND hwnd) {

HBITMAP hBitmap ; HMENU hMenu ;

hMenu = GetSystemMenu (hwnd, FALSE);

hBitmap = StretchBitmap (LoadBitmap (hInstance, TEXT (\

AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ; AppendMenu (hMenu, MF_BITMAP, IDM_HELP, (PTSTR) (LONG) hBitmap) ; }

/*---------------------------------------------- CreateMyMenu: Assembles menu from components ----------------------------------------------*/

HMENU CreateMyMenu (HINSTANCE hInstance) {

HBITMAP hBitmap ;

HMENU hMenu, hMenuPopup ; int i ;

hMenu = CreateMenu () ;

hMenuPopup = LoadMenu (hInstance, TEXT (\

hBitmap = StretchBitmap (LoadBitmap (hInstance, TEXT (\

AppendMenu (hMenu, MF_BITMAP | MF_POPUP, (int) hMenuPopup, (PTSTR) (LONG) hBitmap) ;

hMenuPopup = LoadMenu (hInstance, TEXT (\

hBitmap = StretchBitmap (LoadBitmap (hInstance, TEXT (\

AppendMenu (hMenu, MF_BITMAP | MF_POPUP, (int) hMenuPopup, (PTSTR) (LONG) hBitmap) ;

hMenuPopup = CreateMenu () ;

for (i = 0 ; i < 3 ; i++) {

hBitmap = GetBitmapFont (i) ;

AppendMenu (hMenuPopup, MF_BITMAP, IDM_FONT_COUR + i, (PTSTR) (LONG) hBitmap) ; }

hBitmap = StretchBitmap (LoadBitmap (hInstance, TEXT (\

AppendMenu (hMenu, MF_BITMAP | MF_POPUP, (int) hMenuPopup, (PTSTR) (LONG) hBitmap) ; return hMenu ; }

/*---------------------------------------------------- StretchBitmap: Scales bitmap to display resolution ----------------------------------------------------*/

HBITMAP StretchBitmap (HBITMAP hBitmap1) {

BITMAP bm1, bm2 ; HBITMAP hBitmap2 ;

HDC hdc, hdcMem1, hdcMem2 ; int cxChar, cyChar ;

// Get the width and height of a system font character

cxChar = LOWORD (GetDialogBaseUnits ()) ; cyChar = HIWORD (GetDialogBaseUnits ()) ;

// Create 2 memory DCs compatible with the display

hdc = CreateIC (TEXT (\ hdcMem1 = CreateCompatibleDC (hdc) ; hdcMem2 = CreateCompatibleDC (hdc) ; DeleteDC (hdc) ;

// Get the dimensions of the bitmap to be stretched

GetObject (hBitmap1, sizeof (BITMAP), (PTSTR) &bm1) ;

// Scale these dimensions based on the system font size

bm2 = bm1 ;

bm2.bmWidth = (cxChar * bm2.bmWidth) / 4 ; bm2.bmHeight = (cyChar * bm2.bmHeight) / 8 ; bm2.bmWidthBytes = ((bm2.bmWidth + 15) / 16) * 2 ;

// Create a new bitmap of larger size

hBitmap2 = CreateBitmapIndirect (&bm2) ;

// Select the bitmaps in the memory DCs and do a StretchBlt

SelectObject (hdcMem1, hBitmap1) ; SelectObject (hdcMem2, hBitmap2) ;

StretchBlt (hdcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight,

hdcMem1, 0, 0, bm1.bmWidth, bm1.bmHeight, SRCCOPY) ;

// Clean up

DeleteDC (hdcMem1) ; DeleteDC (hdcMem2) ;

DeleteObject (hBitmap1) ;

return hBitmap2 ; }

/*------------------------------------------------ GetBitmapFont: Creates bitmaps with font names ------------------------------------------------*/

HBITMAP GetBitmapFont (int i) {

static TCHAR * szFaceName[3] = { TEXT (\New\TEXT (\

TEXT (\ HBITMAP hBitmap ; HDC hdc, hdcMem ; HFONT hFont ; SIZE size ; TEXTMETRIC tm ;

hdc = CreateIC (TEXT (\ GetTextMetrics (hdc, &tm) ;

hdcMem = CreateCompatibleDC (hdc) ;

hFont = CreateFont (2 * tm.tmHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

szFaceName[i]) ;

hFont = (HFONT) SelectObject (hdcMem, hFont) ; GetTextExtentPoint32 (hdcMem, szFaceName[i],

lstrlen (szFaceName[i]), &size);

hBitmap = CreateBitmap (size.cx, size.cy, 1, 1, NULL) ; SelectObject (hdcMem, hBitmap) ;

TextOut (hdcMem, 0, 0, szFaceName[i], lstrlen (szFaceName[i])) ;

DeleteObject (SelectObject (hdcMem, hFont)) ; DeleteDC (hdcMem) ; DeleteDC (hdc) ;

return hBitmap ; }

/*------------------------------------------------------- DeleteAllBitmaps: Deletes all the bitmaps in the menu -------------------------------------------------------*/

void DeleteAllBitmaps (HWND hwnd) {

HMENU hMenu ; int i ;

MENUITEMINFO mii = { sizeof (MENUITEMINFO), MIIM_SUBMENU | MIIM_TYPE } ;

// Delete Help bitmap on system menu

hMenu = GetSystemMenu (hwnd, FALSE);

GetMenuItemInfo (hMenu, IDM_HELP, FALSE, &mii) ; DeleteObject ((HBITMAP) mii.dwTypeData) ;

// Delete top-level menu bitmaps

hMenu = GetMenu (hwnd) ;

for (i = 0 ; i < 3 ; i++) {

GetMenuItemInfo (hMenu, i, TRUE, &mii) ;

DeleteObject ((HBITMAP) mii.dwTypeData) ; }

// Delete bitmap items on Font menu

hMenu = mii.hSubMenu ;;

for (i = 0 ; i < 3 ; i++) {

GetMenuItemInfo (hMenu, i, TRUE, &mii) ; DeleteObject ((HBITMAP) mii.dwTypeData) ; } }

chap14-GrafMenuí·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file. // Used by GrafMenu.rc //

#define IDM_FONT_COUR 101 #define IDM_FONT_ARIAL 102 #define IDM_FONT_TIMES 103 #define IDM_HELP 104 #define IDM_EDIT_UNDO 40005 #define IDM_EDIT_CUT 40006 #define IDM_EDIT_COPY 40007 #define IDM_EDIT_PASTE 40008 #define IDM_EDIT_CLEAR 40009 #define IDM_FILE_NEW 40010 #define IDM_FILE_OPEN 40011 #define IDM_FILE_SAVE 40012 #define IDM_FILE_SAVE_AS 40013

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 108 #define _APS_NEXT_COMMAND_VALUE 40014 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 105 #endif #endif

chap14-HelloBit

/*----------------------------------------- HELLOBIT.C -- Bitmap Demonstration (c) Charles Petzold, 1998

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

#include #include \

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;

UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static HBITMAP hBitmap ; static HDC hdcMem ;

static int cxBitmap, cyBitmap, cxClient, cyClient, iSize = IDM_BIG ;

static TCHAR * szText = TEXT (\ HDC hdc ; HMENU hMenu ; int x, y ; PAINTSTRUCT ps ; SIZE size ;

switch (message) {

case WM_CREATE:

hdc = GetDC (hwnd) ;

hdcMem = CreateCompatibleDC (hdc) ;

GetTextExtentPoint32 (hdc, szText, lstrlen (szText), &size) ; cxBitmap = size.cx ; cyBitmap = size.cy ;

hBitmap = CreateCompatibleBitmap (hdc, cxBitmap, cyBitmap) ;

ReleaseDC (hwnd, hdc) ;

SelectObject (hdcMem, hBitmap) ;

TextOut (hdcMem, 0, 0, szText, lstrlen (szText)) ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ;

return 0 ;

case WM_COMMAND:

hMenu = GetMenu (hwnd) ;

switch (LOWORD (wParam)) {

case IDM_BIG: case IDM_SMALL:

CheckMenuItem (hMenu, iSize, MF_UNCHECKED) ; iSize = LOWORD (wParam) ;

CheckMenuItem (hMenu, iSize, MF_CHECKED) ; InvalidateRect (hwnd, NULL, TRUE) ; break ; }

return 0 ;

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

switch (iSize) {

case IDM_BIG:

StretchBlt (hdc, 0, 0, cxClient, cyClient,

hdcMem, 0, 0, cxBitmap, cyBitmap, SRCCOPY) ; break ;

case IDM_SMALL:

for (y = 0 ; y < cyClient ; y += cyBitmap) for (x = 0 ; x < cxClient ; x += cxBitmap) {

BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY) ; }

break ; }

EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

DeleteDC (hdcMem) ;

DeleteObject (hBitmap) ; PostQuitMessage (0) ;

return 0 ; }

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

chap14-HelloBití·???t //{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file. // Used by HelloBit.rc //

#define IDM_BIG 40001 #define IDM_SMALL 40002

// Next default values for new objects //

#ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40003 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif

chap14-Scramble

/*------------------------------------------------ SCRAMBLE.C -- Scramble (and Unscramble) Screen (c) Charles Petzold, 1998

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

#include

#define NUM 300

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static int iKeep [NUM][4] ; HDC hdcScr, hdcMem ; int cx, cy ; HBITMAP hBitmap ; HWND hwnd ;

int i, j, x1, y1, x2, y2 ;

if (LockWindowUpdate (hwnd = GetDesktopWindow ())) {

hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;

hdcMem = CreateCompatibleDC (hdcScr) ;

cx = GetSystemMetrics (SM_CXSCREEN) / 10 ; cy = GetSystemMetrics (SM_CYSCREEN) / 10 ;

hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;

SelectObject (hdcMem, hBitmap) ;

srand ((int) GetCurrentTime ()) ;

for (i = 0 ; i < 2 ; i++) for (j = 0 ; j < NUM ; j++) {

if (i == 0) {

iKeep [j] [0] = x1 = cx * (rand () % 10) ; iKeep [j] [1] = y1 = cy * (rand () % 10) ; iKeep [j] [2] = x2 = cx * (rand () % 10) ; iKeep [j] [3] = y2 = cy * (rand () % 10) ; } else {

x1 = iKeep [NUM - 1 - j] [0] ; y1 = iKeep [NUM - 1 - j] [1] ; x2 = iKeep [NUM - 1 - j] [2] ; y2 = iKeep [NUM - 1 - j] [3] ; }

BitBlt (hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY) ; BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ; BitBlt (hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ;

Sleep (10) ; }

DeleteDC (hdcMem) ;

ReleaseDC (hwnd, hdcScr) ; DeleteObject (hBitmap) ;

LockWindowUpdate (NULL) ;

}

return FALSE ; }

chap14-Sketch

/*----------------------------------------- SKETCH.C -- Shadow Bitmap Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, NULL, hInstance, NULL) ;

if (hwnd == NULL) {

MessageBox (NULL, TEXT (\enough memory to create bitmap!\

szAppName, MB_ICONERROR) ; return 0 ; }

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

void GetLargestDisplayMode (int * pcxBitmap, int * pcyBitmap) {

DEVMODE devmode ;

int iModeNum = 0 ;

* pcxBitmap = * pcyBitmap = 0 ;

ZeroMemory (&devmode, sizeof (DEVMODE)) ; devmode.dmSize = sizeof (DEVMODE) ;

while (EnumDisplaySettings (NULL, iModeNum++, &devmode)) {

* pcxBitmap = max (* pcxBitmap, (int) devmode.dmPelsWidth) ; * pcyBitmap = max (* pcyBitmap, (int) devmode.dmPelsHeight) ; } }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static BOOL fLeftButtonDown, fRightButtonDown ; static HBITMAP hBitmap ; static HDC hdcMem ;

static int cxBitmap, cyBitmap, cxClient, cyClient, xMouse, yMouse ;

HDC hdc ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

GetLargestDisplayMode (&cxBitmap, &cyBitmap) ;

hdc = GetDC (hwnd) ;

hBitmap = CreateCompatibleBitmap (hdc, cxBitmap, cyBitmap) ; hdcMem = CreateCompatibleDC (hdc) ; ReleaseDC (hwnd, hdc) ;

if (!hBitmap) // no memory for bitmap {

DeleteDC (hdcMem) ; return -1 ; }

SelectObject (hdcMem, hBitmap) ;

PatBlt (hdcMem, 0, 0, cxBitmap, cyBitmap, WHITENESS) ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_LBUTTONDOWN:

if (!fRightButtonDown) SetCapture (hwnd) ;

xMouse = LOWORD (lParam) ; yMouse = HIWORD (lParam) ; fLeftButtonDown = TRUE ; return 0 ;

case WM_LBUTTONUP:

if (fLeftButtonDown) SetCapture (NULL) ;

fLeftButtonDown = FALSE ;

return 0 ;

case WM_RBUTTONDOWN:

if (!fLeftButtonDown) SetCapture (hwnd) ;

xMouse = LOWORD (lParam) ; yMouse = HIWORD (lParam) ; fRightButtonDown = TRUE ; return 0 ;

case WM_RBUTTONUP:

if (fRightButtonDown) SetCapture (NULL) ;

fRightButtonDown = FALSE ; return 0 ;

case WM_MOUSEMOVE:

if (!fLeftButtonDown && !fRightButtonDown) return 0 ;

hdc = GetDC (hwnd) ;

SelectObject (hdc,

GetStockObject (fLeftButtonDown ? WHITE_PEN)) ;

SelectObject (hdcMem,

GetStockObject (fLeftButtonDown ? WHITE_PEN)) ;

MoveToEx (hdc, xMouse, yMouse, NULL) ; MoveToEx (hdcMem, xMouse, yMouse, NULL) ;

xMouse = (short) LOWORD (lParam) ; yMouse = (short) HIWORD (lParam) ;

LineTo (hdc, xMouse, yMouse) ; LineTo (hdcMem, xMouse, yMouse) ;

ReleaseDC (hwnd, hdc) ; return 0 ;

BLACK_PEN BLACK_PEN :

:

case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

BitBlt (hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY) ;

EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

DeleteDC (hdcMem) ;

DeleteObject (hBitmap) ; PostQuitMessage (0) ; return 0 ; }

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

chap14-Stretch

/*---------------------------------------- STRETCH.C -- StretchBlt Demonstration (c) Charles Petzold, 1998

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

#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {

static TCHAR szAppName [] = TEXT (\ HWND hwnd ; MSG msg ;

WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_INFORMATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass)) {

MessageBox (NULL, TEXT (\program requires Windows NT!\ szAppName, MB_ICONERROR) ; return 0 ; }

hwnd = CreateWindow (szAppName, TEXT (\ WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ;

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

TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

static int cxClient, cyClient, cxSource, cySource ; HDC hdcClient, hdcWindow ; PAINTSTRUCT ps ;

switch (message) {

case WM_CREATE:

cxSource = GetSystemMetrics (SM_CXSIZEFRAME) + GetSystemMetrics (SM_CXSIZE) ;

cySource = GetSystemMetrics (SM_CYSIZEFRAME) + GetSystemMetrics (SM_CYCAPTION) ; return 0 ;

case WM_SIZE:

cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; return 0 ;

case WM_PAINT:

hdcClient = BeginPaint (hwnd, &ps) ; hdcWindow = GetWindowDC (hwnd) ;

StretchBlt (hdcClient, 0, 0, cxClient, cyClient,

hdcWindow, 0, 0, cxSource, cySource, MERGECOPY) ;

ReleaseDC (hwnd, hdcWindow) ; EndPaint (hwnd, &ps) ; return 0 ;

case WM_DESTROY:

PostQuitMessage (0) ; return 0 ; }

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

本文来源:https://www.bwwdw.com/article/y32p.html

Top