【问题标题】:Window Hiding with C# [duplicate]使用 C# 隐藏窗口 [重复]
【发布时间】:2020-12-03 17:21:22
【问题描述】:

我将一个 C# 程序编译为一个 exe 文件。当我从 Windows 资源管理器中双击 exe 文件时,程序启动时会出现一个黑色的命令行窗口。如何在不显示命令行窗口的情况下从 Windows 资源管理器启动此文件

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    private const int HIDE = 0;
    private const int MAXIMIZE = 3;
    private const int MINIMIZE = 6;
    private const int RESTORE = 9;
    
    Process cmd = new Process();
    cmd.StartInfo.FileName = Path.Combine(path, "Launcher.exe");
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;
    cmd.Start();
    cmd.StandardInput.WriteLine(argHandlerArgs);
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    
    ShowWindow(cmd.MainWindowHandle, 0);

【问题讨论】:

标签: c# console-application process.start


【解决方案1】:

如果这是您所说的 Windows 窗体,则在项目属性中将输出类型设置为 Windows 应用程序

【讨论】:

    【解决方案2】:

    您可以使用ShowWindowApi 隐藏您的命令行。

    这是一个例子

    using System.Runtime.InteropServices
    
    class CommandLine
    {
    
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
        [DllImport("Kernel32")]
        private static extern IntPtr GetConsoleWindow();
    
        const int SW_HIDE=0;
        const int SW_SHOW=5;
    
        static void Main(string[] args)
        {
             IntPtr hwnd;
             hwnd=GetConsoleWindow();
             ShowWindow(hwnd,SW_HIDE);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 2012-02-15
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      相关资源
      最近更新 更多