954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SDL - Hide Console

Hello,
I have decided to make a timer program on my computer to limit the amount of time that I game. Anyways, I have it working fine with a small borderless window in the top left of the screen that ticks down and when it gets to 0 it sounds an alarm, turns red, and waits for me to click it. The problem is that the console window pops up and looks very out of place. Is there some kind of SDL or Windows function that will allow me to destroy that window yet still maintain all the functionality of my SDL window?

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

You can go into your project settings and make it a Win32 Windows application and that will hide the console window.

sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

The issue is that it isn't a project. I tend to make my own projects because they are slightly smaller in size and easier to run. All I have is an executable in the same folder as a bunch of resources (images, sounds, headers). Is there any way to do this from within the .cpp so that the .exe hides the executable on its own?

Here is the folder (I just used the tree /f command):

ALARM.wav
BAD.png
FONT.ttf
GOOD.png
GUIGamingCounter.exe
ICON.bmp
ICON.ico
jpeg.dll
libpng12-0.dll
libtiff-3.dll
MINUTES.txt
Random.h
SDL_image.dll
zlib1.dll

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

If you are using Windows it's pretty simple, but not exactly guaranteed to not "flash" a console window for a moment.

What sfuo meant is you can tell your compiler to not spawn the console window. It's a compiler switch.

Now, this is the "dirty" way...

#include <Windows.h>

int main()
{
	HWND windowHandle = GetConsoleWindow();
	ShowWindow(windowHandle,SW_HIDE);
}
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

For some reason it is telling me that GetConsoleWindow() was not declared in this scope, even though I included ? I also tried changing to with no success. Here are my headers:

#include <windows.h>
#include <fstream>
#include <sstream>
#include "SDL\SDL.h"
#include "SDL\SDL_image.h"
#include "SDL\SDL_ttf.h"
#include "SDL\SDL_mixer.h"
#include "Random.h"
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 50
#define WINDOW_BPP 32

Any ideas?

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

Not really, ya just need to get a handle to the console window somehow.
http://msdn.microsoft.com/en-us/library/ms683175(v=vs.85).aspx

Okay, I think I see the problem...

Remarks

To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later. For more information, see Using the Windows Headers.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

Thanks, I was able to figure a way out:

SetConsoleTitle("||||||||||");
    Sleep(5);
    HWND CONSOLE=FindWindow(NULL, "||||||||||");
    ShowWindow(CONSOLE, SW_HIDE);

This works fine when run from Code::Blocks, but when run as a stand-alone executable the console doesn't go away?

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

Sorry for the double post, but I hit another problem. When I ran the program from C::B the console disappeared, but now its waiting for me to "Hit any key to continue..." before I can build it again. But the console is gone, so how do I get it to end execution?

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

right click your taskbar and select "Task Manager", go to the "Processes" tab, and kill the process.

Really, you should find the compiler option to not create the console window--it will avoid trouble like that.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

There is no process there, just Code::Blocks itself and my other programs, but not the process that I was testing. Also it still doesn't hide the console when I run the executable outside of Code::Blocks.

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

Okay, I used code blocks with the GCC compiler and this works for me:

#if       _WIN32_WINNT < 0x0500
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT   0x0500
#endif

#include <windows.h>

int main()
{
    ShowWindow(GetConsoleWindow(),SW_HIDE);
    Sleep(5000);//Sleep so I can check the task manager for the process.
    return 0;
}


It works inside the editor and outside of it, despite the editor making you press any key.

You can, of course, show the window again at the end of the program.

My best advice to you is to use the right compiler switch to make the console window not appear at all.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

I gave in and decided to make a project. All is well again :)

Labdabeta
Posting Pro in Training
486 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: