How to customize the console applicaton

下面是如何最大化console和改变其显示的字体颜色的代码,顺便包含了计时代码(帮助做性能分析): 

    class Program
    {
        [DllImport("kernel32.dll", ExactSpelling = true)]
        private static extern IntPtr GetConsoleWindow();
        private static IntPtr ThisConsole = GetConsoleWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private const int HIDE = 0;
        private const int MAXIMIZE = 3;
        private const int MINIMIZE = 6;
        private const int RESTORE = 9;

        private const int OldestPhaseNo = 2003001;

        static void Main(string[] args)
        {
            // Set maximum console windows size
            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            ShowWindow(ThisConsole, MAXIMIZE);

            // Start the time watch
            Stopwatch watch = new Stopwatch();
            watch.Start();
            
            // Stop the time wattch
            Dictionary<string, string> ssqRecords = GetSsQiuRecords().Result;
            watch.Stop();

            // Change/Set the console's foregound clodr as green
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(watch.Elapsed.TotalSeconds);

            // Restart the time calculation to 0
            watch.Restart();
            SaveLotteryDataToLocalConfig(ssqRecords);
            watch.Stop();

            Console.WriteLine(watch.Elapsed.TotalSeconds);
            // Revert back to the default color
            Console.ForegroundColor = ConsoleColor.Gray;

            // Set the display buffer for console window
            Console.SetBufferSize(1000, 30000);
        }
    }
原文地址:https://www.cnblogs.com/researcher/p/5004970.html