C# 隱藏命令提示字元UI [C# Console Hide UI]

C# 隱藏命令提示字元UI [C# Console Hide UI]

C# 隱藏命令提示字元UI [C# Console Hide UI]


Code 

using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    public const int SW_HIDE = 0;
    public const int SW_SHOW = 5;
    public const int SW_MIN = 6;

    static public bool blnHide = true;
    static public void ShowNowTime()
    {
        Console.WriteLine(DateTime.Now.ToString());
        var handle = GetConsoleWindow();
        if(blnHide)
        {
            // Hide
            ShowWindow(handle, SW_HIDE);
        }
        else
        {
            // Show
            ShowWindow(handle, SW_SHOW);
        }
        blnHide = !blnHide;
    }

    private static void pause()
    {
        Console.WriteLine("\nPress any key to terminate...");
        Console.ReadKey();
    }
    static void Main(string[] args)
    {
        ShowWindow(GetConsoleWindow(), SW_MIN);
        
        System.Timers.Timer tmr = new System.Timers.Timer(2000);
        tmr.Elapsed += delegate
        {
            ShowNowTime();
        };
        tmr.Start();

        pause();
    }
}


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *