Win32 三色旋转风车

来源:互联网 发布:知乎金融行业现状 编辑:程序博客网 时间:2024/04/29 04:17
// Fly.cpp : 定义应用程序的入口点。//#include "stdafx.h"#include "Fly.h"#define MAX_LOADSTRING 100#include <Windows.h>#include <tchar.h>#include <math.h>long WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);// 全局变量:HINSTANCE hInst;// 当前实例TCHAR szTitle[MAX_LOADSTRING];// 标题栏文本TCHAR szWindowClass[MAX_LOADSTRING];// 主窗口类名#define PI 3.1415926int nNum=0,nMaxNum=20;// 此代码模块中包含的函数的前向声明:ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。MSG msg;HACCEL hAccelTable;// 初始化全局字符串LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_FLY, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// 执行应用程序初始化:if (!InitInstance (hInstance, nCmdShow)){return FALSE;}hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FLY));// 主消息循环:while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}return (int) msg.wParam;}////  函数: MyRegisterClass()////  目的: 注册窗口类。////  注释:////    仅当希望//    此代码与添加到 Windows 95 中的“RegisterClassEx”//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,//    这样应用程序就可以获得关联的//    “格式正确的”小图标。//ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FLY));wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_FLY);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));return RegisterClassEx(&wcex);}////   函数: InitInstance(HINSTANCE, int)////   目的: 保存实例句柄并创建主窗口////   注释:////        在此函数中,我们在全局变量中保存实例句柄并//        创建和显示主程序窗口。//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){   HWND hWnd;   hInst = hInstance; // 将实例句柄存储在全局变量中   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,      0, 0, 400, 400, NULL, NULL, hInstance, NULL);   if (!hWnd)   {      return FALSE;   }   ShowWindow(hWnd, nCmdShow);   UpdateWindow(hWnd);   return TRUE;}////  函数: WndProc(HWND, UINT, WPARAM, LPARAM)////  目的: 处理主窗口的消息。////  WM_COMMAND- 处理应用程序菜单//  WM_PAINT- 绘制主窗口//  WM_DESTROY- 发送退出消息并返回long WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc;    HBRUSH hBrush;    HPEN hp;    PAINTSTRUCT ps;    int nCenterX,nCenterY;static int cxClient,cyClient;    double fAngle;double radius = 50;switch(message){ case WM_SIZE:cxClient = LOWORD(lParam);cyClient = HIWORD(lParam);break;case WM_CREATE:case WM_PAINT:hdc=BeginPaint(hWnd,&ps);        SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);//设置原点为客户区中点//        绘制外圆        hp=CreatePen(PS_SOLID,1,RGB(255,0,255));        SelectObject(hdc,hp);        Ellipse(hdc,-100,-100,100,100);//        绘制风车的叶片//        绘制红色的叶片        hBrush=CreateSolidBrush(RGB(255,0,0));        SelectObject(hdc,hBrush);        fAngle=2*PI/nMaxNum*nNum;        nCenterX=(int)(radius*cos(fAngle));        nCenterY=(int)(radius*sin(fAngle));        Pie(hdc,nCenterX-radius,nCenterY-radius,nCenterX+radius,nCenterY+radius,0,0,(int)(nCenterX+radius*cos(fAngle)),(int)(nCenterY+radius*sin(fAngle)));//        绘制蓝色的叶片        hBrush=CreateSolidBrush(RGB(255,255,0));        SelectObject(hdc,hBrush);        nCenterX=(int)(radius*cos(fAngle+2*PI/3));        nCenterY=(int)(radius*sin(fAngle+2*PI/3));        Pie(hdc,nCenterX-radius,nCenterY-radius,nCenterX+radius,nCenterY+radius,0,0,(int)(nCenterX+radius*cos(fAngle+2*PI/3)),(int)(nCenterY+radius*sin(fAngle+2*PI/3)));        //        绘制黄色的叶片        hBrush=CreateSolidBrush(RGB(0,255,255));        SelectObject(hdc,hBrush);        nCenterX=(int)(radius*cos(fAngle+4*PI/3));        nCenterY=(int)(radius*sin(fAngle+4*PI/3));        Pie(hdc,nCenterX-radius,nCenterY-radius,nCenterX+radius,nCenterY+radius,0,0,(int)(nCenterX+radius*cos(fAngle+4*PI/3)),(int)(nCenterY+radius*sin(fAngle+4*PI/3)));        nNum++;        Sleep(100);        InvalidateRect(hWnd,NULL,1);        EndPaint(hWnd,&ps);        break;case WM_CLOSE:DestroyWindow(hWnd);break;case WM_DESTROY:PostQuitMessage(0);break; default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;} // “关于”框的消息处理程序。INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE;}

0 0
原创粉丝点击