如何将C#二进制雨控制台程序改造为Windows屏保?
二进制数字雨屏保适配问题
我开发了一款C#程序,可持续显示0到255的二进制计数序列,以此模拟电影中常见的数字雨效果,只是以二进制代码呈现。我的目标是让该视觉效果在系统进入空闲状态时显示在显示器上。
遇到的问题
- 尝试将程序改造为适配系统空闲显示机制的格式,先后试过两种方式:
- 创建为Windows Forms Application
- 以Console application形式构建
- 修改文件扩展名符合屏保程序的预期格式,通过右键「安装」的标准流程部署(和现有屏保安装流程一致),但未成功:程序虽出现在配置设置中,屏保预览却无法启动,程序仅在后台运行,系统空闲时不会在显示器上显示。
我的问题
如何修改这款在控制台环境中可正常模拟所需视觉效果的C#程序,使其能在系统空闲时正常作为视觉屏保运行?本质上,如何打通控制台输出与空闲状态显示程序所需格式及行为之间的壁垒?
我的代码能成功生成目标视觉效果,但难以适配到系统空闲场景。修改为屏保对应的扩展名后,程序仅在后台运行,不会在屏幕上显示。

我的代码
using System; using System.Collections.Generic; using System.Threading; namespace BinaryCodeScreensaver { class BinaryCode { static void Main(string[] args) { int matrixCode = 0; int screenHeight = Console.WindowHeight; int screenWidth = Console.WindowWidth / 9; List<string> screenLines = new List<string>(); List<int> currentLines = new List<int>(); List<int> currentColumns = new List<int>(); List<int> columnDelays = new List<int>(); Random random = new Random(); ConsoleColor currentColor = ConsoleColor.DarkGreen; Console.CursorVisible = false; bool pageFilled = false; // Flag to track if the page is filled string symbols = "ÄÅÆÇÐÉÑÖØÜßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"; // String of possible symbols // Initialize screen lines for (int lineIndex = 0; lineIndex < screenHeight; lineIndex++) { screenLines.Add(""); } currentLines.Add(0); currentColumns.Add(0); columnDelays.Add(0); while (true) { // Generate new matrix code and add new columns if (matrixCode >= 255) { matrixCode = 0; if (currentLines.Count < screenWidth) { currentLines.Add(0); currentColumns.Add(currentLines.Count - 1); columnDelays.Add(random.Next(50)); } currentColor = GetRandomGreenShade(random); } // Iterate over columns and update characters for (int columnIndex = 0; columnIndex < currentLines.Count; columnIndex++) { if (columnDelays[columnIndex] > 0) { columnDelays[columnIndex]--; continue; } int currentLine = currentLines[columnIndex]; int currentColumn = currentColumns[columnIndex]; string binaryString = Convert.ToString(matrixCode, 2).PadLeft(8, '0') + " "; string previousString = ""; if (screenLines[currentLine].Length > currentColumn * 9) { previousString = screenLines[currentLine].Substring(currentColumn * 9, 9); } screenLines[currentLine] = screenLines[currentLine].PadRight(currentColumn * 9) + binaryString; Console.SetCursorPosition(currentColumn * 9, currentLine); Console.Write(new string(' ', binaryString.Length)); Console.SetCursorPosition(currentColumn * 9, currentLine); // Output characters with color and effects for (int characterIndex = 0; characterIndex < binaryString.Length - 1; characterIndex++) { if (characterIndex < previousString.Length && binaryString[characterIndex] != previousString[characterIndex]) { ConsoleColor newGreenShade = currentColor; while (newGreenShade == currentColor) { newGreenShade = GetRandomGreenShade(random); } Console.ForegroundColor = newGreenShade; // Apply random opacity to this character if (pageFilled && random.Next(100) < 60) // 60% chance of opacity change { int opacityLevel = random.Next(1, 4); // Generate random opacity level (1-3) switch (opacityLevel) { case 1: Console.ForegroundColor = ConsoleColor.Green; break; case 2: Console.ForegroundColor = ConsoleColor.DarkGreen; break; case 3: Console.ForegroundColor = ConsoleColor.Black; break; case 4: default: break; } } // Replace with a random symbol with a green shade if (pageFilled && random.Next(100) < 5) // 5% chance of symbol replacement { int randomSymbolIndex = random.Next(symbols.Length); Console.ForegroundColor = GetRandomGreenShade(random); Console.Write(symbols[randomSymbolIndex]); Console.ResetColor(); continue; } } else { if (previousString.Length > characterIndex && previousString[characterIndex] == '0') { Console.ForegroundColor = GetRandomGreenShade(random); } else { Console.ForegroundColor = ConsoleColor.White; } } Console.Write(binaryString[characterIndex]); } Console.ResetColor(); // Update line and column positions currentLines[columnIndex]++; if (currentLines[columnIndex] >= screenHeight) { currentLines[columnIndex] = 0; currentColumns[columnIndex]++; if (currentColumns[columnIndex] >= screenWidth) { currentColumns[columnIndex] = 0; pageFilled = true; // Page is filled when all columns are filled } } } matrixCode++; if (!pageFilled) { Thread.Sleep(0); } else { Thread.Sleep(50); } } } // Generate a random shade of green static ConsoleColor GetRandomGreenShade(Random random) { int greenValue = random.Next(100, 256); ConsoleColor greenShade = ConsoleColor.DarkGreen; if (greenValue > 200) { greenShade = ConsoleColor.Green; } else if (greenValue > 150) { greenShade = ConsoleColor.DarkGreen; } return greenShade; // Return the generated color } } }
我尝试将其重构为Form程序,但也未成功。
内容的提问来源于stack exchange,提问作者Jayce Sausa
相关产品推荐
相关产品推荐

