#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
static TCHAR szAppName[] = TEXT("XZZ");
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 = szAppName;
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 cxBitmap, cyBitmap, cxClient, cyClient, sign = 1,
sign1 = 1;
static HBITMAP hBitmap;
static HDC hdcMem;
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
static POINT pos;
switch(message) {
case WM_CREATE:
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
// 创建一个宽与高是128像素的矩形位图
hBitmap = CreateCompatibleBitmap(hdc, 128, 128);
SelectObject(hdcMem, hBitmap);
// 用白色填充整个位图区域,我发现默认颜色是黑色。
Rectangle(hdcMem, -1, -1, 130, 130);
hBrush = CreateSolidBrush(RGB(0, 255, 0));
SelectObject(hdcMem, hBrush);
// 在位图区域中央画一个圆形,半径为32
Ellipse(hdcMem, 32, 32, 96, 96);
SetTimer(hwnd, 1, 50, NULL);
DeleteObject(hBrush);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
pos.x = cxClient / 2;
pos.y = cyClient / 2;
return 0;
case WM_TIMER:
hdc = GetDC(hwnd);
// 使位图中心与客户区域中心重叠在一起
BitBlt(hdc, pos.x - 64, pos.y - 64, 128,
128, hdcMem, 0, 0, SRCCOPY);
pos.x += 16 * sign;
pos.y += 16 * sign1;
if(pos.x + 32 >= cxClient || pos.x - 32 <= 0)
sign = -sign;
if(pos.y + 32 >= cyClient || pos.y - 32 <= 0)
sign1 = -sign1;
return 0;
case WM_DESTROY:
if(hBitmap)
DeleteObject(hBitmap);
DeleteDC(hdcMem);
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|