Hiding the console window

Started by
6 comments, last by balla_the_king 19 years, 4 months ago
Is there an easy way to hide the console window created by win32 console applications? I'm completely allergic to WinMain's and whatever uglyness they bring with it, but the console window is quite unneccesary after it spawned the game's main window.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
Advertisement
So rather than using an entry point that does exactly what you want, you want to instead hack a console app to not display a console?

...
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Easy!

Well, it took me a little bit of figuring out...

int main(int argc, char** argv){	HWND hWndConsole = GetForegroundWindow();	ShowWindow(hWndConsole,SW_HIDE);	return 0;}

That that is it.
Your console hidden.

Be sure to make it appear at the end of your program. Before return 0, just to be at the safe side.
using GetConsoleWindow is probably safer [smile]

ie.

int main(int argc, char** argv){        ShowWindow(GetConsoleWindow(), SW_HIDE);	return 0;}


[edit]
Uhm, i just realized that this function only exists in XP/2000 :(

[edit2]
Just looking through the console section of the MSDN, try FreeConsole(). That is available right back to win95 [smile]

- Thomas Cowellwebsite | journal | engine video

Thanks, all very helpfull! Pitty it will still display for a brief moment though, perhaps I should just use a winmain :/
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
you could just use #ifdef for different platforms

eg.

#ifdef _WIN32int WinMain(...)#elseint main (...)#endif


Of course winmain passes the command line in as a single string, so you'd have to parse each argument out manually. But other than that, theres no other "uglyness" about it.

- Thomas Cowellwebsite | journal | engine video

Quote:Original post by Nerusai
I'm completely allergic to WinMain's and whatever uglyness they bring with it


Are you starting as an empty project? Personally I don't like starting a win32 gui app because of all the extra stuff they like to put in either. The trick is to start the GUI app as an empty project and just write your own WinMain and WindProc, its not hard. Any good windows programming book will show you how to set up a minimal window from scratch and once its done you can use it as the basis for all your gui apps. Petzolds "Programming Windows", any of the windows-based LaMothe books and many tutorials will show you how.

throw table_exception("(? ???)? ? ???");

Don't do it like that! You still have a WinMain as entry-point under the hood which create a console window and calls your main function when you're in console mode! Hiding your console window like this 1000x uglier and really strange. Instead make an empty windows Gui project. Create a winmain.cpp file with a WinMain function which calls your regular main() function like this

//winmain.cpp#include <windows.h>extern int main(int argc, char* argv[]);int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance,				   LPSTR cmdLine, int cmdShow){	return main(__argc, __argv);}

This topic is closed to new replies.

Advertisement