在C中隐藏应用程序窗口

在C中隐藏应用程序窗口,c,winapi,visual-c++,C,Winapi,Visual C++,我知道如何通过以下代码在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

我知道如何通过以下代码在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++中找到的所有。

所有WiNAPI调用都是C声明(不是C++)。p> 所以,你也要这样做:

//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);
//不需要声明任何内容,只需:
#包括
//将其插入到主函数中
HWND handle=GetConsoleWindow();
//隐藏
显示窗口(手柄、开关隐藏);
//展示
显示窗口(手柄、开关显示);

通常情况下,一开始不创建窗口是有意义的。这是一个奇怪的问题:您已经解释了如何从C#访问C API,但现在正在问,如何从C访问C API。答案很简单,对未来的访问者来说也没有帮助:只需访问C API即可。你使用的是C,所以不需要中间人。然而,在这一点上,我不得不站在David Heffernan一边。需要隐藏控制台窗口的人通常不知道,为什么首先要创建控制台,并且您可以控制这种行为。您可能应该以
/SUBSYSTEM:WINDOWS
为目标,而不是
/SUBSYSTEM:CONSOLE
,系统将不会为您分配控制台。对于.net,转到应用程序设置,您将看到“outputtype:CONSOLE application”,如果您不需要控制台,请将其更改为“WINDOWS application”。