9

When I compile and run a simple Win32 GUI program in MinGW+MSys with command line:

$ g++ main.cpp -o app -std=c++0x

$ ./app

only a dialog box shows. But when I put this program into Code::Blocks IDE and compile it, it always results in a black console box with the dialog. Adding -mwindows in link options no effect.

main.cpp:

#include <windows.h>
int WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
    MessageBox(0,"Hello, Windows","MinGW Test Program",MB_OK);
    return 0;
}

How can I get rid of the console box?

gudok
  • 4,029
  • 2
  • 20
  • 30
bo0k
  • 123
  • 1
  • 1
  • 6
  • 1
    Have you tried the options the author of this blog post used? Under Build Options > Other Options they have '-Wl,--subsystem,windows ' - http://phrasz.blogspot.com.au/2011/05/codeblocks-disable-console-window.html – charlemagne Apr 04 '12 at 01:38
  • '-Wl,--subsystem,windows' dont work in codeblock IDE, i add it into both compiler and linker options still show me console box. – bo0k Apr 04 '12 at 01:51
  • 1
    @bo0k, I know it's been a long time, but if you've just forgotten about this, I've just asked if there was any way to do this without a project, since the linker options I tried were not working, and the "problem" with `-Wl,--subsystem,windows` is that it only doesn't work *inside* the IDE. If you run it from explorer, it will not have a console window. I just thought you'd like to know. – chris Dec 20 '12 at 14:57

1 Answers1

18

Put it in a project, and in the project settings there's an option to not have a console window.

If you can't be bothered to have it in a project, a call to ShowWindow (GetConsoleWindow(), SW_HIDE); will make it flash on the screen and then disappear. Note that you must #define _WIN32_WINNT as 0x0500 or greater before including windows.h to have access to GetConsoleWindow(). I'll come back in a bit with a specific location to disable it.

//hide console window at start
#define _WIN32_WINNT 0x0501 //this is for XP
#include <windows.h>

int main()
{
    ShowWindow (GetConsoleWindow(), SW_HIDE);
    //rest of program here
}

EDIT: Found it, here's how to not have a console window:

  1. Click Project on the CodeBlocks menu.
  2. Click Properties.
  3. Click the second tab, Build Targets.
  4. On the right, where it says Type: Console application, change it to GUI application.
  5. Rebuild the project.
chris
  • 60,560
  • 13
  • 143
  • 205
  • 1
    Which option in codeblock? It OK when I am using command line. I think codeblock should add some compile or link options to make the console appear, but I dont konw how to find the command codeblock running. – bo0k Apr 04 '12 at 01:48
  • I'm not sure about any of the C::B-specific command line build options. I know this way works with the GUI though. – chris Apr 04 '12 at 01:56
  • 1
    Note, I had to "clean"/rebuild before this took effect for me – Steve Byrne Nov 16 '17 at 07:56