Jump to content
  • Advertisement
Sign in to follow this  
Nerusai

Hiding the console window

This topic is 5129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Thanks, all very helpfull! Pitty it will still display for a brief moment though, perhaps I should just use a winmain :/

Share this post


Link to post
Share on other sites
you could just use #ifdef for different platforms

eg.


#ifdef _WIN32
int WinMain(...)
#else
int 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!