Last active
March 21, 2021 16:36
-
-
Save z4none/7b2a27167f8b2d7095d4 to your computer and use it in GitHub Desktop.
create a console window in a win32 gui application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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(); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| 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