windowss编程之旋转风车

本文介绍了一个使用Windows API在窗口中绘制并实现风车旋转动画的示例程序。通过不断更新风车顶点坐标及定期重绘窗口,实现了动态效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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,不断的改变风车顶点的坐标;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值