64,678
社区成员
发帖
与我相关
我的任务
分享
// win32.cpp : Defines the entry point for the application.
//
#include <windowsx.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HPEN gPen1;
HPEN gPen2;
HBRUSH gBrush;
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = TEXT("Win32");
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(
TEXT("Win32"),
TEXT("Win32"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
gPen1 = CreatePen(PS_SOLID, 1, RGB(198, 198, 198));
gPen2 = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
gBrush = CreateSolidBrush(RGB(198, 198, 198));
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetTimer(hWnd, 1, 10, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DeletePen(gPen1);
DeletePen(gPen2);
DeleteBrush(gBrush);
return (int) msg.wParam;
}
long gx = 100;
long gy = 100;
long gSpeedx = 10;
long gSpeedy = 10;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
{
RECT rectClient;
GetClientRect(hWnd, &rectClient);
hdc = BeginPaint(hWnd, &ps);
SelectBrush(hdc, gBrush);
SelectPen(hdc, gPen1);
Ellipse(hdc, gx, gy, gx + 50, gy + 50);
gx += gSpeedx;
gy += gSpeedy;
if (gx < rectClient.left || gx + 25 > rectClient.right)
{
gSpeedx = -gSpeedx;
break;
}
if (gy < rectClient.top || gy + 25 > rectClient.bottom)
{
gSpeedy = -gSpeedy;
break;
}
SelectPen(hdc, gPen2);
Ellipse(hdc, gx, gy, gx + 50, gy + 50);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND :
{
RECT rect;
hdc = (HDC)wParam;
GetClipBox(hdc, &rect);
FillRect(hdc, &rect, gBrush);
return 0;
}
break;
case WM_TIMER :
{
if (wParam == 1)
{
InvalidateRect(hWnd, NULL, FALSE);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}