How to resize console window?

How can I resize console window to make its size to 40x40 console chars and make console window unresizable?
Thanks.
Here you go. (I presume you are using Windows.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <limits>
#include <stdexcept>
using namespace std;

#include <windows.h>

//----------------------------------------------------------------------------
struct console
  {
  console( unsigned width, unsigned height )
    {
    SMALL_RECT r;
    COORD      c;
    hConOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (!GetConsoleScreenBufferInfo( hConOut, &csbi ))
      throw runtime_error( "You must be attached to a human." );

    r.Left   =
    r.Top    = 0;
    r.Right  = width -1;
    r.Bottom = height -1;
    SetConsoleWindowInfo( hConOut, TRUE, &r );

    c.X = width;
    c.Y = height;
    SetConsoleScreenBufferSize( hConOut, c );
    }

  ~console()
    {
    SetConsoleTextAttribute(    hConOut,        csbi.wAttributes );
    SetConsoleScreenBufferSize( hConOut,        csbi.dwSize      );
    SetConsoleWindowInfo(       hConOut, TRUE, &csbi.srWindow    );
    }

  void color( WORD color = 0x07 )
    {
    SetConsoleTextAttribute( hConOut, color );
    }

  HANDLE                     hConOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  };

//----------------------------------------------------------------------------
console con( 40, 40 );

//----------------------------------------------------------------------------
int main()
  {
  con.color( 0x1B );
  cout << "Press ";
  con.color( 0x5E );
  cout << " ENTER ";
  con.color( 0x1B );
  cout << " to quit.";

  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  return 0;
  }

There are some caveats. Make sure to read up over at MSDN on the console functions used.

Notice, BTW, how the "con" object is created before the main function... This is an attempt to preserve the user's console no matter what error happens. (Try running the program and pressing Ctrl-C to terminate it.)

The color() function could be made into a manipulator to control the stream -- heck, the console object itself could be made into a stream -- but both of these are another subject, so I left them out to keep the code simple.

Hope this helps.
Topic archived. No new replies allowed.