栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C#控制台应用程序中的自定义文本颜色?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#控制台应用程序中的自定义文本颜色?

该列表位于http://msdn.microsoft.com/zh-
cn/library/system.console.backgroundcolor.aspx

我相信是控制台中唯一受支持的颜色。不允许使用十六进制。

BlackDarkBlueDarkGreenDarkCyanDarkRedDarkMagentaDarkYellowGrayDarkGrayBlueGreenCyanRedMagentaYellowWhite

编辑

从我的公共仓库中获取工作项目文件

https://bitbucket.org/benskolnick/color-
console/

但是,在进一步调查中,您可以做很多工作来将红色和黄色组合成橙色。 请在此处遵循示例。不会重新张贴代码墙。
http://support.microsoft.com/kb/319883

这样做并不能使您获得更多的颜色,但是会导致正确的方向。您将需要进行一些PINVOKE工作,但我很容易就能将橙色或任何其他RGB颜色添加到控制台中。
http://pinvoke.net/default.aspx/kernel32.SetConsoleScreenBufferInfoEx

// Copyright Alex Shvedov// Modified by MercuryP with color specifications// Use this pre in any way you wantusing System;using System.Diagnostics;     // for Debugusing System.Drawing;         // for Color (add reference to  System.Drawing.assembly)using System.Runtime.InteropServices;    // for StructLayoutclass SetScreenColorsApp{    [StructLayout(LayoutKind.Sequential)]    internal struct COORD    {        internal short X;        internal short Y;    }    [StructLayout(LayoutKind.Sequential)]    internal struct SMALL_RECT    {        internal short Left;        internal short Top;        internal short Right;        internal short Bottom;    }    [StructLayout(LayoutKind.Sequential)]    internal struct COLORREF    {        internal uint ColorDWORD;        internal COLORREF(Color color)        { ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);        }        internal COLORREF(uint r, uint g, uint b)        { ColorDWORD = r + (g << 8) + (b << 16);        }        internal Color GetColor()        { return Color.FromArgb((int) (0x000000FFU & ColorDWORD), (int) (0x0000FF00U & ColorDWORD) >> 8, (int) (0x00FF0000U & ColorDWORD) >> 16);        }        internal void SetColor(Color color)        { ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);        }    }    [StructLayout(LayoutKind.Sequential)]    internal struct CONSOLE_SCREEN_BUFFER_INFO_EX    {        internal int cbSize;        internal COORD dwSize;        internal COORD dwCursorPosition;        internal ushort wAttributes;        internal SMALL_RECT srWindow;        internal COORD dwMaximumWindowSize;        internal ushort wPopupAttributes;        internal bool bFullscreenSupported;        internal COLORREF black;        internal COLORREF darkBlue;        internal COLORREF darkGreen;        internal COLORREF darkCyan;        internal COLORREF darkRed;        internal COLORREF darkMagenta;        internal COLORREF darkYellow;        internal COLORREF gray;        internal COLORREF darkGray;        internal COLORREF blue;        internal COLORREF green;        internal COLORREF cyan;        internal COLORREF red;        internal COLORREF magenta;        internal COLORREF yellow;        internal COLORREF white;    }    const int STD_OUTPUT_HANDLE = -11;       // per Winbase.h    internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);    // per Winbase.h    [Dllimport("kernel32.dll", SetLastError = true)]    private static extern IntPtr GetStdHandle(int nStdHandle);    [Dllimport("kernel32.dll", SetLastError = true)]    private static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);    [Dllimport("kernel32.dll", SetLastError = true)]    private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);    // Set a specific console color to an RGB color    // The default console colors used are gray (foreground) and black (background)    public static int SetColor(ConsoleColor consoleColor, Color targetColor)    {        return SetColor(consoleColor, targetColor.R, targetColor.G, targetColor.B);    }    public static int SetColor(ConsoleColor color, uint r, uint g, uint b)    {        CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();        csbe.cbSize = (int)Marshal.SizeOf(csbe);         // 96 = 0x60        IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7        if (hConsoleOutput == INVALID_HANDLE_VALUE)        { return Marshal.GetLastWin32Error();        }        bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);        if (!brc)        { return Marshal.GetLastWin32Error();        }        switch (color)        { case ConsoleColor.Black:     csbe.black = new COLORREF(r, g, b);     break; case ConsoleColor.DarkBlue:     csbe.darkBlue = new COLORREF(r, g, b);     break; case ConsoleColor.DarkGreen:     csbe.darkGreen = new COLORREF(r, g, b);     break; case ConsoleColor.DarkCyan:     csbe.darkCyan = new COLORREF(r, g, b);     break; case ConsoleColor.DarkRed:     csbe.darkRed = new COLORREF(r, g, b);     break; case ConsoleColor.DarkMagenta:     csbe.darkMagenta = new COLORREF(r, g, b);     break; case ConsoleColor.DarkYellow:     csbe.darkYellow = new COLORREF(r, g, b);     break; case ConsoleColor.Gray:     csbe.gray = new COLORREF(r, g, b);     break; case ConsoleColor.DarkGray:     csbe.darkGray = new COLORREF(r, g, b);     break; case ConsoleColor.Blue:     csbe.blue = new COLORREF(r, g, b);     break; case ConsoleColor.Green:     csbe.green = new COLORREF(r, g, b);     break; case ConsoleColor.Cyan:     csbe.cyan = new COLORREF(r, g, b);     break; case ConsoleColor.Red:     csbe.red = new COLORREF(r, g, b);     break; case ConsoleColor.Magenta:     csbe.magenta = new COLORREF(r, g, b);     break; case ConsoleColor.Yellow:     csbe.yellow = new COLORREF(r, g, b);     break; case ConsoleColor.White:     csbe.white = new COLORREF(r, g, b);     break;        }        ++csbe.srWindow.Bottom;        ++csbe.srWindow.Right;        brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);        if (!brc)        { return Marshal.GetLastWin32Error();        }        return 0;    }    public static int SetScreenColors(Color foregroundColor, Color backgroundColor)    {        int irc;        irc = SetColor(ConsoleColor.Gray, foregroundColor);        if (irc != 0) return irc;        irc = SetColor(ConsoleColor.Black, backgroundColor);        if (irc != 0) return irc;        return 0;    }}

然后,如果要使用橙色或任何其他颜色,则可以简单地调用SetScreenColor

static void Main(string[] args)    {        Color screenTextColor = Color.Orange;        Color screenBackgroundColor = Color.Black;        int irc = SetScreenColorsApp.SetScreenColors(screenTextColor, screenBackgroundColor);        Debug.Assert(irc == 0, "SetScreenColors failed, Win32Error pre = " + irc + " = 0x" + irc.ToString("x"));        Debug.WriteLine("LargestWindowHeight=" + Console.LargestWindowHeight + " LargestWindowWidth=" + Console.LargestWindowWidth);        Debug.WriteLine("BufferHeight=" + Console.BufferHeight + " WindowHeight=" + Console.WindowHeight + " BufferWidth=" + Console.BufferWidth + " WindowWidth=" + Console.WindowWidth);        //// these are relative to the buffer, not the screen:        //Debug.WriteLine("WindowTop=" + Console.WindowTop + " WindowLeft=" + Console.WindowLeft);        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);        Console.WriteLine("Some text in a console window");        Console.BackgroundColor = ConsoleColor.Cyan;        Console.ForegroundColor = ConsoleColor.Yellow;        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);        Console.Write("Press ENTER to exit...");        Console.ReadLine();        // Note: If you use SetScreenColors, the RGB values of gray and black are changed permanently for the console window.        // Using i.e. Console.ForegroundColor = ConsoleColor.Gray afterwards will switch the color to whatever you changed gray to        // It's best to use SetColor for the purpose of choosing the 16 colors you want the console to be able to display, then use        // Console.BackgroundColor and Console.ForegrondColor to choose among them.    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号