简体   繁体   English

Win32编程隐藏控制台window

[英]Win32 programming hiding console window

I'm learning C++ and I made a new program.我正在学习 C++ 并制作了一个新程序。 I deleted some of my code and now my console window is not hidden.我删除了一些代码,现在我的控制台 window 没有隐藏。 Is there a way to make it hide on startup without them seeing it?有没有办法让它在启动时隐藏起来而不让他们看到?

If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole .如果您正在编写一个控制台程序,并且想要断开您的程序与它开始时使用的控制台的连接,请调用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.最终,您可能不会对 function 真正做到的事情感到满意,但这就是您所问问题的字面答案。

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. “Consoleness”是 EXE 文件的一个属性。 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.有时非控制台程序被称为“GUI 程序”,因此您可能会在开发环境的配置选项中寻找“控制台”和“GUI”之间的选择。 Setting it to GUI doesn't require that you have any user interface at all, though.不过,将其设置为 GUI 根本不需要您有任何用户界面。 The setting merely controls whether your program starts with a console.该设置仅控制您的程序是否从控制台开始。

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 应用程序吗?

Assuming you're on windows, configure your linker to make a gui-program, not a console program.假设您使用的是 windows,请配置您的 linker 以制作 gui 程序,而不是控制台程序。

  • VS: Look in Linker ptions on project properties VS:查看项目属性的 Linker 选项
  • LINK: add /SUBSYSTEM:WINDOWS链接:添加 /SUBSYSTEM:WINDOWS
  • MinGW: -mwindows MinGW:-mwindows
#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;
}

I used to use ShowWindow (GetConsoleWindow(), SW_HIDE);我曾经使用ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you no need console, so don't create console app project.在这种情况下,但是如果您不需要控制台,请不要创建控制台应用程序项目。

As already said, starting the application with console or not is set in the exe.如前所述,是否使用控制台启动应用程序是在 exe 中设置的。 Using gnu compiler the option is -mwindows for no console, for example例如,使用 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,它也会被关闭!

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 .要根据需要从字面上隐藏/显示控制台 window,您可以使用以下功能: 可以使用ShowWindow隐藏/显示控制台。 GetConsoleWindow retrieves the window handle used by the console. GetConsoleWindow检索控制台使用的 window 句柄。 IsWindowVisible can be used to checked if a window (in that case the console) is visible or not. IsWindowVisible可用于检查 window(在这种情况下为控制台)是否可见。

#include <Windows.h>

void HideConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}

void ShowConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}

bool IsConsoleVisible()
{
    return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}

You can create your window minimized.您可以创建最小化的 window。 Or paint it outside the visible screen.或将其绘制在可见屏幕之外。

But you could also have messed with the window creation flags.但是您也可能弄乱了 window 创建标志。 If you really messed things up.如果你真的把事情搞砸了。 It is often better to start a new window.通常最好开始一个新的 window。 (Or restore from a previous version, or the backup). (或从以前的版本或备份恢复)。

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.第一个参数告诉程序获取当前正在运行程序的控制台 window。 The second argument passes down the instruction for what you want to do with the window.第二个参数传递您想要对 window 执行什么操作的指令。 "SW_HIDE" hides the window, while "SW_SHOW" shows the window. “SW_HIDE”隐藏 window,而“SW_SHOW”显示 window。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM