同时实现Winform 和Console

1.改project 输出为console

2.Winform显示的情况运行,需要隐藏console窗口:

        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                Console.Title = "1234567.exe";
                IntPtr intptr = FindWindow("ConsoleWindowClass", "1234567.exe");
                if (intptr != IntPtr.Zero)
                {
                    ShowWindow(intptr, 0);//隐藏这个窗口
                }
                Utility.log.WriteLine("Program start with GUI!");
                Application.Run(new ewav());
            }
            else
            {
                Utility.log.WriteLine("Program start with CMD!");
                ewav cmdewav = new ewav(args);
                cmdewav.WindowState = FormWindowState.Minimized;
                cmdewav.ShowInTaskbar = false;
                cmdewav.Visible = false;
                cmdewav.Hide();
                Utility.CMDRUN = true;
                Application.Run(cmdewav);
            }

3.cmd运行可以进行交互

 

你可能感兴趣的:(笔记)