Created
May 24, 2019 22:34
-
-
Save jsoctocat/cea753a9528b49b9747fd71bf935221b to your computer and use it in GitHub Desktop.
create and attach a console to a windows application for cin/cout
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 <windows.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
// Function prototypes. | |
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ); | |
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ); | |
// In a C++ Windows app, the starting point is WinMain(). | |
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) | |
{ | |
// these next few lines create and attach a console | |
// to this process. note that each process is only allowed one console. | |
AllocConsole() ; | |
AttachConsole( GetCurrentProcessId() ) ; | |
freopen( "CON", "w", stdout ) ; | |
printf("HELLO!!! I AM THE CONSOLE!" ) ; | |
WNDCLASSEX wc = { 0 }; | |
wc.cbSize = sizeof( WNDCLASSEX ) ; | |
wc.cbClsExtra = 0; // ignore for now | |
wc.cbWndExtra = 0; // ignore for now | |
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH ); | |
wc.hCursor = LoadCursor( NULL, IDC_ARROW ); | |
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); | |
wc.hInstance = hInstance; | |
wc.lpfnWndProc = WndProc; | |
wc.lpszClassName = TEXT("Philip"); | |
wc.lpszMenuName = 0; | |
wc.style = CS_HREDRAW | CS_VREDRAW; // Redraw the window | |
RegisterClassEx( &wc ); | |
HWND hwnd = CreateWindowEx( 0, TEXT("Philip"), TEXT("window's title!"), WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, hInstance, NULL ); | |
ShowWindow(hwnd, iCmdShow ); | |
UpdateWindow(hwnd); | |
MSG msg; | |
while( GetMessage( &msg, NULL, 0, 0 ) ) | |
{ | |
TranslateMessage( &msg ); | |
DispatchMessage( &msg ); | |
} | |
return msg.wParam; // return from WinMain | |
} | |
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ) | |
{ | |
switch( message ) | |
{ | |
case WM_CREATE: | |
// upon creation, let the speaker beep at 50Hz, for 10ms. | |
Beep( 50, 10 ); | |
printf("HELLO!!! I AM THE CONSOLE!" ) ; | |
return 0; | |
break; | |
case WM_PAINT: | |
{ | |
// we would place our Windows painting code here. | |
HDC hdc; | |
PAINTSTRUCT ps; | |
hdc = BeginPaint( hwnd, &ps ); | |
// draw a circle and a 2 squares | |
Ellipse( hdc, 20, 20, 160, 160 ); | |
Rectangle( hdc, 50, 50, 90, 90 ); | |
Rectangle( hdc, 100, 50, 140, 90 ); | |
printf("HELLO!!! I AM THE CONSOLE!" ) ; | |
EndPaint( hwnd, &ps ); | |
} | |
return 0; | |
break; | |
case WM_LBUTTONDOWN: | |
printf("STOP POKING MEEE!!!\n") ; | |
break; | |
case WM_DESTROY: | |
PostQuitMessage( 0 ) ; | |
return 0; | |
break; | |
} | |
return DefWindowProc( hwnd, message, wparam, lparam ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create and attach a console to a windows application for cin/cout
references:
https://bobobobo.wordpress.com/2009/03/01/how-to-attach-a-console-to-your-gui-app-in-c/
https://www.codeproject.com/Articles/15836/Writing-to-and-read-from-the-console-From-a-GUI-ap
https://www.codeguru.com/cpp/w-d/console/redirection/article.php/c3961/Windows-Applications-that-Have-Access-To-Console-Features.htm