Skip to main content
 首页 » 编程设计

c#之控制台窗口颜色、KeyUp、KeyDown 和 KeyPress 事件

2024年02月20日12qq号

1) 如何更改背景颜色使其看起来像这样?

screenshot

ConsoleColor 只改变文本行,我需要改变整个屏幕;所以不要说 ConsoleColor。

2) 如何使用 KeyUp、KeyDown 和 KeyPress 事件?以及如何为不同的按键设置不同的 Action ?
尽管我才刚刚开始使用 C#,但我发现了很多关于这些事件的指南,因此最好提供解释(而不仅仅是代码)。

请您参考如下方法:

How do I change the background color

您必须在设置 BackgroundColor 之后 清除控制台例如

Console.BackgroundColor = ConsoleColor.Blue; 
Console.Clear(); 

How do I use KeyUp, KeyDown and KeyPress Event?

您将使用 Console.ReadKey并处理每个键类型,例如

var input = Console.ReadKey(); 
switch (input.Key) 
{ 
    case ConsoleKey.LeftArrow: 
        // handle left arrow 
        break: 
    case ConsoleKey.RightArrow: 
        // handle right arrow 
        break; 
    ... 
}