Loading

C# 调用控制台输出

     [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        [DllImport("User32.dll ", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll ", EntryPoint = "GetSystemMenu")]
        extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
        [DllImport("user32.dll ", EntryPoint = "RemoveMenu")]
        extern static int RemoveMenu(IntPtr hMenu, int nPos, int flags);
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();
     ///
        /// 打开控制台方法(条用次方法可打开控制台)
        ///
        public static void OpenConsole()
        {
            AllocConsole();
            //根据控制台标题找控制台
            int WINDOW_HANDLER = FindWindow(null, Console.Title);
            //A找关闭按钮 次代码和B下的代码是禁用打开窗口的关闭按钮(同时也会关闭程序)           
            IntPtr CLOSE_MENU = GetSystemMenu((IntPtr)WINDOW_HANDLER, IntPtr.Zero);
            int SC_CLOSE = 0xF060;
            //B关闭按钮禁用 (A和B可根据情况选用)          
            RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0);

            Console.WriteLine("程序已启动");
        }
在需要启动的位置,调用OpenConsole方法,在程序关闭后,调用FreeConsole方法,释放一下。
posted @ 2019-12-24 16:52  眾尋  阅读(837)  评论(0编辑  收藏  举报