【问题标题】:How to hide console window? [duplicate]如何隐藏控制台窗口? [复制]
【发布时间】:2014-08-23 11:01:34
【问题描述】:

csharp 不是我的母语,但我正在尝试了解如何更改我的 csharp 代码,以便在我运行可执行文件时不会出现控制台窗口。

我使用的代码是:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            Thread.Sleep(5000);
        }
    }
  }
}

我失败的尝试是:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    /// Foreground dll's
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    /// Console hide dll's
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

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

    const int SW_HIDE = 0;

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);

            Thread.Sleep(5000);
        }
    }
  }
}

任何帮助将不胜感激。

【问题讨论】:

  • 也许您正在尝试构建控制台应用程序以外的东西?类似于 Windows 服务或系统托盘应用程序?
  • 为什么这个问题被否决了? Op 提出了一个关于语言新手的想法,并提供失败的代码。

标签: c# console executable minimize


【解决方案1】:

在您的项目属性中选择 Windows Application 而不是 Console Application

【讨论】:

  • @Robert 请下载 Visual Studio Express。
  • 您能解释一下您是如何完成这个 Visual Studio Express 2013 的吗?
猜你喜欢
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 2019-07-22
  • 2012-10-14
  • 2012-10-14
相关资源
最近更新 更多