将黑客帝国代码雨设置为屏幕保护程序

代码雨程序生成

  • 首先生成一个代码雨效果的程序,下面提供源代码,使用visual studio直接编辑即可。
    •   #include <windows.h>
      
        // 定义定时器的 ID
        #define ID_TIMER    1
        
        // 定义字符串的最大长度和最小长度
        #define STRMAXLEN  25
        #define STRMINLEN  8
        
        // 声明窗口过程函数
        LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
        
        // 定义字符链表结构体
        typedef struct tagCharChain {
        	struct tagCharChain *prev;  // 指向前一个字符节点的指针
        	char ch;  // 字符
        	struct tagCharChain *next;  // 指向后一个字符节点的指针
        } CharChain, *pCharChain;
        
        // 定义字符列结构体
        typedef struct tagCharColumn {
        	CharChain *head, *current, *point;  // 分别指向链表头、当前节点、遍历指针
        	int x, y, iStrLen;  // x、y 坐标和字符串长度
        	int iStopTimes, iMustStopTimes;  // 停止次数和必须停止的次数
        } CharColumn, *pCharColumn;
        
        // 主函数入口
        int main(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
        	// 窗口类名
        	static TCHAR szAppName[] = TEXT("matrix");
        	HWND hwnd;  // 窗口句柄
        	MSG msg;  // 消息结构体
        	WNDCLASS wndclass;  // 窗口类结构体
        
        	// 设置窗口类的风格为水平和垂直重绘
        	wndclass.style = CS_HREDRAW | CS_VREDRAW;
        	// 设置窗口过程函数为 WndProc
        	wndclass.lpfnWndProc = WndProc;
        	// 类的额外字节数为 0
        	wndclass.cbClsExtra = 0;
        	// 窗口的额外字节数为 0
        	wndclass.cbWndExtra = 0;
        	// 应用程序实例句柄
        	wndclass.hInstance = hInstance;
        	// 加载默认图标
        	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        	// 加载默认光标
        	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        	// 设置窗口背景为黑色画刷
        	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        	// 菜单名为 NULL,表示没有菜单
        	wndclass.lpszMenuName = NULL;
        	// 窗口类名
        	wndclass.lpszClassName = szAppName;
        
        	// 注册窗口类
        	if (!RegisterClass(&wndclass)) {
        		// 如果注册失败,弹出错误消息框并返回 0
        		MessageBox(NULL, TEXT("此程序必须运行在 NT 下!"), szAppName, MB_ICONERROR);
        		return 0;
        	}
        
        	// 创建窗口
        	hwnd = CreateWindow(szAppName, NULL, WS_DLGFRAME | WS_THICKFRAME | WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);
        
        	// 显示窗口并更新窗口
        	ShowWindow(hwnd, SW_SHOWMAXIMIZED);
        	UpdateWindow(hwnd);
        	// 隐藏光标
        	ShowCursor(FALSE);
        
        	// 使用当前时间作为随机数种子
        	srand((int)GetCurrentTime());
        	// 消息循环
        	while (GetMessage(&msg, NULL, 0, 0)) {
        		TranslateMessage(&msg);
        		DispatchMessage(&msg);
        	}
        	// 显示光标
        	ShowCursor(TRUE);
        	return msg.wParam;
        }
        
        // 生成随机字符,0 或 1
        char randomChar() {
        	return (char)(33 + rand() % (126 - 33 + 1));
        }
        
        // 初始化字符列
        int init(CharColumn *cc, int cyScreen, int x) {
        	int j;
        	// 随机生成字符串长度
        	cc->iStrLen = rand() % (STRMAXLEN - STRMINLEN) + STRMINLEN;
        	// 设置 x 坐标
        	cc->x = x + 3;
        	// 随机设置 y 坐标或为 0
        	cc->y = rand() % 3 ? rand() % cyScreen : 0;
        	// 随机生成必须停止的次数
        	cc->iMustStopTimes = rand() % 6;
        	// 初始化停止次数为 0
        	cc->iStopTimes = 0;
        	// 分配内存创建字符链表
        	cc->head = cc->current = (pCharChain)calloc(cc->iStrLen, sizeof(CharChain));
        	for (j = 0; j < cc->iStrLen - 1; j++) {
        		// 设置链表节点的前后指针
        		cc->current->prev = cc->point;
        		cc->current->ch = '\0';
        		cc->current->next = cc->current + 1;
        		cc->point = 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;
        	// 设置链表头节点的字符为随机字符
        	cc->head->ch = randomChar();
        	return 0;
        }
        
        // 窗口过程函数
        LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
        	HDC hdc;  // 设备上下文句柄
        	int i, j, temp, ctn;
        	static HDC hdcMem;  // 内存设备上下文句柄
        	HFONT hFont;  // 字体句柄
        	static HBITMAP hBitmap;  // 位图句柄
        	static int cxScreen, cyScreen;  // 屏幕宽度和高度
        	static int iFontWidth = 10, iFontHeight = 15, iColumnCount;  // 字体宽度、高度和列数
        	static CharColumn *ccChain;  // 字符列指针
        
        	switch (message) {
        	case WM_CREATE:
        		// 获取屏幕宽度和高度
        		cxScreen = GetSystemMetrics(SM_CXSCREEN);
        		cyScreen = GetSystemMetrics(SM_CYSCREEN);
        		// 设置定时器,ID 为 ID_TIMER,时间间隔为 10 毫秒
        		SetTimer(hwnd, ID_TIMER, 10, NULL);
        
        		// 获取窗口设备上下文
        		hdc = GetDC(hwnd);
        		// 创建与窗口设备上下文兼容的内存设备上下文
        		hdcMem = CreateCompatibleDC(hdc);
        		// 创建与窗口兼容的位图
        		hBitmap = CreateCompatibleBitmap(hdc, cxScreen, cyScreen);
        		// 选择位图到内存设备上下文
        		SelectObject(hdcMem, hBitmap);
        		// 释放窗口设备上下文
        		ReleaseDC(hwnd, hdc);
        
        		// 创建字体
        		hFont = CreateFont(iFontHeight, iFontWidth - 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, hFont);
        		// 删除字体句柄
        		DeleteObject(hFont);
        		// 设置背景模式为透明
        		SetBkMode(hdcMem, TRANSPARENT);
        		// 计算列数
        		iColumnCount = cxScreen / (iFontWidth * 3 / 2);
        
        		// 分配内存创建字符列数组
        		ccChain = (pCharColumn)calloc(iColumnCount, sizeof(CharColumn));
        		for (i = 0; i < iColumnCount; i++) {
        			// 初始化每个字符列
        			init(ccChain + i, cyScreen, (iFontWidth * 3 / 2) * i);
        		}
        		return 0;
        
        	case WM_TIMER:
        		// 获取窗口设备上下文
        		hdc = GetDC(hwnd);
        		// 用黑色填充内存设备上下文
        		PatBlt(hdcMem, 0, 0, cxScreen, cyScreen, BLACKNESS);
        		for (i = 0; i < iColumnCount; i++) {
        			// 判断是否应该停止
        			ctn = (ccChain + i)->iStopTimes++ >(ccChain + i)->iMustStopTimes;
        			// 设置遍历指针为链表头
        			(ccChain + i)->point = (ccChain + i)->head;
        
        			// 设置文本颜色为白色
        			SetTextColor(hdcMem, RGB(255, 255, 255));
        			// 在内存设备上下文输出一个字符
        			TextOut(hdcMem, (ccChain + i)->x, (ccChain + i)->y, &((ccChain + i)->point->ch), 1);
        			j = (ccChain + i)->y;
        			// 移动遍历指针到下一个节点
        			(ccChain + i)->point = (ccChain + i)->point->next;
        			temp = 0;
        			while ((ccChain + i)->point != (ccChain + i)->head && (ccChain + i)->point->ch) {
        				// 设置文本颜色为渐变绿色
        				SetTextColor(hdcMem, RGB(0, 255 - (255 * (temp++) / (ccChain + i)->iStrLen), 0));
        				// 在内存设备上下文输出一个字符
        				TextOut(hdcMem, (ccChain + i)->x, j -= iFontHeight, &((ccChain + i)->point->ch), 1);
        				// 移动遍历指针到下一个节点
        				(ccChain + i)->point = (ccChain + i)->point->next;
        			}
        			if (ctn)
        				// 如果应该停止,重置停止次数
        				(ccChain + i)->iStopTimes = 0;
        			else continue;
        			// 更新 y 坐标
        			(ccChain + i)->y += iFontHeight;
        			if ((ccChain + i)->y - (ccChain + i)->iStrLen * iFontHeight > cyScreen) {
        				// 如果超出屏幕,释放链表内存并重新初始化
        				free((ccChain + i)->current);
        				init(ccChain + i, cyScreen, (iFontWidth * 3 / 2) * i);
        			}
        			// 链表头指针移动到前一个节点
        			(ccChain + i)->head = (ccChain + i)->head->prev;
        			// 设置新的随机字符到链表头
        			(ccChain + i)->head->ch = randomChar();
        		}
        
        		// 将内存设备上下文的内容复制到窗口设备上下文
        		BitBlt(hdc, 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY);
        		// 释放窗口设备上下文
        		ReleaseDC(hwnd, hdc);
        		return 0;
        
        	case WM_RBUTTONDOWN:
        		// 右键按下,停止定时器
        		KillTimer(hwnd, ID_TIMER);
        		return 0;
        
        	case WM_RBUTTONUP:
        		// 右键抬起,重新启动定时器
        		SetTimer(hwnd, ID_TIMER, 10, NULL);
        		return 0;
        
        	case WM_KEYDOWN:
        	case WM_LBUTTONDOWN:
        	case WM_DESTROY:
        		// 按键按下、左键按下或窗口销毁时,停止定时器
        		KillTimer(hwnd, ID_TIMER);
        		// 删除位图
        		DeleteObject(hBitmap);
        		// 删除内存设备上下文
        		DeleteDC(hdcMem);
        		for (i = 0; i < iColumnCount; i++) {
        			// 释放每个字符列的链表内存
        			free((ccChain + i)->current);
        		}
        		// 释放字符列数组内存
        		free(ccChain);
        		// 发送退出消息
        		PostQuitMessage(0);
        		return 0;
        	}
        	return DefWindowProc(hwnd, message, wParam, lParam);
        }
      
  • 代码雨程序参考 C语言实现黑客帝国代码雨

设置屏保

  • 上述代码编译生成一个可执行程序 customScreen.exe,将此文件后缀名修改为customScreen.scr。然后将customScreen.scr拷贝到C:\Windows\System32目录下。
  • 打开设置,搜索更改屏幕保护程序,点击打开对话框。会看到屏幕保护程序下拉框多了一个刚才添加的customScreen程序。选择这一项,应用并保存。就设置成功了。
    • 在这里插入图片描述
  • 效果
    • 请添加图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大草原的小灰灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值