Windows下的动态阿基米德螺线

#include<Windows.h>
#include<tchar.h>
#include<math.h>  
BOOLEAN InitWindowClass(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
	MSG msg;
	if(!InitWindowClass(hInstance,nCmdShow)){
		MessageBox(NULL,L"创建窗口失败!",_T("创建窗口"),NULL);
		return 1;
	}
	while(GetMessage(&msg,NULL,0,0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return(int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
	HDC hDC;
	PAINTSTRUCT ps;
	HBRUSH hBrush;
	RECT clientRect;
	int xOrg,yOrg;
	double r = 0.0;
	double sita = 0.0;
	double r_step = 1.0;   //射线长度渐变步长
	double sita_step = 1.0;   //射线角度渐变步长
	int point_x;   
	int point_y;   //先算出螺线上每个点的纵横坐标
	switch(message){
	case WM_PAINT: 
		hDC = BeginPaint(hWnd,&ps);
		GetClientRect(hWnd,&clientRect);
		hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
		SelectObject(hDC,hBrush);
		xOrg = (clientRect.left + clientRect.right) / 2;  //螺线中心x坐标
		yOrg = (clientRect.top + clientRect.bottom) / 2;  //螺线中心y坐标
		/*阿基米德螺线计算公式:
		x = xOrg + (r + r_step) * cos(sita + sita_step)
		y = yOrg + (r + r_step) * sin(sita + sita_step)
		//*/
		for(double j = 0.0; j < 20000.0; j = j + 30.0){
		for(double i = 0.0; i < 1000.0; i = i + 10.0){
		point_x = xOrg + (int)((r + r_step * i) * sin((sita + sita_step * i + j) * 3.1415926 / 180));
		point_y = yOrg - (int)((r + r_step * i) * cos((sita + sita_step * i + j) * 3.1415926 / 180));//一定要注意强制转换的位置,否则可能出现零项!
		Ellipse(hDC,point_x - 10,point_y - 10,point_x + 10,point_y + 10);
		}
		DeleteObject(hBrush);
		hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
		GetClientRect(hWnd,&clientRect);
		FillRect(hDC,&clientRect,hBrush);
		Sleep(100);
		}
		DeleteObject(hBrush);
		EndPaint(hWnd,&ps);
		break;
	case WM_SIZE:
		InvalidateRect(hWnd,NULL,true);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd,message,wParam,lParam);
		break;
	}
	return 0;
}
BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow){
	WNDCLASSEX wcex;
	HWND hWnd;
	TCHAR szWindowClass[] = L"窗口示例";
	TCHAR szTitle[] = L"阿基米德螺线";
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = 0;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
	if(!RegisterClassEx(&wcex))
		return FALSE;
	hWnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW,
		250,0,       //调节窗口看这里:窗口左上角坐标
		800,730,     //调节窗口看这里:窗口右下角坐标
		NULL,
		NULL,
		hInstance,
		NULL
		);
	if(!hWnd)
		return FALSE;
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值