【问题标题】:Hide application window in C在 C 中隐藏应用程序窗口
【发布时间】:2019-01-05 18:54:22
【问题描述】:

我知道如何通过这段代码在 C# 中隐藏控制台应用程序的窗口:

using System.Runtime.InteropServices;
//Insert below before the main function
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

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

const int SW_HIDE = 0;
const int SW_SHOW = 5;

///End of before main


//now insert this under main function


var handle = GetConsoleWindow();

// Hide
ShowWindow(handle, SW_HIDE);

// Show
ShowWindow(handle, SW_SHOW);

现在的问题是,如何在 C 而不是 C++ 中隐藏窗口?我已经搜索了一个解决方案,但我在 C++ 中找到了所有解决方案。

【问题讨论】:

  • 通常不首先创建窗口是有意义的
  • 多么奇怪的问题:您已经解释了如何从 C# 访问 C API,但现在要问的是,您将如何从 C 访问 C API。答案既平凡又无益未来的访问者:只需访问 C API。您使用的是 C,因此不需要中介。
  • 然而,在这一点上,我必须支持大卫赫弗南。需要隐藏控制台窗口的人通常不知道为什么要首先创建该控制台,以及您可以控制该行为。您可能应该以/SUBSYSTEM:WINDOWS 而不是/SUBSYSTEM:CONSOLE 为目标,系统不会为您分配控制台。
  • 对于.net,进入应用程序设置,你会看到“输出类型:控制台应用程序”,如果你不想要控制台,将其更改为“Windows 应用程序”。

标签: c winapi visual-c++


【解决方案1】:

所有 WinAPI 调用都是 c 声明(不是 c++)。

所以,你也这样做:

//don't need to declare anything just:
#include <Windows.h>

//insert this in the main function
HWND handle = GetConsoleWindow();

// Hide
ShowWindow(handle, SW_HIDE);

// Show
ShowWindow(handle, SW_SHOW);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多