减少控制台大小 [英] 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:在一些测试后,我注意到,如果我一次更改一个参数,那么调整大小(减少)是可能的。示例(假设控制台为100x100)
@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?!
推荐答案
SetConsoleScreenBufferSize
更改控制台内部缓冲区的大小。
更改它对控制台窗口范围没有影响。
如果您需要在控制台(缓冲区)的可见部分需要效果,请调用 SetConsoleWindowInfo
。
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.
如果调用 SetConsoleScreenBufferSize
在COORDS中有非法值(例如太小的高度/宽度),那么你得到一个
错误,通常是 87'invalid argument'。
If you call SetConsoleScreenBufferSize
with illegal value in COORDS (e.g. too little height/width) then you get an
error, usually 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;
}
这篇关于减少控制台大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!