gpt4 book ai didi

c# ShowWindow(hWnd, 0) 不隐藏窗口

转载 作者:行者123 更新时间:2023-11-30 16:58:53 26 4
gpt4 key购买 nike

当我将下面的代码编译成可执行文件并运行它时,会出现控制台窗口。从我读过的文本中,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 设置”的答案。

最佳答案

花了一段时间才弄明白,但这是工作代码。

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);

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

/// 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);
}
}
}
}

关于c# ShowWindow(hWnd, 0) 不隐藏窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24557671/

26 4 0
文章推荐: c# - 如何获取使用 'Text figures' 的字体的上升或下降高度
文章推荐: javascript - 电子表格的谷歌脚本 : How to keep value saved for "ever"?
文章推荐: c - 如何使用 libvlc 检索编解码器信息?
文章推荐: javascript - 如何在使用 AngularJS 提交