开发者

Platform independant ways of hiding console when program runs

开发者 https://www.devze.com 2023-03-21 11:25 出处:网络
Im looking for a way to hide the console (in windows) in my program, and ive found this code: #if defined (__WIN32__)

Im looking for a way to hide the console (in windows) in my program, and ive found this code:

#if defined (__WIN32__)
    #include <windows.h>
    HWND hWnd = GetConsoleWindow();
    ShowWindow(hWnd, SW_HIDE);
#endif

however, codeblocks keeps on giving me error: expected constructor, destructor, or type conversion before '(' token. what am i doing wrong?

what are codes of hide the console in mac and linux? I dont use mac or linux too often, but i want to make my p开发者_如何学Crograms platform independent. is there a code that works in all platforms? does the console even show up in mac and linux when compiling?


In general, console applications shouldn't mess with their window. If you need more advanced stuff (show/hide your window, decide its size, ...) you should probably switch to a GUI application. This holds true even if you just don't want any window: create a GUI application and don't create windows.

On Mac and Linux no console is shown by default when you start an executable (and there's no distinction between GUI and console executables); if you start it in a terminal, the application don't have much control over it (unless it uses escape codes, but they are to control text formatting/positioning). You can use some heuristic to guess the terminal emulator used and tell it to hide, but it's ugly, cumbersome and, again, defeats the purpose of a console application.

As far as your code snippet is concerned, you can't put that #include inside a function body: you should split that stuff in two pieces:

At the top of the file:

#ifdef __WIN32__
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
#endif

inside the function body:

#ifdef __WIN32__
    ShowWindow(GetConsoleWindow(), SW_HIDE);
#endif
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号