vc中实现所画风车的旋转,我用vc++6.0 cpp画了一个风车 怎么才能让它能旋转起来
推荐于2016-03-14
展开全部
//源程序,示例代码:
// Instance_3_1_.cpp : Defines the entry point for the application.
//
/*************************************************************************
在窗口中画一个旋转的风车,风车中有三个叶片,颜色分别为红黄和蓝,
叶片外侧有一个外接圆。
*************************************************************************/
#include <windows.h>
#include <math.h>
// 回调函数声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
// 初始化窗口类声明
BOOL InitWindowsClass(HINSTANCE hInstance, char *lpszClassName);
// 初始化窗口声明
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow, char *lpszClassName, char *lpTitle);
WNDCLASS wndclass; // 定义一个窗口类
HWND hwnd; // 定义一个窗口句柄
const double Pi = 3.1415926;
int nMaxNumber = 20; // 叶片循环一周中绘图的次数
int nNum = 0; // 记录当前的顺序
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG Msg; // 定义消息
char lpszClassName[] = "风车"; // 窗口的类名
char lpTitle[] = "基本绘图-旋转的风车"; // 窗口标题名
// 初始化窗口类
if (!InitWindowsClass(hInstance, lpszClassName))
{
return FALSE;
}
// 初始化窗口
if (!InitWindows(hInstance, nCmdShow, lpszClassName, lpTitle))
{
return FALSE;
}
//消息循环
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam; // 程序终止时将信息返回系统
}
// 初始化窗口类定义
BOOL InitWindowsClass(HINSTANCE hInstance, char *lpszClassName)
{
//1、窗口类定义
wndclass.style = 0; // 窗口类型为默认类型
wndclass.lpfnWndProc = WndProc; // 窗口处理函数为 WNDPROC
wndclass.cbClsExtra = 0; // 窗口类无扩展
wndclass.cbWndExtra = 0; // 窗口实例无扩展
wndclass.hInstance = hInstance; // 当前实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 窗口的最小化图标为默认图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // 窗口采用箭头光标
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // 窗口采用白色背景
wndclass.lpszMenuName = NULL; // 窗口中无菜单
wndclass.lpszClassName = lpszClassName; //类名为 lpClassName
//2、注册窗口类
if (!RegisterClass(&wndclass))
{ // 如果注册失败则发出警告声音
MessageBeep(0);
return FALSE;
}
return TRUE;
}
// 初始化窗口声明
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow, char *lpszClassName, char *lpTitle)
{
//3、创建窗口
hwnd = CreateWindow(
lpszClassName,
lpTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
600,
450,
NULL,
NULL,
hInstance,
NULL
);
//4、显示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
// 回调函数定义
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC; // 定义设备环境句柄
HPEN hPen; // 定义画笔句柄
HBRUSH hBrush; // 定义画刷句柄
PAINTSTRUCT PtStr; // 定义包含绘制信息的结构体变量
POINT pCenterPoint; // 定义一个圆尽心点的坐标
int nRadious = 50;// 定义圆的半径
double fAngle; // 叶片的直边与水平轴的夹角
switch(message)
{
case WM_PAINT:
{ // 处理绘图消息
hDC = BeginPaint(hwnd, &PtStr); // 得到设备句柄
SetMapMode(hDC, MM_ANISOTROPIC); // 设置映像模式
SetWindowExtEx(hDC, 400, 300, NULL); // 设置窗口区域(逻辑单位)
SetViewportExtEx(hDC, 600, 450, NULL); // 设置视口区域(物理单位)
SetViewportOrgEx(hDC, 300, 200, NULL); // 设置视口原点坐标为(300, 200)
// 绘制外圆
hPen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDC, hPen);
Ellipse(hDC, -100, -100, 100, 100);
// 绘制风车的叶片
// 1、画红色叶片
hBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(hDC, hBrush);
fAngle = 2 * Pi / nMaxNumber * nNum;
pCenterPoint.x = (int)(nRadious * cos(fAngle));
pCenterPoint.y = (int)(nRadious * sin(fAngle));
Pie(
hDC,
pCenterPoint.x - nRadious, pCenterPoint.y - nRadious,
pCenterPoint.x + nRadious, pCenterPoint.y + nRadious,
(int)(pCenterPoint.x + nRadious * cos(fAngle)),
(int)(pCenterPoint.y + nRadious * sin(fAngle)),
(int)(pCenterPoint.x + nRadious * cos(fAngle + Pi)),
(int)(pCenterPoint.y + nRadious * sin(fAngle + Pi))
);
// 2、画天蓝色叶片
hBrush = CreateSolidBrush(RGB(255, 255, 0));
SelectObject(hDC, hBrush);
pCenterPoint.x = (int)(nRadious * cos(fAngle + 2 * Pi / 3));
pCenterPoint.y = (int)(nRadious * sin(fAngle + 2 * Pi / 3));
Pie(
hDC,
pCenterPoint.x - nRadious, pCenterPoint.y - nRadious,
pCenterPoint.x + nRadious, pCenterPoint.y + nRadious,
(int)(pCenterPoint.x + nRadious * cos(fAngle + 2 * Pi / 3)),
(int)(pCenterPoint.y + nRadious * sin(fAngle + 2 * Pi / 3)),
(int)(pCenterPoint.x + nRadious * cos(fAngle + Pi + 2 * Pi / 3)),
(int)(pCenterPoint.y + nRadious * sin(fAngle + Pi + 2 * Pi / 3))
);
// 2、画黄色叶片
hBrush = CreateSolidBrush(RGB(0, 255, 255));
SelectObject(hDC, hBrush);
pCenterPoint.x = (int)(nRadious * cos(fAngle + 4 * Pi / 3));
pCenterPoint.y = (int)(nRadious * sin(fAngle + 4 * Pi / 3));
Pie(
hDC,
pCenterPoint.x - nRadious, pCenterPoint.y - nRadious,
pCenterPoint.x + nRadious, pCenterPoint.y + nRadious,
(int)(pCenterPoint.x + nRadious * cos(fAngle + 4 * Pi / 3)),
(int)(pCenterPoint.y + nRadious * sin(fAngle + 4 * Pi / 3)),
(int)(pCenterPoint.x + nRadious * cos(fAngle + Pi + 4 * Pi / 3)),
(int)(pCenterPoint.y + nRadious * sin(fAngle + Pi + 4 * Pi / 3))
);
nNum++; // 当前充数增加1
Sleep(50); //等待0.1秒
InvalidateRect(hwnd, NULL, 1); // 重绘窗口区域
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hwnd, &PtStr);
break;
}
case WM_DESTROY:
{
// 调用 PostQuitMessage 发出 WM_QUIT 消息
PostQuitMessage(0);
}
default:
{
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询