代码之家  ›  专栏  ›  技术社区  ›  Timwi

显示/隐藏C控制台应用程序的控制台窗口

  •  158
  • Timwi  · 技术社区  · 14 年前

    我在谷歌上搜索如何隐藏自己的控制台窗口的信息。令人惊讶的是,我能找到的唯一解决方案是涉及 FindWindow() 按其标题 . 我深入研究了windowsapi,发现有一个更好更简单的方法,所以我想把它贴在这里,让其他人找到。

    如何隐藏(并显示)与我自己的C#控制台应用程序关联的控制台窗口?

    8 回复  |  直到 14 年前
        1
  •  296
  •   David Ferenczy Rogožan Hugo L.M    7 年前

    去应用程序的 然后改变主意 输出类型 控制台应用程序 Windows应用程序

        2
  •  290
  •   Timwi    12 年前

    方法如下:

    using System.Runtime.InteropServices;
    

    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    const int SW_HIDE = 0;
    const int SW_SHOW = 5;
    

    var handle = GetConsoleWindow();
    
    // Hide
    ShowWindow(handle, SW_HIDE);
    
    // Show
    ShowWindow(handle, SW_SHOW);
    
        3
  •  22
  •   Sasha    12 年前

    我建议将项目输出类型设置为 而不是控制台应用程序。它不会显示控制台窗口,而是像控制台应用程序那样执行所有操作。

        4
  •  22
  •   Maiko Kingma    7 年前

    您可以执行相反的操作,并将应用程序输出类型设置为:Windows Application。然后将此代码添加到应用程序的开头。

    [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr GetStdHandle(int nStdHandle);
    
    [DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern int AllocConsole();
    
    private const int STD_OUTPUT_HANDLE = -11;
    private const int MY_CODE_PAGE = 437;
    private static bool showConsole = true; //Or false if you don't want to see the console
    
    static void Main(string[] args)
    {
        if (showConsole)
        {
            AllocConsole();
            IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdHandle, true);
            FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
            System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
            StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
            standardOutput.AutoFlush = true;
            Console.SetOut(standardOutput);
        }
    
        //Your application code
    }
    

    showConsole true

        5
  •  11
  •   Community CDub    8 年前
        6
  •  7
  •   N T    6 年前

    “只是为了隐藏”你可以:

    将输出类型更改为 控制台应用程序 Windows应用程序

    而不是 Console.Readline/key 你可以用 new ManualResetEvent(false).WaitOne() 以保持应用程序运行。

        7
  •  2
  •   rag    12 年前

    如果不想依赖窗口标题,请使用以下选项:

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

        IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
        ShowWindow(h, 0);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormPrincipale());
    
        8
  •  0
  •   ScilsOff    10 年前

    如果集成一个小批量应用程序没有问题,那么有一个叫做 Cmdow.exe 这将允许您根据控制台标题隐藏控制台窗口。

    Console.Title = "MyConsole";
    System.Diagnostics.Process HideConsole = new System.Diagnostics.Process();
    HideConsole.StartInfo.UseShellExecute = false;
    HideConsole.StartInfo.Arguments = "MyConsole /hid";
    HideConsole.StartInfo.FileName = "cmdow.exe";
    HideConsole.Start();
    

    HideConsole.StartInfo.Arguments = "MyConsole /Vis";