#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;
}
Windows下的动态阿基米德螺线
最新推荐文章于 2021-10-24 10:04:08 发布