/*
* RECT结构练习, by netrookie, ChinaUnix
*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("rect demo");
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 = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
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;
HDC hdc;
PAINTSTRUCT ps;
int i, j;
/*
* rect用于保存下一个的矩形, tmpRect用于保存上一个矩形,
* destRect用于保存上一个与下一个矩形的交集
*/
RECT rect, tmpRect, destRect;
HBRUSH hBrush;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
/*
* 初始化矩形
*/
SetRect(&rect, 0, 0, cxClient / 16, cyClient / 16);
/*
* 画第一个矩形
*/
FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
/*
* 保存上一个的矩形
*/
tmpRect = rect;
/*
* 设置下一次矩形
*/
OffsetRect(&rect, cxClient / 32, cyClient / 32);
for(i = 100; rect.right < cxClient &&
rect.bottom < cyClient; i += 5) {
/*
* 创建画刷, 用于FillRect函数
*/
hBrush = CreateSolidBrush(RGB(0, i, i));
/*
* 求上一个矩形与下一个矩形的交集, 保存于destRect
*/
IntersectRect(&destRect, &tmpRect, &rect);
/*
* 填充上一个与下一个矩形的交集
*/
FillRect(hdc, &destRect, hBrush);
/*
* 重复
*/
FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
tmpRect = rect;
OffsetRect(&rect, cxClient / 32, cyClient / 32);
DeleteObject(hBrush);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|