用C语言怎样写五角星沿正弦曲线移动,个人基本不会C语言,求高手解答.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/24 03:47:21
用C语言怎样写五角星沿正弦曲线移动,个人基本不会C语言,求高手解答.
用C语言怎样写五角星沿正弦曲线移动,个人基本不会C语言,求高手解答.
用C语言怎样写五角星沿正弦曲线移动,个人基本不会C语言,求高手解答.
编译环境vc6.0,文件扩展名是cpp.
#include
#include
#include
long WINAPI WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
UINT SCREEN_WIDTH = 1000;
UINT SCREEN_HEIGHT = 600;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
\x05TCHAR className[] = _T("serenesunny");
\x05TCHAR windowName[] = _T("五角星");
\x05WNDCLASS wndcls;
\x05wndcls.cbClsExtra = 0;
\x05wndcls.cbWndExtra = 0;
\x05wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
\x05wndcls.hCursor = LoadCursor(hInstance,IDC_ARROW);
\x05wndcls.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
\x05wndcls.hInstance = hInstance;
\x05wndcls.lpfnWndProc = WndProc;
\x05wndcls.lpszClassName = className;
\x05wndcls.lpszMenuName = 0;
\x05wndcls.style = CS_HREDRAW | CS_VREDRAW;
\x05RegisterClass(&wndcls);
\x05HWND hWnd = CreateWindow(className,windowName,WS_OVERLAPPEDWINDOW,0,
\x05\x050,SCREEN_WIDTH,SCREEN_HEIGHT,0,0,hInstance,0);
\x05ShowWindow(hWnd,SW_SHOWNORMAL);
\x05UpdateWindow(hWnd);
\x05MSG msg;
\x05while (GetMessage(&msg,hWnd,0,0))
\x05{
\x05\x05TranslateMessage(&msg);
\x05\x05DispatchMessage(&msg);
\x05}
\x05return msg.wParam;
}
#define Pi 3.1415926
double n = sin(3 * Pi / (double)10) / sin(Pi / (double)10); // 半径之比
int ti = 0; // 旋转变量
UINT index = 0;
UINT r[] = { (UINT)(n * 20),20 };
float theta[] = {(float)(- Pi / 2),(float)(Pi / (float)5 - Pi / 2)};
POINT points[10];
UINT centerx = 100;
UINT centery = SCREEN_HEIGHT / 2;
long WINAPI WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
\x05HDC hdc;
\x05PAINTSTRUCT ps;
\x05HBRUSH hBrush;
//\x05HPEN hPen;
\x05int i;
\x05int index;
\x05switch(message)
\x05{
\x05case WM_CREATE:
\x05case WM_PAINT:
\x05\x05hdc = BeginPaint(hWnd,&ps);
\x05\x05hBrush = CreateSolidBrush(0x00ffff);
\x05\x05SelectObject(hdc,hBrush);
\x05\x05centerx = 100 + ti;
\x05\x05centery = SCREEN_HEIGHT / 2 - (long)(200 * sin(Pi * ti / (double)300));
\x05\x05for (index = 0; index < 5; index++)
\x05\x05{
\x05\x05\x05for (i = 0; i < 2; i++)
\x05\x05\x05{
\x05\x05\x05\x05points[index * 2 + i].x = (long)(centerx + r[i] * cos(theta[i] + index * 2 * Pi / 5));
\x05\x05\x05\x05points[index * 2 + i].y = (long)(centery + r[i] * sin(theta[i] + index * 2 * Pi / 5));
\x05\x05\x05}\x05
\x05\x05}
\x05\x05Polygon(hdc,points,10); // 把五角星看成一个10边形,用多边形画法去画
\x05\x05DeleteObject(hBrush);
\x05\x05ti += 2;
\x05\x05ti = ti % 600;
\x05\x05Sleep(20);
\x05\x05InvalidateRect(hWnd,NULL,1);\x05//重绘窗口区域.
\x05\x05EndPaint(hWnd,&ps);
\x05\x05break;
\x05case WM_CLOSE:
\x05\x05DestroyWindow(hWnd);
\x05\x05break;
\x05case WM_DESTROY:
\x05\x05PostQuitMessage(0);
\x05\x05break;
\x05default:
\x05\x05return DefWindowProc(hWnd,message,wParam,lParam);
\x05}
\x05return 0;
}
如果出现错误:
LIBCD.lib(crt0.obj) :error LNK2001:unresolved external symbol _main
解决方法:
控制台项目要使用windows子系统,而不是console,设置:
[Project] --> [Settings] --> 选择"Link"属性页,
在Project Options中将/subsystem:console改成/subsystem:windows.