C++ Win32 编程隐藏控制台窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/622592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Win32 programming hiding console window
提问by H4cKL0rD
Im learning C++ and i made a New program and i deleted some of the code and now my console window will not hide is there a way to make it hide on startup without them seeing it
我正在学习 C++,我做了一个新程序,我删除了一些代码,现在我的控制台窗口不会隐藏 有没有办法让它在启动时隐藏而不被他们看到
回答by Rob Kennedy
If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole
. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.
如果你正在编写一个控制台程序并且你想断开你的程序与它开始的控制台的连接,那么调用FreeConsole
. 最终,您可能不会对该函数的实际功能感到满意,但这就是您所问问题的字面答案。
If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't requirethat you have any user interface at all, though. The setting merely controls whether your program starts with a console.
如果您正在编写一个从一开始就不想拥有控制台的程序,那么请配置您的项目,使其不是控制台程序。“控制台”是 EXE 文件的一个属性。操作系统读取该设置并决定是否在您的任何代码运行之前为您的程序分配一个控制台,因此您无法在程序中控制它。有时,非控制台程序称为“GUI 程序”,因此您可能会在开发环境的配置选项中寻找“控制台”和“GUI”之间的选择。不过,将其设置为 GUI 根本不需要您拥有任何用户界面。该设置仅控制您的程序是否以控制台启动。
If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?
如果您正在尝试编写一个有时可以有控制台而有时没有的程序,那么请参阅前面的问题,一个可执行文件可以既是控制台又是 GUI 应用程序吗?
回答by Macke
Assuming you're on windows, configure your linker to make a gui-program, not a console program.
假设您在 Windows 上,请配置您的链接器以制作 gui 程序,而不是控制台程序。
- VS: Look in Linker ptions on project properties
- LINK: add /SUBSYSTEM:WINDOWS
- MinGW: -mwindows
- VS:查看项目属性上的链接器选项
- 链接:添加 /SUBSYSTEM:WINDOWS
- MinGW:-mwindows
回答by udit043
#include <windows.h>
#include <iostream>
using namespace std;
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
int main()
{
cout<<"this sentence is visible\n";
Stealth(); //to hide console window
cout<<"this sentence is not visible\n";
system("PAUSE");
return EXIT_SUCCESS;
}
回答by ST3
I used to use ShowWindow (GetConsoleWindow(), SW_HIDE);
in such case, however if you no need console, so don't create console app project.
我曾经ShowWindow (GetConsoleWindow(), SW_HIDE);
在这种情况下使用,但是如果您不需要控制台,那么不要创建控制台应用程序项目。
回答by Alejadro Xalabarder
As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
如前所述,在 exe 中设置是否使用控制台启动应用程序。例如,使用 gnu 编译器的选项是 -mwindows 没有控制台
g++ -mwindows winapp.c
it seems that the method
看来方法
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a command line console this will be closed as well!
也关闭所有父控制台,因此如果您从命令行控制台启动 winapp.exe,这也将关闭!
回答by nikau6
To literally hide/show the console window on demand, you could use the following functions: It's possible to hide/show the console by using ShowWindow. GetConsoleWindowretrieves the window handle used by the console. IsWindowVisiblecan be used to checked if a window (in that case the console) is visible or not.
要根据需要隐藏/显示控制台窗口,您可以使用以下功能: 可以使用ShowWindow隐藏/显示控制台。GetConsoleWindow检索控制台使用的窗口句柄。 IsWindowVisible可用于检查窗口(在这种情况下是控制台)是否可见。
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}
回答by Toon Krijthe
You can create your window minimized. Or paint it outside the visible screen.
您可以创建最小化的窗口。或将其绘制在可见屏幕之外。
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).
但是您也可能弄乱了窗口创建标志。如果你真的把事情搞砸了。通常最好打开一个新窗口。(或从以前的版本或备份恢复)。
回答by DerpyCoder
You can try this
你可以试试这个
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
它是 win32 API 的一部分,您可以使用“#include”包含它
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.
第一个参数告诉程序获取当前正在运行程序的控制台窗口。第二个参数传递您想要对窗口执行的操作的指令。“SW_HIDE”隐藏窗口,而“SW_SHOW”显示窗口。