减少控制台尺寸(Reducing console size)

2019-08-01 11:46发布

我买了改变控制台的大小的问题。 这是我的代码:

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;
}

这工作完全正常,当我尝试扩大控制台。 当一个参数小于前一个什么都不会发生。 怎么了?

@edit:一些测试,我注意到,该调整(降低)是可能的,如果我改变一个参数一次之后。 实施例(假定控制台是100×100)

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

为什么?!

Answer 1:

SetConsoleScreenBufferSize改变控制台的内部缓冲器的大小。 改变它的控制台窗口程度没有影响。 呼叫SetConsoleWindowInfo如果你需要在控制台(缓冲)的可见部分的效果的。

该窗口的缓冲区不能大于内部缓冲区小,减小也降低了内部缓冲区,而不是周围的其他方式。

如果调用SetConsoleScreenBufferSize在COORDS(如太少高/宽)非法值,那么你会得到一个错误,通常87 'invalid argument'.

试试这个代码:

#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;
}  


Answer 2:

迟到了......

至于可以从MSDN和几个测试来设计,画面缓冲器不能设置比窗口的程度或由比屏幕缓冲寄存器大窗口的程度更小。

一个黑客正在改变缓冲区大小之前缩小窗口到最小:

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);
}


Answer 3:

试试这个代码:

system("mode 650");  


Answer 4:

我修改由“Software_Designer”年10月15' 12所提供的码和创建的命令行实用程序来设置控制台大小和滚动缓冲器。

我用DEV C ++(编译它http://www.bloodshed.net/devcpp.html )。

可执行包括在https://sourceforge.net/projects/wa2l-wintools/ 。 我希望这有帮助。

/*
 * consolesize.cpp - set console size and buffer dimensions
 *
 * [00] 02.07.2016 CWa  Initial Version
 *
 * inspired by: http://stackoverflow.com/questions/12900713/reducing-console-size
 *
 */

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

using namespace std;


// SetWindow(Width,Height,WidthBuffer,HeightBuffer) -- set console size and buffer dimensions
//
void SetWindow(int Width, int Height, int WidthBuffer, int HeightBuffer) { 
    _COORD coord; 
    coord.X = WidthBuffer; 
    coord.Y = HeightBuffer; 

    _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 
}  // SetWindow



// main(Width,Height,WidthBuffer,HeightBuffer) -- main
//
int main(int argc, char *argv[]) {     
    int width = 80;
    int height = 25;
    int wbuffer = width + 200;
    int hbuffer = height + 1000;

    if ( argc == 5 ){
        width = atoi(argv[1]);
        height = atoi(argv[2]);
        wbuffer = atoi(argv[3]);
        hbuffer = atoi(argv[4]);
    } else if ( argc > 1 ) {
        cout << "Usage: " << argv[0] << " [ width height bufferwidth bufferheight ]" << endl << endl;
        cout << "  Where" << endl;
        cout << "    width            console width" << endl;
        cout << "    height           console height" << endl;
        cout << "    bufferwidth      scroll buffer width" << endl;
        cout << "    bufferheight     scroll buffer height" << endl;
        return 4;
    }    

    SetWindow(width,height,wbuffer,hbuffer);
    return 0;
} 


文章来源: Reducing console size