Create window console inside win32 main window

I have a win32 application that needs to open the console, like games when tilde is pressed. I believe the best solution is to use the CreateWindow function. It is right? How could I do this by blocking the main window and hiding it when the tilde is pressed again? Thanks to everyone.

+7
source share
5 answers

It is often tempting to use the console window in your application (using AllocConsole ), but it is certainly NOT a standard reusable Windows control. It has many features and features that make it unique from a typical window.

For this reason, I would agree with your instinct without using the true Console window. Create your own window using a text editor, since you have developed any other component of the user interface, for example, HUD.

Shouldn't I use CreateWindow : how do you do the rest of your GUI? DirectX? Gdi Some tools? Do you use other standard Windows controls?

+3
source

This is a pretty old code, didn't even look at it. Hope this is what you need. If you just need a very simple one, you can also just call AllocConsole ();

 void DevConsole::Create(){ CONSOLE_SCREEN_BUFFER_INFO consoleInfo; int consoleHandleR, consoleHandleW ; long stdioHandle; FILE *fptr; AllocConsole(); std::wstring strW = L"Dev Console"; SetConsoleTitle( strW.c_str() ); EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED); DrawMenuBar(GetConsoleWindow()); GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo ); stdioHandle = (long)GetStdHandle( STD_INPUT_HANDLE ); consoleHandleR = _open_osfhandle( stdioHandle, _O_TEXT ); fptr = _fdopen( consoleHandleR, "r" ); *stdin = *fptr; setvbuf( stdin, NULL, _IONBF, 0 ); stdioHandle = (long) GetStdHandle( STD_OUTPUT_HANDLE ); consoleHandleW = _open_osfhandle( stdioHandle, _O_TEXT ); fptr = _fdopen( consoleHandleW, "w" ); *stdout = *fptr; setvbuf( stdout, NULL, _IONBF, 0 ); stdioHandle = (long)GetStdHandle( STD_ERROR_HANDLE ); *stderr = *fptr; setvbuf( stderr, NULL, _IONBF, 0 ); } 
+2
source

Easy:

 AllocConsole(); HANDLE myConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD cCharsWritten; char* str = TEXT("enter game command"); WriteConsole(myConsoleHandle, str, strlen(str), &cCharsWritten, NULL); char* command = malloc(100); int charsRead = 0; ReadConsole(myConsoleHandle, command, 100, &cCharsRead, NULL); 

The advantage of using the system console is that you do not need to worry about handling many key events (except for tildes), etc. You can align the console with the game window using SetConsoleWindowInfo (it was hard to know).

This is an example of how you create AND write to the console what you want in your game, evaluate the first part when the user clicks the tilde (you evaluate it on the key event of the tilde), and you want to hide it when the user press enter.

Additional functions for customization: http://msdn.microsoft.com/en-us/library/ms682073%28VS.85%29.aspx

+2
source

The solutions will not work here because newer versions of the Windows SDK define the FILE structure:

 #ifndef _FILE_DEFINED #define _FILE_DEFINED typedef struct _iobuf { void* _Placeholder; } FILE; #endif 

If you try to overwrite FILE stdin / out structures with the => operator, only one pointer will be copied. To copy the entire FILE structure, you need to define the FILE structure before your windows.h includes:

 #ifndef _FILE_DEFINED struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE; #define _FILE_DEFINED #endif #include <Windows.h> 

If for some reason this fails, you can define your FILE structure as FILE_COMPLETE and use this code:

 #include <Windows.h> #include <io.h> #include <fcntl.h> AllocConsole(); SetConsoleTitleA("ConsoleTitle"); typedef struct { char* _ptr; int _cnt; char* _base; int _flag; int _file; int _charbuf; int _bufsiz; char* _tmpfname; } FILE_COMPLETE; *(FILE_COMPLETE*)stdout = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT), "w"); *(FILE_COMPLETE*)stderr = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w"); *(FILE_COMPLETE*)stdin = *(FILE_COMPLETE*)_fdopen(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r"); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0); 
+1
source

I found this little tutorial / code examples useful. http://www.halcyon.com/~ast/dload/guicon.htm

It creates a console and redirects STD_IN and STD_OUT to the console. This way you can easily use the streams std :: cin and std :: cout etc.

0
source

All Articles