136上位机VC MFC字符雨特效仿黑客帝国数字雨
136上位机VC MFC字符雨特效仿黑客帝国数字雨
功能展示
电影中黑客角色技术演饰的特牛逼,我们看到被黑的电脑,或正在入侵的电脑界面会有显示字符雨特效,我们当前例程也模仿实现了字符雨功能,效果如图; 要点提示 关键点是自定义两个结构体,用于保存显示单个字符的信息,然后动态准备几列的字符,用定时器进行字符内容与位置的更新,代码比较多,具体的实现我们可以看源码!
实现功能 1.新建基于对话框的应用程序
2.自定义两个结构体用于保存字符信息 #define STRMAXLEN 25 //一个显示列的最大长度 #define STRMINLEN 8 //一个显示列的最小长度
typedef struct tagCharChain //整个当作屏幕的一个显示列,这是个双向列表 { structtagCharChain *prev; //链表的前个元素 TCHAR ch; //一个显示列中的一个字符 struct tagCharChain *next; //链表的后个元素 }CharChain, *pCharChain;
typedef struct tagCharColumn { CharChain*head, *current, *point; int x, y,iStrLen; //显示列的开始显示的x,y坐标,iStrLen是这个列的长度 int iStopTimes, iMustStopTimes; //已经停滞的次数和必须停滞的次数,必须停滞的次数是随机的 }CharColumn, *pCharColumn; 3.添加几个变量用于保存显示字符的信息,在OnInitDialog()中初始化 private: int m_nWndWidth,m_nWndHeight; //显示窗口的宽度 高度. int m_nFontWidth,m_nFontHeight ,m_nColumnCount;//字体的宽度 高度 列数
CharColumn *m_pCharColumn; - srand ((int) GetCurrentTime ()) ; //初始化随机数发生器
- SetTimer(1,10,NULL);
- m_nFontWidth=10;
- m_nFontHeight=15;
- m_nWndWidth = GetSystemMetrics(SM_CXSCREEN) ; //屏幕宽度
- m_nWndHeight = GetSystemMetrics(SM_CYSCREEN) ;
- MoveWindow(0,0,m_nWndWidth,m_nWndHeight);
-
-
- m_nColumnCount = m_nWndWidth/(m_nFontWidth*3/2); //屏幕所显示字母雨的列数
- m_pCharColumn = (pCharColumn)calloc(m_nColumnCount, sizeof(CharColumn));
- for(int i=0; i<m_nColumnCount; i++)
- {
- init(m_pCharColumn+i, m_nWndHeight, (m_nFontWidth*3/2)*i);
- }
复制代码- Init()为初化单个字符结构体的自定义函数,函数体为
- int CGkbc8Dlg::init(CharColumn *cc, int nWndHeight, int x) //初始化
- {
- cc->iStrLen = rand()%(STRMAXLEN-STRMINLEN) + STRMINLEN; //显示列的长度
- cc->x = x+3 ; //显示列的开始显示的x坐标
- cc->y =rand()%3?rand()%nWndHeight:0; //显示列的开始显示的y坐标
- cc->iMustStopTimes = rand()%6 ;
- cc->iStopTimes = 0 ;
- cc->head = cc->current = (pCharChain)calloc(cc->iStrLen, sizeof(CharChain)); //生成显示列
- for(int j=0; j<cc->iStrLen-1; j++)
- {
- cc->current->prev = cc->point; //cc->point一个显示列的前个元素
- cc->current->ch = '\0';
- cc->current->next = cc->current+1; //cc->current+1一个显示列的后个元素
- cc->point = cc->current++; //cc->point = cc->current; cc->current++; }
- cc->current->prev = cc->point; //最后一个节点
- cc->current->ch = '\0';
- cc->current->next = cc->head; cc->head->prev = cc->current; //头节点的前一个为此链的最后一个元素
- cc->current = cc->point = cc->head; //释放掉申请的内存要用current当参数
- cc->head->ch = (TCHAR)(rand()%(126-33)+33); //33到126之间,对链表头的 元素填充
- return 0;
- }
复制代码
4.添加定时器处理函数,实现字符的显示与更新 - void CGkbc8Dlg::OnTimer(UINT nIDEvent)
- {
- int j;
- HDC hdc,hdcMem;
- HBITMAP hBitmap;
- HFONT hFont;
- // hdc = ::GetDC(::GetDesktopWindow());//桌面上显示
- hdc = ::GetDC(m_hWnd);//当前窗口上显示
- hdcMem = CreateCompatibleDC(hdc);
- hBitmap = CreateCompatibleBitmap(hdc, m_nWndWidth, m_nWndHeight);
- hFont = CreateFont(m_nFontHeight, m_nFontWidth-5, 0, 0, FW_BOLD, 0, 0, 0,
- DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
- DRAFT_QUALITY, FIXED_PITCH | FF_SWISS, TEXT("Fixedsys"));
- SelectObject(hdcMem, hBitmap);
- SelectObject(hdcMem, hFont);
- SetBkMode(hdcMem, TRANSPARENT); //设置背景模式为 透明
- PatBlt (hdcMem, 0, 0, m_nWndWidth, m_nWndHeight, BLACKNESS) ; //将内存设备映像刷成黑色
- //<>
- int ctn;
- for(int i=0; i<m_nColumnCount; i++)
- {
- ctn = (m_pCharColumn+i)->iStopTimes++ > (m_pCharColumn+i)->iMustStopTimes;
- (m_pCharColumn+i)->point = (m_pCharColumn+i)->head; //point用于遍历整个显示列
- //第一个字符显示为 白色
- SetTextColor(hdcMem, RGB(255, 255, 255));
- TextOut(hdcMem, (m_pCharColumn+i)->x, (m_pCharColumn+i)->y, &((m_pCharColumn+i)->point->ch), 1);
- j = (m_pCharColumn+i)->y;
- (m_pCharColumn+i)->point = (m_pCharColumn+i)->point->next;
- //遍历整个显示列,将这个显示列里的字符从下往上显示
- int temp = 0 ; //temp绿色过度到黑色之用
- while((m_pCharColumn+i)->point != (m_pCharColumn+i)->head && (m_pCharColumn+i)->point->ch)
- {
- SetTextColor(hdcMem, RGB(0, 255-(255*(temp++)/(m_pCharColumn+i)->iStrLen), 0));
- TextOut(hdcMem, (m_pCharColumn+i)->x, j-=m_nFontHeight, &((m_pCharColumn+i)->point->ch), 1);
- (m_pCharColumn+i)->point = (m_pCharColumn+i)->point->next;
- }
- if(ctn)
- (m_pCharColumn+i)->iStopTimes = 0 ;
-
复制代码- else continue;
- (m_pCharColumn+i)->y += m_nFontHeight; //下次开始显示的y坐标 为当前的y坐标加上 一个字符的高度
- //如果开始显示的y坐标减去 整个显示列的长度超过了屏幕的高度
- if( (m_pCharColumn+i)->y-(m_pCharColumn+i)->iStrLen*m_nFontHeight > m_nWndHeight)
- {
- free( (m_pCharColumn+i)->current );
- init(m_pCharColumn+i, m_nWndHeight, (m_nFontWidth*3/2)*i);
- }
- //链表的头 为此链表的前个元素,因为下次开始显示的时候 就相当与在整个显示列的开头添加个元素,然后在开始往上显示
- (m_pCharColumn+i)->head = (m_pCharColumn+i)->head->prev;
- (m_pCharColumn+i)->head->ch = (TCHAR)(rand()%(126-33)+33);
- }
- //<>
- BitBlt(hdc, 0, 0, m_nWndWidth, m_nWndHeight, hdcMem, 0, 0, SRCCOPY);
- DeleteObject (hFont) ;
- DeleteObject (hBitmap) ;
- DeleteDC(hdcMem);
- ::ReleaseDC(m_hWnd, hdc);
- CDialog::OnTimer(nIDEvent);
- }
复制代码
最后是程序退出时,释放动态创建的字符变量
我们来实现整个功能实现的过程
|