Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing console size

I got a problem with changing console size. This is my code:

BOOL setConsole(int x, int y)
{
hStdin = GetStdHandle(STD_INPUT_HANDLE); 
hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
if (hStdin == INVALID_HANDLE_VALUE || 
    hStdout == INVALID_HANDLE_VALUE) 
{
    MessageBox(NULL, TEXT("GetStdHandle"),
        TEXT("Console Error"), MB_OK);
    return false;
}


SMALL_RECT windowSize = {0, 0, x-1, y-1};

// Change the console window size:
SetConsoleWindowInfo(hStdout, TRUE, &windowSize);

COORD c = { x, y};

//Change the internal buffer size:
SetConsoleScreenBufferSize(hStdout, c);


SetConsoleDisplayMode(hStdout,CONSOLE_FULLSCREEN_MODE, &c);

return true;
}

It works perfectly fine, when I try to enlarge the console. When one parameter is smaller than previous one nothing happens. What is wrong?

@edit: after some tests I noticed, that resizing(reducing) is possible if I change one parameter at once. Example(assume console is 100x100)

 setConsole(90,90); //dosen't work.
 setConsole(90,100);
 setConsole(90,90); // works perfectly

WHY?!

like image 520
Krever Avatar asked Dec 18 '25 00:12

Krever


2 Answers

SetConsoleScreenBufferSize changes the size of the internal buffer of the console. Changing it has no effect on the console windows extent. Call SetConsoleWindowInfo if you need an effect on the visible part of the console (buffer).

The window buffer cannot be smaller than the internal buffer , and decreasing it will also decrease the internal buffer, but not the other way around.

If you call SetConsoleScreenBufferSize with illegal value in COORDS (e.g. too little height/width) then you get an error, usually 87 'invalid argument'.

Try this code:

#include <iostream>
#include <windows.h> 

using namespace std;


void SetWindow(int Width, int Height) 
{ 
    _COORD coord; 
    coord.X = Width; 
    coord.Y = Height; 

    _SMALL_RECT Rect; 
    Rect.Top = 0; 
    Rect.Left = 0; 
    Rect.Bottom = Height - 1; 
    Rect.Right = Width - 1; 

    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle 
    SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size 
    SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size 
} 

int main(void) 
{     
    SetWindow(80,40);
    int dx=1,i=5,l=0;

     while(l<5)
     {
        i=i+dx;
        if( (i<1) || (i>10)){ dx=-dx; l++;}

        SetWindow(10*i,5*i);
        Sleep(100);

     }

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();
  return 0;
}  
like image 145
Software_Designer Avatar answered Dec 20 '25 13:12

Software_Designer


Late to the party ...

As far as can be devised from MSDN and a few tests, the screen buffer can't be set smaller than the window's extent or the window's extent made bigger than the screen buffer.

One hack is to shrink the window to a minimal before changing the buffer size :

static void
set_console_size(HANDLE screen_buffer, SHORT width, SHORT height)
{
    assert(screen_buffer != NULL);
    assert(width > 0);
    assert(height > 0);

    COORD const size = { width, height };
    BOOL success;

    SMALL_RECT const minimal_window = { 0, 0, 1, 1 };
    success = SetConsoleWindowInfo(screen_buffer, TRUE, &minimal_window);
    CHECK(success);

    success = SetConsoleScreenBufferSize(screen_buffer, size);
    CHECK(success);

    SMALL_RECT const window = { 0, 0, size.X - 1, size.Y - 1 };
    success = SetConsoleWindowInfo(screen_buffer, TRUE, &window);
    CHECK(success);
}
like image 21
diapir Avatar answered Dec 20 '25 14:12

diapir