相信大家看过许许多多的关于计算机黑客、骇客、人工智能、AI方面的电影,每当黑客入侵某个五角大楼,某个网站时,都会出现这样一副画面:
或者这样的:
然后就轻而易举的成功入侵夺取管理员权限了,这时候的我们,心情肯定是激动的无以复加,心里大喊着:666!!!如果我有这么厉害的技术就好了!
所以大家想到今天要说的是什么了吗?没错,
(^U^)ノ~YO
和这并没有太大的关系,今天我们要讲的,是《数字雨》!
好了,我们来看下什么是数字雨。
数字雨,顾名思义,就是类似于填上下雨一样,往下掉数字或者是文字。
如下动态图:
这样看起来是不是感觉我们像是进入了一个科幻的虚拟世界呢?
这个呢,就是我们今天的效果图,由于只是截了张图,所以没有动态的效果!
当然了程序运行后会有有个小小的惊喜哦?
/**********************************************************************************
项目名称:数字雨《一颗开花的树》
***********************************************************************************/
#include
#include
#include
#pragma comment(lib, "WINMM.LIB")
#define NumOfColumn 25 //显示列的列数
typedef struct charList
{
struct charList * prev;
TCHAR ch; //放字符
struct charList * next;
}CharList;
typedef struct tagCharColumn
{
struct charList * head, *cur;
int x, y, iShownLen, iStrNum; //显示字数,字符数
}CharQueue;
struct showChar
{
TCHAR myChar[60];
int iNum; //字符个数
}charArr[7] = {//《一颗开花的树》
{ TEXT("如何让你遇见我,在我最美丽的时刻"),16 },
{ TEXT("为这,我已在佛前求了五百年,求他让我们结一段尘缘"),24 },
{ TEXT("佛于是把我化作一棵树,长在你必经的路旁"),19 },
{ TEXT("阳光下慎重地开满了花,朵朵都是我前世的盼望"),21 },
{ TEXT("当你走近,请你细听,那颤抖的叶是我等待的热情"),21 },
{ TEXT("而当你终于无视地走过,在你身后落了一地的"), 20 },
{ TEXT("朋友啊,那不是花瓣,是我凋零的心"),16 }
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
TCHAR szClassName[] = TEXT("数字雨");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;
if (!RegisterClass(&wndclass))
{
return 0;
}
hwnd = CreateWindow(szClassName, NULL, WS_DLGFRAME | WS_THICKFRAME | WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);
Show Window(hwnd, SW_SHOWMAXIMIZED);
Update Window(hwnd);
Show Cursor(FALSE);
srand(time(0));
//消息机制
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Show Cursor(TRUE);
return msg.wParam;
}
void CreateQueue(CharQueue * cc, int cyScreen, int x)
{
//bug -- 调用API---写接口---ctrl+c/ctrl+v
CharList * front;
int NumTemp = rand() % 6;
cc->x = x;
cc->y = rand() % 10 ? rand() % cyScreen : 0; //大约9/10的概率从中间开始下落。
cc->iShownLen = 1; //一开始就显示一个字符,然后慢慢增加,增加到等于歌词字符数时保持不变
cc->iStrNum = charArr[NumTemp].iNum; //歌词字符数
cc->head = cc->cur = front = (CharList *)calloc(cc->iStrNum, sizeof(CharList)); //创建显示列
//生成每个节点
int i;
for (i = 0; iiStrNum - 1; i++)
{
cc->cur->prev = front;
cc->cur->ch = charArr[NumTemp].myChar[i];
front = cc->cur++;
front->next = cc->cur;
}
//最后一个是标点符号
cc->cur->prev = front;
cc->cur->ch = charArr[NumTemp].myChar[i];
cc->cur->next = cc->head;
cc->head->prev = cc->cur;
cc->cur = cc->head;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static HDC hdcMem;
static HBITMAP hBitmap;
static CharQueue * AllChar;
HFONT hFont;
static int cxScreen, cyScreen;
static int iFontWidth = 20, iFontHeight = 20;
int i, j, y, greenToblack;
CharQueue * ccElem;
CharList * temp;
switch (message)
{
case WM_CREATE:
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
SetTimer(hwnd, 1, 70, NULL);
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, cxScreen, cyScreen);
SelectObject(hdcMem, hBitmap);
ReleaseDC(hwnd, hdc);
hFont = CreateFont(iFontHeight, iFontWidth, 0/*角度设置*/, 0/*角度设置*/, FW_BOLD/*黑体*/, 0, 0, 0,/*斜体 下划线 啊、删除线*/
DEFAULT_CHARSET/*字符集*/, OUT_DEFAULT_PRECIS/*指定输出精度*/, CLIP_DEFAULT_PRECIS/*指定裁剪精度*/,
DRAFT_QUALITY/*指向输出质量*/, FIXED_PITCH | FF_SWISS/*指定字体间距| 字体族*/, TEXT("宋体"));
SelectObject(hdcMem, hFont);
DeleteObject(hFont);
SetBkMode(hdcMem, TRANSPARENT);
PlaySound(L"素材.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//异步循环播放
AllChar = (CharQueue *)calloc(NumOfColumn, sizeof(CharQueue));//自动初始化为o
for (i = 0; i
{
CreateQueue(AllChar + i, cyScreen, 50 * i + 20);
}
return 0;
case WM_TIMER:
//该函数使用当前选入指定设备环境中的刷子绘制给定的矩形区域。通过使用给出的光栅操作来对该刷子的颜色和表面颜色进行组合。
PatBlt(hdcMem, 0, 0, cxScreen, cyScreen, BLACKNESS);
for (i = 0; i
{
ccElem = AllChar + i;
temp = ccElem->head;
SetTextColor(hdcMem, RGB(255, 255, 255));
TextOut(hdcMem, ccElem->x, ccElem->y, &temp->ch, 1/*字符个数*/);
y = ccElem->y;
greenToblack = 0;
ccElem->head = ccElem->head->next;
temp = temp->prev;
for (j = 1; jiShownLen; j++)
{
SetTextColor(hdcMem, RGB(/*greenToblack*5%255*/0, 255 - 255 * (greenToblack++) / (ccElem->iStrNum), 0));
TextOut(hdcMem, ccElem->x, y -= iFontHeight, &temp->ch, 1);
temp = temp->prev;
}
if (ccElem->iShownLeniStrNum)
{
ccElem->iShownLen++;
}
ccElem->y += iFontHeight;
if (ccElem->y - ccElem->iStrNum*iFontHeight>cyScreen)
{
free(ccElem->cur);
CreateQueue(ccElem, cyScreen, 128 * i + 17);
}
}
hdc = GetDC(hwnd);
BitBlt(hdc, 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
return 0;
case WM_RBUTTONDOWN:
KillTimer(hwnd, 1);
return 0;
case WM_RBUTTONUP:
SetTimer(hwnd, 1, 70, NULL);
return 0;
//case WM_LBUTTONDOWN:
case WM_KEYDOWN:
case WM_DESTROY:
KillTimer(hwnd, 1);
for (i = 0; i
{
ccElem = AllChar + i;
free(ccElem->cur);
}
free(AllChar);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
看完了今天的效果图和代码,细心的同学是不是发现了什么?
学C/C++不易,此路应携手前行。
欢迎关注我的编程公众號【草莓味狸猫】!
如果你想跟着小编一起学编程的话!
可以来我专栏介绍的C语言C++编程学习基地,【点击进入】!
还有(源码,零基础教程,项目实战教学视频)!