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.
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.)
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.
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.
#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.
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:
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.