1.首先遇到问题,先找答案,然后拼命试试。
2.我没有看自己的环境,编译环境等等,首先,我说明一下,这里是用CMake 编译出来的exe可执行文件.
3.现在这里用的c++. (MinGW)
进入正题:
a. 首先搜索 怎么隐藏cmd窗口,(之前在用python打包exe的时候,遇到过,打包完成的python程序,变成exe 后,点击exe 会有黑色的弹框,也是cmd 窗口)。
用一个Pyinstaller 工具打包的,添加一个-F 编译选项即可 解决了。
我TM 无语了,我程序本来是python写的,因为某些原因改用c++写了,你说要我 用回之前的python语言写?我不,就不。
于是,就找到了这些链接:
无非就是这些:
#include<windows.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
int main()
{
MessageBox(NULL,"Hello","Notice",NULL);
return 0;
}
试过了,没有用 的。这个适合,在vs 上编译的exe
还有就是这个,
使用api函数隐藏:
#include<windows.h>
int main()
{
HWND hwnd;
hwnd=FindWindow("ConsoleWindowClass",NULL); //处理顶级窗口的类名和窗口名称匹配指定的字符串,不搜索子窗口。
if(hwnd)
{
ShowWindow(hwnd,SW_HIDE); //设置指定窗口的显示状态
}
MessageBox(NULL,"Hello","Notice",MB_OK);
system("pause");
return 0;
}
我 程序里面,什么都没有调用,都会出现那个 黑色的cmd 窗口, 还是没法解决的啊。
........
......
后面,我觉得是要在编译器 上面下功夫,是要告诉编译器,有些东西不要生成。
找了好 几个小时。
https://stackoverflow.com/questions/18260508/c-how-do-i-hide-a-console-window-on-startup
在这个解决方案中,有人回答了,类似这个:
Hiding a console window at startup is not really possible in your code because the executable is run by the operating system with specific settings. That's why the console window is displayed for a very short time at startup when you use for example
FreeConsole();
To really hide the window at startup, you have to add a special option to you compiler. If you use gcc on Windows (MinGW) you can just add-mwindows
as compiler option in your makefile and there will be absolutely no window or "flash". I don't know about VisualStudio or whatever you use at the moment, but changing the way your IDE compiles you code is the way to go instead of coding workarounds in C++.In my view, this approach is better than using
WinMain
because it works reliably and you don't make your C++ Code platform dependent.
这个 意思是说,添加一个编译选项,-mwindows
我在CMakeLists 文件中添加了,好像没什么作用,那个框,还是弹出来了,真是无语...
到后来又过了一个小时,我看到了这个:
https://www.codenong.com/2752792/
add_executable(Cmd WIN32 cmd.c)
需要在生成可执行文件的时候,添加 WIN32 关键字.
不然上面的-mwindows 参数 ,好像也不会生效啊.
至此,问题已经解决。
o yeah ~~