Retrieving Parameters about Console

I am wondering how to retrieve information about the command prompt window, such as height and width, or font size, and if there are ways to change these.
Last edited on
Google "msdn GetConsoleScreenBufferInfo".

If you are thinking instead of playing with the actual console window and its geometry, then you'll have to get the console window's actual window DC to modify the window's pixel dimensions. I haven't actually tried to do it, so I'm not positive that it can be done. (The console window is unique in that it doesn't have an accessible WindowProc.)

Hope this helps.
I'll try it.. and let you know if/when it works.

( Thanks : )
Last edited on
Eh. I was being hopeful, but I never can understand a darn thing on msdn. Their constructs are over complicated (ironic, because obviously it was an attempt at simplification), and everything requires a special function to be initialised, which usually takes multiple other parameters who require their own initialising functions and so on.

I just won't worry about it.
Thanks though.
Heh, I know what you mean. I avoid MSDN as much as possible, but over the year's I've become used to it and it makes more sense.

What exactly do you want? Just to get the width and height of the window currently displayed to the user, or the width and height of the entire window accessible by the user (including the hidden part that scrolls away)?

I've probably got a little routine lying around that will get that info for you.
MSDN...stinks (unless you can use it).

I tried the same thing, but was totally confused.

Hope you find that routine, Duoas!
OK then, by popular demand.
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
#include <windows.h>

void get_window_size( int& lines, int& columns )
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  if (GetConsoleScreenBufferInfo(
        GetStdHandle( STD_OUTPUT_HANDLE ),
        &csbi
        ))
    {
    lines   = csbi.srWindow.Bottom -csbi.srWindow.Top  +1;
    columns = csbi.srWindow.Right  -csbi.srWindow.Left +1;
    }
  else lines = cols = 0;
  }

void set_window_size( int lines, columns )
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  if (GetConsoleScreenBufferInfo(
        GetStdHandle( STD_OUTPUT_HANDLE ),
        &csbi
        ))
    {
    // Make sure the new size isn't too big
    if (lines   > csbi.dwSize.Y) lines   = csbi.dwSize.Y;
    if (columns > csbi.dwSize.X) columns = csbi.dwSize.X;

    // Adjust window origin if necessary
    if ((csbi.srWindow.Top  +lines)   > csbi.dwSize.Y) csbi.srWindow.Top  = csbi.dwSize.Y -lines   -1;
    if ((csbi.srWindow.Left +columns) > csbi.dwSize.Y) csbi.srWindow.Left = csbi.dwSize.X -columns -1;

    // Calculate new size
    csbi.srWindow.Bottom = csbi.srWindow.Top  +lines   -1;
    csbi.srWindow.Right  = csbi.srWindow.Left +columns -1;

    SetConsoleWindowInfo(
      GetStdHandle( STD_OUTPUT_HANDLE ),
      true,
      &csbi.srWindow
      );
    }
  }

My routines are a bit more complex than this, and have extra stuff to make them compile under C and C++, so I basically just typed this in. Errors may have happened.

Is this what you want?
I haven't tested your arrangement of code yet, but I'll keep it under my belt.

What I really wish to ask of console is how many characters wide and tall the text area is. I guess font size isn't really necessary; I should hope the user can read at whatever size they've set it to.
The get_window_size() routines return the number of characters and lines visible in the console window at any given time.

To get the number of characters and lines actually available in the console window's buffer (which you can see using the scrollbars), lines 11 and 12 should be changed to:
1
2
lines   = csbi.dwSize.Y;
columns = csbi.dwSize.X;

Have fun!
Last edited on
How do most of you use MSDN - it dosn't seem that bad to use to me.
I just go to www.msdn.com (which is the same as www.msdn.microsoft.com).

It is a tabbed window - so just go to the Library tab and you get a nice
pane of the left with subject headings.

I tend to use my own docs. But they are a little old, so I'll google "msdn x" where 'x' is something (procedure name, function group name, etc) that I know will be there. Then I just use the left pane to zero-in on what I want.
Topic archived. No new replies allowed.