#include<windows.h>
#include<math.h>
#include<string.h>
#include<stdio.h>
const double PI = 3.1415926;
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = "我的窗口";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass); //注册窗口类
HWND hwnd;
hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW,
0, 0, 640, 480, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
HDC hdc;
PAINTSTRUCT ps;
HPEN hpen;
HBRUSH hbrush;
int nlength = 100, nwidth = 40;
static int j = 0;
switch(uMsg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
hpen = (HPEN)GetStockObject(BLACK_PEN);
hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
MoveToEx(hdc, 300 , 200, NULL);
int k, nxl, nyl, nxw, nyw;
for(k=0; k<4; k++)
{
nxl = 300 + nwidth * cos(j/5+(90-90*k)*PI/180);
nyl = 200 - nwidth * sin(j/5+(90-90*k)*PI/180);
nxw = 300 + nlength * cos(j/5+(-90*k)*PI/180);
nyw = 200 - nlength * sin(j/5+(-90*k)*PI/180);
POINT point[3];
point[0].x = nxl; point[0].y = nyl;
point[1].x = nxw; point[1].y = nyw;
point[2].x = 300; point[2].y = 200;
hbrush = CreateSolidBrush(RGB(25+50*k, 240-40*k, 45+60*k));
SelectObject(hdc, hpen);
SelectObject(hdc, hbrush);
Polygon(hdc, point, 3);
LineTo(hdc, nxl, nyl);
LineTo(hdc, nxw, nyw);
LineTo(hdc, 300, 200);
}
j += 5;
Sleep(500);
InvalidateRect(hwnd, NULL, 1);//重绘窗口区域
EndPaint(hwnd, &ps);
DeleteObject(hpen);
DeleteObject(hbrush);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
总结:
要实现风车转动关键有两点:
1.InvalidateRect(hwnd, NULL, 1)//重绘窗口区域;
2.一个静态局部变量j,不断的改变风车顶点的坐标;