Win32 Console App... multiple console buffers

This is a discussion on Win32 Console App... multiple console buffers within the Windows Programming forums, part of the Platform Specific Boards category; I want to have multiple consoles in a program, and switch between them. So I can have multiple screens the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Win32 Console App... multiple console buffers

    I want to have multiple consoles in a program, and switch between them.

    So I can have multiple screens the user can swap between by pressing a button... that would be especially useful for debugging.

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    
    int main()
    {
    	HANDLE oldConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	int junk;
    
    	cout << "Old!" << endl;
    
    	getch();
    
    	HANDLE newConsole = CreateConsoleScreenBuffer(GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    	FreeConsole();
    	cout << "This shouldn't show up" << endl;
    	SetConsoleActiveScreenBuffer(newConsole);
    	system("cls");
    
    	cout << "New!" << endl;
    	getch();
    
    	system("cls");
    	FreeConsole();
    	SetConsoleActiveScreenBuffer(oldConsole);
    	return 0;
    }
    I know I'm not doing this exactly right, but I don't know exactly how to do so.

    I know there is a way to do it with the Console APIs... http://msdn.microsoft.com/library/de..._functions.asp
    Last edited by Trauts; 04-28-2003 at 12:10 PM.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I wrote a class called Console that does such things... Source of a sample app is attached. Please forgive the state it's in. It works and all, but I never did get to putting the finishing touches on it...

    Hope it helps.

    Code:
    class Console {
      HANDLE  _scr;
      WORD     _textColor;
      WORD     _bgColor;
    
      void setColor() { if (isValid()) SetConsoleTextAttribute(_scr, _textColor | _bgColor); }
    
    public:
      Console(int textColor=COLOR_WHITE | COLOR_BOLD, int bgColor=COLOR_BLUE);
      ~Console();
    
      bool open();
      void show();// show our console (more than one may be used)
      void close();  
    
      void clear();// "cls"
      void goToXY(int x, int y);// reposition cursor
    
      void setTextColor(int color);
      void setBgColor(int color);
      
      DWORD print(const LPTSTR text);
      DWORD print(TCHAR ch);
    
      void flush();// empty the buffer
      bool keyHit();// was a key pressed?
      DWORD read(LPTSTR buf, int size);// buffered read
      bool getChar(TCHAR &ch);// unbuffered read
      TCHAR getChar() { TCHAR ch = 0; getChar(ch); return ch; }
    
      void setTitle(const LPTSTR title) { SetConsoleTitle(title); }
      bool isValid() const { return _scr != INVALID_HANDLE_VALUE; }
    
      // output operators
      Console& operator<<(const LPTSTR text) { print(text); return *this; }
      Console& operator<<(TCHAR ch) { print(ch); return *this; }
      Console& operator<<(int num);
      Console& operator<<(long num);
      Console& operator<<(double num);
    
      // input operators
      Console& operator>>(int &num);
      Console& operator>>(long &num);
      Console& operator>>(double &num);
    };
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Thanks, I'm trying it now. Run a search for the "Apoutput" thing if you're interested... an object oriented printf thing, where each object has its own cursor position, not changing the cout/printf's position.

    You can change the color using escape sequences, too. Or move the cursor using escape sequences or colors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a console window to a Win32 app
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 02-01-2009, 01:09 PM
  2. Replies: 28
    Last Post: 10-30-2008, 04:52 PM
  3. Win32 console, xp,nt,2000 only?
    By Ash1981 in forum C Programming
    Replies: 8
    Last Post: 01-01-2006, 03:09 AM
  4. Multiple toolboxes in an app.
    By Anonymous Cowar in forum Windows Programming
    Replies: 2
    Last Post: 12-03-2002, 03:17 AM
  5. multiple console windows
    By gordy in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-13-2002, 11:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21