Console 花樣(2) — 自訂文字顯示的顏色

來源:互聯網
上載者:User
第二節: 自訂文字顯示的顏色Console 中顯示的文字預設都是黑底白字, 雖然可以用 Color 命令改變整個 Console 的背景色和前景色彩, 但是我們這裡研究的是在同一個Console視窗中,程式可以跟據不同需求輸入不同顏色的文字, 一個彩色的Console 視窗相信會大大吸引使用者的注意力. 給 Console 中的文字沒置顏色需要調用系統API SetConsoleTextAttribute 函數, 以下這段代碼是在網上找的,我測試過是可行的,現在帖出來供大家參考:// ConsoleColour.cs
using System;
// need this to make API calls
using System.Runtime.InteropServices;
using PFitzsimons.ConsoleColour;

namespace PFitzsimons.ConsoleColour
...{
    /**//// <summary>
    /// Static class for console colour manipulation.
    /// </summary>
    public class ConsoleColour
    ...{
        // constants for console streams
        const int STD_INPUT_HANDLE = -10;
        const int STD_OUTPUT_HANDLE = -11;
        const int STD_ERROR_HANDLE = -12;
        [DllImportAttribute("Kernel32.dll")]
        private static extern IntPtr GetStdHandle
        (
            int nStdHandle // input, output, or error device
        );
        [DllImportAttribute("Kernel32.dll")]
        private static extern bool SetConsoleTextAttribute
        (
            IntPtr hConsoleOutput, // handle to screen buffer
            int wAttributes    // text and background colors
        );
        // colours that can be set
        [Flags]
        public enum ForeGroundColour
        ...{
            Black = 0x0000,
            Blue = 0x0001,
            Green = 0x0002, 
            Cyan = 0x0003,
            Red = 0x0004,
            Magenta = 0x0005,
            Yellow = 0x0006,
            Grey = 0x0007,
            White = 0x0008
        }
        // class can not be created, so we can set colours 
        // without a variable
        private ConsoleColour()
        ...{
        }
        public static bool SetForeGroundColour()
        ...{
            // default to a white-grey
            return SetForeGroundColour(ForeGroundColour.Grey);
        }
        public static bool SetForeGroundColour(
            ForeGroundColour foreGroundColour)
        ...{
            // default to a bright white-grey
            return SetForeGroundColour(foreGroundColour, true);
        }
        public static bool SetForeGroundColour(
            ForeGroundColour foreGroundColour, 
            bool brightColours)
        ...{
            // get the current console handle
            IntPtr nConsole = GetStdHandle(STD_OUTPUT_HANDLE);
            int colourMap;
            
            // if we want bright colours OR it with white
            if (brightColours)
                colourMap = (int) foreGroundColour | 
                    (int) ForeGroundColour.White;
            else
                colourMap = (int) foreGroundColour;
            // call the api and return the result
            return SetConsoleTextAttribute(nConsole, colourMap);
        }
    }
}
//然後是主程式
namespace MyApp
...{
    public class MyApp
    ...{
        public static void Main()
        ...{
            ConsoleColour.SetForeGroundColour(
                ConsoleColour.ForeGroundColour.Green);
            Console.WriteLine("Text in green");
            ConsoleColour.SetForeGroundColour(
                ConsoleColour.ForeGroundColour.Red);
            Console.WriteLine("Text in red");
            // reset console back to a default
            ConsoleColour.SetForeGroundColour();
   Console.WriteLine("Text in default");
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.