"旋转的风车"----windows(GDI)绘图
这正是秋季将尽, 冬季未到的时节。 黄土高坡上已滚起了漫天黄沙, 而这里却是万里晴空如练! 风, 丝丝入骨! 未央柳即将枯死的枝条,仍在挣扎。 街道两旁清一色的银杏树叶, 金灿耀眼。 耀的令人感动, 感动的令人心碎! 很显然,这又是一个悲伤的人, 和一个悲伤的故事。
C艹老师给我们布置了一道作业, 就是 “旋转的风车”,卧槽! 我还几乎完全没有接触过 windows编程啊。我和室友“执手相看泪眼,竟无语凝噎!”。 无奈只有自己强势看textbook啦,然而,,,,这本书也是精炼的可以,,,,,由于是我的一位老师编写的, 我不做过多评论! 。看着书上仅有的两个示例, 不觉我已泪流满面, 无奈, 只有强行乱写一下啦! 心疼~~~~。

1 /* *********************************************** 2 Author : Cao Tan Xiao Ke 3 Language : C++ 4 project : The windmills 5 ************************************************ */ 6 7 // caoTanXiaoKe.cpp : Define the application's entry point。 8 // 9 10 #include "stdafx.h" 11 #include "caoTanXiaoKe.h" 12 #include <windows.h> 13 #include <cstdlib> 14 #include <cstring> 15 #include <cmath> 16 /************************************************************************************* 17 some constants 18 19 **************************************************************************************/ 20 #define Pi 3.1415626 21 const int cir = 81; 22 const double angle = 0.2; 23 int T = 0; 24 POINT nodes[105], nodes2[105], nodes3[105]; // define the coordinates of the stiangle 25 26 /*************************************************************************************** 27 make a window 28 29 ****************************************************************************************/ 30 long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam); 31 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR IpCmdLine, int nCmdShow) 32 { 33 MSG Message; 34 HWND hWnd; 35 WNDCLASS WndClass; 36 WndClass.cbClsExtra = 0; 37 WndClass.cbWndExtra = 0; 38 WndClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); //white background 39 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 40 WndClass.hIcon = LoadIcon(NULL,"END"); 41 WndClass.hInstance = hInstance; 42 WndClass.lpfnWndProc = WndProc; 43 WndClass.lpszClassName="SPin"; 44 WndClass.lpszMenuName = NULL; 45 WndClass.style = 0; 46 if(!RegisterClass(&WndClass)) 47 { 48 MessageBeep(0); 49 return FALSE; 50 } 51 hWnd = CreateWindow(("SPin"), //create a window 52 ("旋转的风车"), 53 WS_OVERLAPPEDWINDOW, 54 CW_USEDEFAULT, 55 0, 56 CW_USEDEFAULT, 57 0, 58 NULL, 59 NULL, 60 hInstance, 61 NULL); 62 ShowWindow(hWnd, nCmdShow); // show in window 63 UpdateWindow(hWnd); // Update and map the user area 64 65 /*************************************************************************** 66 the points of the pictures 67 ***************************************************************************/ 68 69 nodes[0].x = 0; 70 nodes[0].y = 0; 71 nodes[1].x = 140; 72 nodes[1].y = 75; 73 nodes[2].x = 250; 74 nodes[2].y = 80; 75 76 for(int i=3; i<=100; i++) 77 { 78 nodes[i].x = (long)(nodes[i-3].x*cos(2.0*Pi/3)+nodes[i-3].y*sin(2.0*Pi/3)); 79 nodes[i].y = (long)(nodes[i-3].y*cos(2.0*Pi/3)-nodes[i-3].x*sin(2.0*Pi/3)); 80 } 81 82 for(int i=0; i<=8; i++) 83 { 84 nodes2[i].x = (long)(nodes[i].x*cos(angle)+nodes[i].y*sin(angle)); 85 nodes2[i].y = (long)(nodes[i].y*cos(angle)-nodes[i].x*sin(angle)); 86 nodes3[i].x = (long)(nodes[i].x*cos(angle)+nodes[i].y*sin(angle))+550; 87 nodes3[i].y = (long)(nodes[i].y*cos(angle)-nodes[i].x*sin(angle)); 88 } 89 90 91 for(int j=9; j<=100; j+=9) 92 { 93 for(int i=0; i<=8; i++) 94 { 95 nodes[i].x = (long)(nodes[i].x*cos(angle)+nodes[i].y*sin(angle)*(1.25)); 96 nodes[i].y = (long)(nodes[i].y*cos(angle)-nodes[i].x*sin(angle)*(1.25)); 97 nodes2[j+i].x = nodes[i].x; 98 nodes2[j+i].y = nodes[i].y; 99 nodes3[j+i].x = nodes[i].x+550; 100 nodes3[j+i].y = nodes[i].y; 101 } 102 } 103 104 105 106 /************************************************************************** 107 a part of window 108 ***************************************************************************/ 109 110 while(GetMessage(&Message, 0, 0, 0)) //消息循环 111 { 112 TranslateMessage(&Message); 113 DispatchMessage(&Message); 114 } 115 116 return Message.wParam; 117 } 118 119 120 long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam) //消息处理函数 121 { 122 HDC hDC; // 定义指向设备描述表的句柄 123 HBRUSH hBrush; // 定义指向画刷的句柄 124 HPEN hPen; // 定义指向画笔的句柄 125 PAINTSTRUCT PtStr; // 定义指向包含绘图信息的结构体变量 126 switch(iMessage) // 处理变量 127 { 128 case WM_PAINT: // 处理绘图消息 129 hDC = BeginPaint(hWnd, &PtStr); // 获得设备环境指针 130 SetWindowOrgEx(hDC, -300, -250, NULL); //设置原点坐标为(-300, -250) 131 hPen=CreatePen(PS_INSIDEFRAME, 1, RGB(0, 0, 0)); 132 SelectObject(hDC, hPen); // 选择画笔 133 /********************************************************************* 134 draw your pictures 135 ***********************************************************************/ 136 Rectangle(hDC, -5, -25, 5, 500); 137 Ellipse(hDC, -10, -10, 10, 10); 138 139 Rectangle(hDC, 545, -25, 555, 500); 140 Ellipse(hDC, 540, -10, 560, 10); 141 142 hPen=CreatePen(PS_INSIDEFRAME, 1, RGB(255, 0, 0)); // 创建红色画笔 143 hBrush=CreateSolidBrush(RGB(225, 0, 0)); //创建红色画刷 144 SelectObject(hDC, hBrush); // 选入画刷 145 SelectObject(hDC, hPen); // 选入画笔 146 Polygon(hDC, nodes2+T*3%cir, 3); // 147 hBrush=CreateSolidBrush(RGB(0, 0, 0)); // 148 SelectObject(hDC, hBrush); // 选入画刷 149 Polygon(hDC, nodes3+18+T*3%cir, 3); 150 151 152 hPen=CreatePen(PS_INSIDEFRAME, 1, RGB(0, 0, 255)); // 创建绿色画笔 153 hBrush=CreateSolidBrush(RGB(0, 255, 0)); //创建绿色画刷 154 SelectObject(hDC, hBrush); // 选入画刷 155 SelectObject(hDC, hPen); // 选入画笔 156 Polygon(hDC, nodes2+3+T*3%cir, 3); // 157 hBrush=CreateSolidBrush(RGB(255, 255, 33)); 158 SelectObject(hDC, hBrush); // 选入画刷 159 Polygon(hDC, nodes3+3+18+T*3%cir, 3); 160 161 hPen=CreatePen(PS_INSIDEFRAME, 1, RGB(0, 0, 255)); // 创建蓝色画笔 162 hBrush=CreateSolidBrush(RGB(0, 0, 255)); //创建蓝色画刷 163 SelectObject(hDC, hBrush); // 选入画刷 164 SelectObject(hDC, hPen); // 选入画笔 165 Polygon(hDC, nodes2+6+T*3%cir, 3); 166 hBrush=CreateSolidBrush(RGB(255, 99, 255)); //创建红色画刷 167 SelectObject(hDC, hBrush); // 选入画刷 168 Polygon(hDC, nodes3+6+18+T*3%cir, 3); 169 170 171 172 T++; 173 DeleteObject(hPen); // 删除画笔 174 DeleteObject(hBrush); // 删除画刷 175 EndPaint(hWnd, &PtStr); // 删除设备环境指针 176 if(T<5) 177 Sleep(100); // ----------------> 延时函数 178 else if(T<80) 179 Sleep(100-T); 180 else if(T<850) 181 Sleep(10); 182 else if(T<=1000) 183 Sleep(T-840); 184 if(T<=1000) InvalidateRect(hWnd, NULL, 1); 185 return 0; 186 case WM_DESTROY: 187 PostQuitMessage(0); 188 return 0; 189 default: 190 return(DefWindowProc(hWnd, iMessage, wParam, lParam)); 191 } 192 }
小技巧: 旋转矩阵, 数组存点, 取模循环。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步