Hello, I was wondering how I could go about hiding the command window during the execution of an application. If this is not possible, then how could I prevent the closing of the command window when the application is running again. For example, clicking on the close window button in the command window doesn't exit the program

Thank you all.

Recommended Answers

All 6 Replies

There are two basic ways.

First, you could simply FreeConsole to get rid of the console window, then AllocConsole to get a new one.

Better yet though, is to use ShowWindow:

#include <string>
#include <windows.h>

const char STR_CONSOLE_WINDOW_CLASS[] = "ConsoleWindowClass";

std::string GetConsoleTitle() {
  unsigned long length;
  std::string result( 1000, '\0' );
  length = GetConsoleTitle(
    const_cast<char *>( result.c_str() ),
    1000
    );
  result.resize( length );
  return result;
  }

void HideConsole() {
  HWND hConsole = FindWindow(
    STR_CONSOLE_WINDOW_CLASS,
    GetConsoleTitle().c_str()
    );
  ShowWindow( hConsole, SW_HIDE );
  }

void ShowConsole() {
  HWND hConsole = FindWindow(
    STR_CONSOLE_WINDOW_CLASS,
    GetConsoleTitle().c_str()
    );
  ShowWindow( hConsole, SW_SHOW );
  }

Well, that should do it... As usual, I just hacked this in so don't everyone complain too much if it needs a slight tweak to work.

You may want to use SetConsoleCtrlHandler() to keep the user from terminating your application when the console window is closed. A handler might look something like:

BOOL my_console_ctrl_event_handler( DWORD dwCtrlType ) {
  std::cout
    << "\n+-----------------------------------------------+"
    << "\n|  HEY YOU THERE! CLOSING ME ENDS THE PROGRAM!  |"
    << "\n+-----------------------------------------------+"
    << "\n"
    << "\nTo make this window disappear, select"
    << "\nFile -> Hide Console from the main window's menu."
    << std::endl;
  return dwCtrlType == CTRL_CLOSE_EVENT;
  }

(And if you use this message, don't forget to add a "Hide Console" command to the File menu.)

Hope this helps.

It poses many errors for me, unfortunetly.
I have two errors, "undefined reference to 'winmain@16' and Id returned 1 exit status.

This is when I run the first section of code you sent.

None of the code I posted is a complete program. They are utility functions.

[EDIT] In other words, copy and paste them into your program and use them at the appropriate moment. For example, if you just want the console window to disappear, start your program and either FreeConsole or HideConsole.

So how can I fix it?

Tried it, doesn't seem to work. Still the same error...

#include <string>
#include <windows.h>

const char STR_CONSOLE_WINDOW_CLASS[] = "ConsoleWindowClass";

std::string GetConsoleTitle() {
unsigned long length;
std::string result( 1000, '\0' );
length = GetConsoleTitle(
const_cast<char *>( result.c_str() ),
1000
);
result.resize( length );
return result;
}

void HideConsole() {
HWND hConsole = FindWindow(
STR_CONSOLE_WINDOW_CLASS,
GetConsoleTitle().c_str()
);
ShowWindow( hConsole, SW_HIDE );
}

Scratch that, I can't even use void HideConsole();, its says its undefined?

Thanks

The error you have has nothing to do with my code. undefined reference to 'winmain@16' means that you did not define a main function. Which means that you did not use the code I gave you in an application. Go to File -> New -> Windows Application to create a new project, write your forms, etc. and somewhere in there copy in the functions I gave you. Only then can you use them.

The second error is because of the first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.