c++ 减小控制台尺寸

62o28rlo  于 2023-01-18  发布在  其他
关注(0)|答案(5)|浏览(267)

我在更改控制台大小时遇到问题。这是我的代码:

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 x100)

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

为什么?

s3fp2yjn

s3fp2yjn1#

SetConsoleScreenBufferSize更改控制台内部缓冲区的大小。更改它对控制台窗口范围没有影响。如果需要对控制台的可见部分(缓冲区)产生影响,请调用SetConsoleWindowInfo
窗口缓冲区不能小于内部缓冲区,减小窗口缓冲区也会减小内部缓冲区,但不是相反。
如果使用COORDs中的非法值调用SetConsoleScreenBufferSize(例如,高度/宽度太小),则会得到错误,通常为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;
}
7eumitmz

7eumitmz2#

派对迟到了...
根据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);
}
eivgtgni

eivgtgni3#

验证码:

system("mode 650");
lkaoscv7

lkaoscv74#

我于12年10月15日修改了“Software_Designer”提供的代码,并创建了一个命令行实用程序来设置控制台大小和滚动缓冲区。
我使用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;
}
iyfamqjs

iyfamqjs5#

/* So, tiny recap -- 
SetConsoleScreenBufferSize(): fails if buffer width and height are less than the width and height of the current screen window rectangle.

SetConsoleWindowInfo():  fails if the specified window rectangle is larger than the  boundaries of the screen buffer.

Setting the correct one 1st  (buffer or window rectangle) would solve much in a screen resize, but that's hard to know because calls to GetConsoleScreenBufferInfo() fail easily, and so you cannot always reliably discover the current screen rectangle and/or buffer size, and hence you cannot know if you'll be setting the screen rectangle larger or smaller, and in turn you can't always know which function to call 1st.

WORKAROUND: 

Sloppy solution but super-reliable... set the buffer 1st, and set it to something uncommonly large -- like 1000x1000.   Then set the window rectangle to the actual desired size.  Finally, set the window buffer to the actual desired size (matching the screen rectangle) */

int set_screen_size(int requested_wdth, int requested_hgt)
{

SMALL_RECT srWindowRect; /* hold the new console size */
COORD coordBuffer;
BOOL bsuccess = FALSE, wsuccess = FALSE;

srWindowRect.Left   = 0;
srWindowRect.Right  = requested_wdth;
srWindowRect.Top    = 0;
srWindowRect.Bottom = requested_hgt;

/*set buffer oversize -- like 1000x1000  (500x500 and less could do too)*/
coordBuffer.X = coordBuffer.Y = 1000;
bsuccess = SetConsoleScreenBufferSize(hConOut, coordBuffer);
wsuccess = SetConsoleWindowInfo(hConOut, TRUE, &srWindowRect);
coordScreen.X = requested_wdth;
coordScreen.Y = requested_hgt;
bsuccess = SetConsoleScreenBufferSize(hConOut, coordBuffer);

/* ... */

}

相关问题