c#控制台程序运行时隐藏窗体方法

一、最简单的办法:

在VS里选中项目点击右键,打开“属性”,把“输出类型”改成“Windows应用程序”。保存后运行发现黑色窗体不见了!

二、用代码设置:

<span style="font-family:Microsoft YaHei;font-size:14px;">        #region 隐藏窗口
        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        public static void WindowHide(string consoleTitle)
        {
            IntPtr a = FindWindow("ConsoleWindowClass", consoleTitle);
            if (a != IntPtr.Zero)
                ShowWindow(a, 0);//隐藏窗口
            else
                throw new Exception("can't hide console window");
        }
        #endregion



调用方 
//WindowHide(System.Console.Title);

WindowHide(System.Console.Title);</span>


你可能感兴趣的:(控制台)