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

时间:2014-07-03 15:23:21

标签: c# console window hide

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您也可以使用

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