Create window console inside main win32 window

后端 未结 4 1962
有刺的猬
有刺的猬 2021-02-20 02:20

I have a win32 application that need to open a console like the games when tilde is pressed. I tought that the best solution is to use the CreateWindow function. Is this right?

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 02:35

    This is some pretty old code, haven't even really looked over it. Hopefully it's what you need. If you just need a very simple one you can also just make a call to 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 );
    
    }
    

提交回复
热议问题