用C++实现模拟数字、字母流星雨,其主要用到链表、win32编程基础。
demo实例:
1 // DatarainDemo.cpp : 定义应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "DatarainDemo.h" 6 #include<time.h> 7 #include<stdlib.h> 8 #include<windows.h> 9 #define STRMINLEN 8 10 #define STRMAXLEN 25 11 12 13 14 //双向链表的每一个节点 15 typedef struct MyList 16 { 17 struct MyList *prev; //前一节点 18 struct MyList *pNext; //下一节点 19 char ch; //数据保存 20 } MyList,*pMyList; 21 //处理数据 22 typedef struct MyListColum 23 { 24 MyList *phead; //整个链表的头 25 MyList *pCurent; //链表所在当前的位置 26 MyList *pTemp; //临时变量 27 int ListStrLen; //整个链表的长度 28 int x,y; //链表的位置 29 int iStopTimes; 30 int iMusstopTimes; 31 }StrColum,*pStrColum; 32 33 34 #define MAX_LOADSTRING 100 35 36 // 全局变量: 37 HINSTANCE hInst; // 当前实例 38 WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 39 WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 40 41 // 此代码模块中包含的函数的前向声明: 42 ATOM MyRegisterClass(HINSTANCE hInstance); 43 BOol InitInstance(HINSTANCE,int); 44 LRESulT CALLBACK WndProc(HWND,UINT,WParaM,LParaM); 45 INT_PTR CALLBACK About(HWND,LParaM); 46 47 int APIENTRY wWinMain(_In_ HINSTANCE hInstance, 48 _In_opt_ HINSTANCE hPrevInstance, 49 _In_ LPWSTR lpCmdline, 50 _In_ int nCmdshow) 51 { 52 UNREFERENCED_ParaMETER(hPrevInstance); 53 UNREFERENCED_ParaMETER(lpCmdline); 54 55 // Todo: 在此放置代码。 56 57 // 初始化全局字符串 58 LoadStringW(hInstance,IDS_APP_Title,szTitle,MAX_LOADSTRING); 59 LoadStringW(hInstance,IDC_DATaraINDEMO,szWindowClass,MAX_LOADSTRING); 60 MyRegisterClass(hInstance); 61 62 // 执行应用程序初始化: 63 if (!InitInstance (hInstance,nCmdshow)) 64 { 65 return FALSE; 66 } 67 srand(time(NulL));//以时间为种子 68 HACCEL hAcceltable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_DATaraINDEMO)); 69 70 MSG msg; 71 72 // 主消息循环: 73 while (GetMessage(&msg,nullptr,0,0)) 74 { 75 if (!TranslateAccelerator(msg.hwnd,hAcceltable,&msg)) 76 { 77 TranslateMessage(&msg); 78 dispatchMessage(&msg); 79 } 80 } 81 82 return (int) msg.wParam; 83 } 84 //宽字节 两个字节代表一个字符 85 TCHAR randomChar() 86 { 87 return (rand() % 10 + 98);//0-9的ASCII 88 } 89 90 // 91 // 函数: MyRegisterClass() 92 // 93 // 目的: 注册窗口类。 94 // 95 ATOM MyRegisterClass(HINSTANCE hInstance) 96 { 97 WNDCLASSEXW wcex; 98 99 wcex.cbSize = sizeof(WNDCLASSEX);100 101 wcex.style = CS_HREDRAW | CS_VREDRAW;102 wcex.lpfnWndProc = WndProc;103 wcex.cbClsExtra = 0;104 wcex.cbWndExtra = 0;105 wcex.hInstance = hInstance;106 wcex.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_DATaraINDEMO));107 wcex.hCursor = LoadCursor(nullptr,IDC_ARROW);108 wcex.hbrBackground = (HBrush)(color_WINDOW+1);109 wcex.lpszMenuname = NulL;//MAKEINTRESOURCEW(IDC_DATaraINDEMO);110 wcex.lpszClassname = szWindowClass;111 wcex.hIconSm = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));112 113 return RegisterClassExW(&wcex);114 }115 116 //117 // 函数: InitInstance(HINSTANCE,int)118 //119 // 目的: 保存实例句柄并创建主窗口120 //121 // 注释: 122 //123 // 在此函数中,我们在全局变量中保存实例句柄并124 // 创建和显示主程序窗口。125 //126 BOol InitInstance(HINSTANCE hInstance,int nCmdshow)127 {128 hInst = hInstance; // 将实例句柄存储在全局变量中129 130 /*HWND hWnd = CreateWindowW(szWindowClass,WS_OVERLAPPEDWINDOW,131 CW_USEDEFAulT,CW_USEDEFAulT,hInstance,nullptr);*/132 /*133 API 函数原型:134 HWND WINAPI CreateWindow(135 _In_opt_ LPCTSTR lpClassname,// 窗口类名称136 _In_opt_ LPCTSTR lpWindowname,// 窗口标题137 _In_ DWORD DWStyle,// 窗口风格,或称窗口格式138 _In_ int x,// 初始 x 坐标139 _In_ int y,// 初始 y 坐标140 _In_ int nWIDth,// 初始 x 方向尺寸141 _In_ int nHeight,// 初始 y 方向尺寸142 _In_opt_ HWND hWndParent,// 父窗口句柄143 _In_opt_ HMENU hMenu,// 窗口菜单句柄144 _In_opt_ HINSTANCE hInstance,// 程序实例句柄145 _In_opt_ LPVOID lpParam // 创建参数146 );147 148 注:_In_ 说明该参数是输入的,_opt_ 说明该参数是可选参数(不是说没有参数,而是可以利用宏搭配参数)。149 */150 HWND hWnd = CreateWindowW(szWindowClass,NulL,WS_DLGFRAME|WS_THICKFRAME|WS_POPUP,151 0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),nullptr);152 153 if (!hWnd)154 {155 return FALSE;156 }157 158 ShowWindow(hWnd,nCmdshow);159 UpdateWindow(hWnd);160 161 return TRUE;162 }163 164 //165 // 函数: WndProc(HWND,LParaM)166 //167 // 目的: 处理主窗口的消息。168 //169 // WM_COMMAND - 处理应用程序菜单170 // WM_PAINT - 绘制主窗口171 // WM_DESTROY - 发送退出消息并返回172 //173 //174 //初始化每个链表175 voID init(StrColum *cc,int cyScreen,int x)176 {177 int j;178 cc->ListStrLen = rand() % (STRMAXLEN - STRMINLEN) + STRMINLEN;179 cc->x = x+3;180 cc->y = rand() % 3 ? rand() % cyScreen : 0;181 cc->iMusstopTimes = rand() % 6;182 cc->iStopTimes = 0;183 cc->phead = cc->pCurent = (pMyList)calloc(cc->ListStrLen,sizeof(MyList));184 for ( j = 0; j < cc->ListStrLen-1; j++)185 {186 cc->pCurent->prev = cc->pTemp;187 cc->pCurent->ch = ‘‘;1881 cc->pCurent->pNext = cc->pCurent + ;189; cc->pTemp = cc->pCurent++190 } 191pTemp; cc->pCurent->prev = cc->192‘ cc->pCurent->ch = ‘;193phead;194 cc->pCurent->pNext = cc->pCurent;195 cc->phead->prev = cc->phead;196 cc->pCurent = cc->pTemp = cc-> randomChar();197 cc->phead->ch =}198 LRESulT CALLBACK WndProc(HWND hWnd,UINT message,WParaM wParam,LParaM lParam)199 {200 HDC hdc;201 int i,j,temp,ctn; 202static HDC hdcmem; 203 HFont hFont;204 static HBITMAP hBimtmap; 205staticint cxScreen,cyScreen; 206staticint 10 15 iFontWIDth = ,iColumCount;,iFontHeight = 207staticccChain; 208 StrColum *switch (message) 209 {210 case WM_CREATE: 211 {212 GetSystemMetrics(SM_CXSCREEN);213 cxScreen = GetSystemMetrics(SM_CYSCREEN);214 cyScreen =110 SetTimer(hWnd,,NulL);,215 GetDC(hWnd);216 hdc = CreateCompatibleDC(hdc);217 hdcmem = CreateCompatibleBitmap(hdc,cxScreen,cyScreen);218 hBimtmap = SelectObject(hdcmem,hBimtmap);219 ReleaseDC(hWnd,hdc); 220 2215 222 hFont = CreateFont(iFontHeight,iFontWIDth - 223, OUT_DEFAulT_PRECIS,CliP_DEFAulT_PRECIS, FW_BolD,DEFAulT_CHARSET,224 FF_SWISS,225 DRAFT_QUAliTY,FIXED_PITCH |"Fixedsys TEXT("));226 SelectObject(hdcmem,hFont);227 DeleteObject(hFont);228 SetBkMode(hdcmem,transparent);229 //设置显示的列数 23032 iColumCount = cxScreen / (iFontWIDth * ); / 231callocsizeof ccChain = (pStrColum)(StrColum));(iColumCount,232forint 0 () i = 233; i < iColumCount; i++ {234 32 init(ccChain + i,cyScreen,(iFontWIDth*i);/235)* }236 return0 ; 237 }238 break; 239case WM_COMMAND: 240 {241 int LOWORD(wParam); 242 wmID =// 分析菜单选择: 243switch (wmID) 244 {245 case IDM_ABOUT: 246 DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUTBox),hWnd,About);247 break; 248case IDM_EXIT: 249 DestroyWindow(hWnd);250 break; 251default: 252return DefWindowProc(hWnd,message,wParam,lParam); 253 }254 }255 break; 256case WM_PAINT: 257 {258 PAINTSTRUCT ps;259 ps);260 HDC hdc = BeginPaint(hWnd,&// Todo: 在此处添加使用 hdc 的任何绘图代码... 261ps);262 EndPaint(hWnd,& }263 break; 264265case WM_DESTROY: 2661); KillTimer(hWnd,267 DeleteObject(hBimtmap);268 DeleteDC(hdcmem);269 for0 ) ( i = 270; i < iColumCount; i++ {271 freepCurent); 272((ccChain+i)-> }273 free(ccChain); 2740); PostQuitMessage(275break; 276default: 277return DefWindowProc(hWnd,lParam); 278case WM_TIMER: 279 {280 GetDC(hWnd);281 hdc =282for PatBlt(hdcmem,BLACKnesS);int 0 () i = 283; i < iColumCount; i++ {284 iMusstopTimes;285 ctn = (ccChain + i)->iStopTimes++ >(ccChain + i)->phead;286 (ccChain + i)->pTemp = (ccChain + i)->255255 SetTextcolor(hdcmem,RGB(255,));,2871); textout(hdcmem,(ccChain+i)->x,(ccChain+i)->y,&((ccChain+i)->pTemp->ch),288y;289 j = (ccChain + i)->pNext;290 (ccChain + i)->pTemp = (ccChain + i)->pTemp->0; temp = 291//temp 绿色过度到黑色 292whilephead 293 ((ccChain+i)->pTemp!=(ccChain+i)->ch)294 &&(ccChain+i)->pTemp-> {295 0255 SetTextcolor(hdcmem,RGB(255,)-(296*(temp++0)); /(ccChain+i)->ListStrLen),297iFontHeight,298 textout(hdcmem,j-=1); &((ccChain+i)->pTemp->ch),299pNext;300 (ccChain + i)->pTemp = (ccChain + i)->pTemp-> }301 if (ctn) 3020; (ccChain + i)->iStopTimes = 303else304 continue; 305//下次开始显示的地方 (ccChain + i)->y += iFontHeight;306ifcyScreen) 307 ((ccChain+i)->y-(ccChain+i)->ListStrLen*iFontHeight> {308 freepCurent); 309((ccChain+i)->32 init(ccChain+i,(iFontWIDth*i);/310)* }311 prev;312 (ccChain + i)->phead = (ccChain + i)->phead-> randomChar();313 (ccChain + i)->phead->ch = }314 315316 ReleaseDC(hWnd,hdc); BitBlt(hdc,hdcmem,SRCcopY);317 }318 //case WM_RbuttonDOWN: 319// KillTimer(hWnd,1); 320// return 0; 321//case WM_RbuttonUP: 322// SetTimer(hWnd,1,10,NulL); 323// return 0; 324325 } 326 return0 ; 327}328 329330 331 332 333 334 // “关于”框的消息处理程序。 335INT_PTR CALLBACK About(HWND hDlg,LParaM lParam)336 {337 UNREFERENCED_ParaMETER(lParam);338 switch (message) 339 {340 case WM_INITDIALOG: 341return (INT_PTR)TRUE; 342343case WM_COMMAND: 344if IDCANCEL) 345 (LOWORD(wParam) == IDOK || LOWORD(wParam) == {346 EndDialog(hDlg,LOWORD(wParam));347 return (INT_PTR)TRUE; 348 }349 break; 350 }351 return (INT_PTR)FALSE; 352 }
实现效果展示
总结以上是内存溢出为你收集整理的C++------流星雨全部内容,希望文章能够帮你解决C++------流星雨所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)