1 .net c# console background colors
所以我最近开始学习 C#,我正在努力将控制台的背景颜色更改为十六进制值。这是我的代码:
using System;
namespace MyFisrtC_App
{
class Program
{
static void Main(string[] args)
{
ConsoleColor BGColor = ConsoleColor.White;
ConsoleColor FGColor = ConsoleColor.Black;
Console.Title = "Dumb App";
Console.BackgroundColor = BGColor;
Console.ForegroundColor = FGColor;
Console.Clear();
Console.WriteLine("This is just a simple dumb app. There is nothing special about it.");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,如果您知道如何在 C# 中更改字体,那就太酷了。
小智 5
遗憾的是,由于控制台 API 的限制,这是不可能的。
但是,您可以计算与 API 中存在的颜色最接近的颜色。
static ConsoleColor ClosestConsoleColor(byte r, byte g, byte b)
{
ConsoleColor ret = 0;
double rr = r, gg = g, bb = b, delta = double.MaxValue;
foreach (ConsoleColor cc in Enum.GetValues(typeof(ConsoleColor)))
{
var n = Enum.GetName(typeof(ConsoleColor), cc);
var c = System.Drawing.Color.FromName(n == "DarkYellow" ? "Orange" : n); // bug fix
var t = Math.Pow(c.R - rr, 2.0) + Math.Pow(c.G - gg, 2.0) + Math.Pow(c.B - bb, 2.0);
if (t == 0.0)
return cc;
if (t < delta)
{
delta = t;
ret = cc;
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
这是来自:/sf/answers/863809551/全部归功于@Glenn
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |