当我将下面的代码编译成可执行文件并运行时,控制台窗口会出现。根据我所读的资料,
以下是代码:
ShowWindow(hWnd,0)
应该能隐藏掉控制台窗口,但实际上它没有起作用。以下是代码:
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();
Console.WriteLine(handle);
ShowWindow(handle, SW_HIDE);
Thread.Sleep(5000);
}
}
}
}
有人能够解释一下代码中的问题在哪里吗?
Console.WriteLine(handle)
是一行代码,用于显示程序正在获取句柄,但它并没有将窗口最小化到该句柄所表示的状态。
请注意:我希望得到基于代码的答案,而不是“更改IDE设置”的答案。