hdc = GetDC (hwnd) ;<br />
hdcMem = CreateCompatibleDC (hdc) ;<br />
hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ;<br />
ReleaseDC (hwnd, hdc) ;<br />
<br />
SelectObject (hdcMem, hBitmap) ;<br />
Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ;<br />
<br />
hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ;<br />
SelectObject (hdcMem, hBrush) ;<br />
SetBkColor (hdcMem, RGB (255, 0, 255)) ;<br />
Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove) ;
================
The hwnd has a white background color, I wonder how's the preceding codes turn out to be a cyTotal * cxTotal rectangular(white bkground) and within which is a centered circle(pink bkground) painted with hatched brush. How's the code " Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) " play a part? Could anybody give me a explanations in details
The following is the complete source code
#include <windows.h><br />
#define ID_TIMER 1<br />
<br />
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;<br />
<br />
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,<br />
PSTR szCmdLine, int iCmdShow)<br />
{<br />
static TCHAR szAppName[] = TEXT ("Bounce") ;<br />
HWND hwnd ;<br />
MSG msg ;<br />
WNDCLASS wndclass ;<br />
<br />
wndclass.style = CS_HREDRAW | CS_VREDRAW ;<br />
wndclass.lpfnWndProc = WndProc ;<br />
wndclass.cbClsExtra = 0 ;<br />
wndclass.cbWndExtra = 0 ;<br />
wndclass.hInstance = hInstance ;<br />
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;<br />
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;<br />
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;<br />
wndclass.lpszMenuName = NULL ;<br />
wndclass.lpszClassName = szAppName ;<br />
<br />
if (!RegisterClass (&wndclass))<br />
{<br />
MessageBox (NULL, TEXT ("This program requires Windows NT!"),<br />
szAppName, MB_ICONERROR) ;<br />
return 0 ;<br />
}<br />
<br />
hwnd = CreateWindow (szAppName, TEXT ("Bouncing Ball"),<br />
WS_OVERLAPPEDWINDOW,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
NULL, NULL, hInstance, NULL) ;<br />
<br />
ShowWindow (hwnd, iCmdShow) ;<br />
UpdateWindow (hwnd) ;<br />
<br />
<br />
while (GetMessage (&msg, NULL, 0, 0))<br />
{<br />
TranslateMessage (&msg) ;<br />
DispatchMessage (&msg) ;<br />
}<br />
return msg.wParam ;<br />
}<br />
<br />
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)<br />
{<br />
static HBITMAP hBitmap ;<br />
static int cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal,<br />
cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel ;<br />
HBRUSH hBrush ;<br />
HDC hdc, hdcMem ;<br />
int iScale ;<br />
<br />
switch (iMsg)<br />
{<br />
case WM_CREATE:<br />
hdc = GetDC (hwnd) ;<br />
xPixel = 1 ;<br />
yPixel = 1 ;<br />
ReleaseDC (hwnd, hdc) ;<br />
<br />
SetTimer (hwnd, ID_TIMER, 50, NULL) ;<br />
return 0 ;<br />
<br />
case WM_SIZE:<br />
xCenter = (cxClient = LOWORD (lParam)) / 2 ;<br />
yCenter = (cyClient = HIWORD (lParam)) / 2 ;<br />
<br />
iScale = min (cxClient, cyClient) / 16 ;<br />
<br />
cxRadius = iScale;<br />
cyRadius = iScale;<br />
<br />
cxMove = max (1, cxRadius / 2) ;<br />
cyMove = max (1, cyRadius / 2) ;<br />
<br />
cxTotal = 2 * (cxRadius + cxMove) ;<br />
cyTotal = 2 * (cyRadius + cyMove) ;<br />
<br />
if (hBitmap)<br />
DeleteObject (hBitmap) ;<br />
hdc = GetDC (hwnd) ;<br />
hdcMem = CreateCompatibleDC (hdc) ;<br />
hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ;<br />
ReleaseDC (hwnd, hdc) ;<br />
<br />
SelectObject (hdcMem, hBitmap) ;<br />
Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ;<br />
<br />
hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ;<br />
SelectObject (hdcMem, hBrush) ;<br />
SetBkColor (hdcMem, RGB (255, 0, 255)) ;<br />
Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove) ;<br />
DeleteDC (hdcMem) ;<br />
DeleteObject (hBrush) ;<br />
return 0 ;<br />
<br />
case WM_TIMER:<br />
if (!hBitmap)<br />
break ;<br />
<br />
hdc = GetDC (hwnd) ;<br />
hdcMem = CreateCompatibleDC (hdc) ;<br />
SelectObject (hdcMem, hBitmap) ;<br />
<br />
BitBlt (hdc, xCenter - cxTotal / 2,<br />
yCenter - cyTotal / 2, cxTotal, cyTotal,<br />
hdcMem, 0, 0, SRCCOPY) ;<br />
<br />
ReleaseDC (hwnd, hdc) ;<br />
DeleteDC (hdcMem) ;<br />
<br />
xCenter += cxMove ;<br />
yCenter += cyMove ;<br />
<br />
if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0))<br />
cxMove = -cxMove ;<br />
<br />
if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0))<br />
cyMove = -cyMove ;<br />
<br />
return 0 ;<br />
<br />
case WM_DESTROY:<br />
if (hBitmap)<br />
DeleteObject (hBitmap) ;<br />
<br />
KillTimer (hwnd, ID_TIMER) ;<br />
PostQuitMessage (0) ;<br />
return 0 ;<br />
}<br />
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;<br />
}
|