有一个winform项目,其中需要使用AllocConsole()方法产生一个控制台界面同步输出结果但这个控制台界面只能用用FreeConsole()释放,不能直接按X或者ctrl+c关闭,否则整个程序都会关闭我现在采用的是让X灰掉的方法            IntPtr windowHandle = NativeMethod.FindWindow(null, Process.GetCurrentProcess().MainModule.FileName);            IntPtr closeMenu = NativeMethod.GetSystemMenu(windowHandle, IntPtr.Zero);            
            uint SC_CLOSE = 0xF060;            NativeMethod.RemoveMenu(closeMenu, SC_CLOSE, 0x0);但只对winxp/2003有效,vista下就没效果了而且xp下也经常出现灰不掉的状况,各位高手有什么好的方案解决这个问题不,求教了如何能使它灰得更彻底,或者直接废掉控制台上的X,那就完美了,等待答案ing

解决方案 »

  1.   

    最好的办法是不让控制台界面出现,而去截获控制台程序的输出,并将输出显示在winform界面上,如此在任何操作系统下,你都无需关心X是否能够隐藏了。
             .....
             Process consoleProcess = new Process();
             
             consoleProcess .StartInfo.FileName = "myconsole.exe";
             consoleProcess .StartInfo.UseShellExecute = false;
             consoleProcess .StartInfo.RedirectStandardInput = true;
             StreamReader streamReader = consoleProcess .StandardOutput;
             string lineString = streamReader.ReadLine();
             .....