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

本文关键字:隐藏 窗口 ShowWindow hWnd | 更新日期: 2023-09-27 18:11:23

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

c# 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);
    [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);
        }
    }
  }
}

您也可以使用

private static extern int ShowWindow(int hwnd, int nCmdShow);

隐藏窗口。此方法接受窗口的整数处理程序(而不是指针)。使用 spy++ (在Visual Studio工具中),您可以获得想要隐藏的窗口的类名窗口名。然后可以执行如下操作

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
const int SW_HIDE = 0;
public void hideScannerDialog()
        {
            // retrieve the handler of the window  
            int iHandle = FindWindow("ClassName", "WindowName"); //The className & WindowName I got using Spy++
            if (iHandle > 0)
            {
                // Hide the window using API        
                ShowWindow(iHandle, SW_HIDE);
            }
        }