C ++如何在启动时隐藏控制台窗口? [英] C++ How do I hide a console window on startup?
问题描述
我想知道如何在启动时隐藏控制台窗口。
I want to know how to hide a console window when it starts.
它是用于键盘记录程序的,但这不是我的意图入侵某人。这是我想做的一个学校项目,目的是展示有关黑客的危险。
It's for a keylogger program, but it's not my intention to hack someone. It's for a little school project that I want to make to show the dangers about hackers.
到目前为止,这是我的代码:
Here's my code so far:
#include <cstdlib>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
cout << "Note. This program is only created to show the risk of being unaware of hackers." << endl;
cout << "This program should never be used to actually hack someone." << endl;
cout << "Therefore this program will never be avaiable to anyone, except me." << endl;
FreeConsole();
system("PAUSE");
return 0;
}
我看到窗口出现,并在启动时立即消失。之后似乎打开了一个新控制台,只是空白。 (空白表示按任意键以继续。我想知道是否与 system( PAUSE)
)有任何关系
I see the window appear and immediately disappear at startup. It seems to open a new console right after that, which is just blank. (By blank I mean "Press any key to continue.." I'm wondering if it has anything to do with system("PAUSE")
)
所以我想知道为什么它要打开一个新的控制台,而不是只创建和隐藏第一个控制台。
So I want to know why it opens a new console, instead of only creating and hiding the first one.
谢谢。 :)
推荐答案
要按需隐藏/显示控制台窗口,可以使用以下功能:
可以使用 ShowWindow 。 GetConsoleWindow 检索窗口控制台使用的句柄。
IsWindowVisible 可用于检查窗口(在这种情况下为控制台)是否可见。
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. GetConsoleWindow retrieves the window handle used by the console. IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return ::IsWindowVisible(::GetConsoleWindow()) != FALSE;
}
这篇关于C ++如何在启动时隐藏控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!