Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

c++ - How to get ride of console box of a GUI program compile by MinGW + Code::Block

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...