Skip to content

Instantly share code, notes, and snippets.

@z4none
Last active March 21, 2021 16:36
Show Gist options
  • Select an option

  • Save z4none/7b2a27167f8b2d7095d4 to your computer and use it in GitHub Desktop.

Select an option

Save z4none/7b2a27167f8b2d7095d4 to your computer and use it in GitHub Desktop.
create a console window in a win32 gui application
#include <fcntl.h>
#include <io.h>
//
class EnableConsole
{
FILE * fp;
public:
~EnableConsole()
{
Close();
}
//
bool Open()
{
if (!AllocConsole())
{
return false;
}
freopen_s(&fp, "CONOUT$", "w", stdout);
return true;
}
void Close()
{
fclose(fp);
FreeConsole();
}
};
//
bool CreateConsole()
{
if(!AllocConsole()) return false;
HWND console_hwnd = GetConsoleWindow();
//::SetWindowLong(console_hwnd, GWL_STYLE, ::GetWindowLong(console_hwnd, GWL_STYLE) & (~WS_SYSMENU));
HMENU console_menu = ::GetSystemMenu(console_hwnd, FALSE);
if (console_menu != NULL) DeleteMenu(console_menu, SC_CLOSE, MF_BYCOMMAND);
SetWindowPos(console_hwnd, HWND_TOPMOST, 0, 0, 500, 300, SWP_SHOWWINDOW);
SetConsoleTitleA("console");
freopen_s(&_fp_console_out , "CONOUT$" , "w" , stdout);
freopen_s(&_fp_console_in , "CONIN$" , "r" , stdin);
freopen_s(&_fp_console_err , "CONOUT$" , "r" , stderr);
setlocale(LC_ALL, "chs");
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment