terminal icon indicating copy to clipboard operation
terminal copied to clipboard

SetConsoleScreenBufferInfoEx isn't working well on Window Terminal.

Open BDisp opened this issue 2 years ago • 0 comments

Windows Terminal version

1.15.3466.0

Windows build number

10.0.22621.963

Other Software

No response

Steps to reproduce

Please try to run this code:

#include <windows.h>
#include <stdio.h>

int main(void)
{
	HANDLE hStdout;
	CONSOLE_SCREEN_BUFFER_INFOEX csbiInfoEx{ 0 };
	csbiInfoEx.cbSize = sizeof(csbiInfoEx);
	SMALL_RECT winRect, srctScrollRect, srctClipRect;
	CHAR_INFO chiFill;
	COORD coordDest;
	COORD size = COORD{ 80, 25 };
	int i;

	printf("\nPrinting 20 lines for reference. ");
	printf("Notice that line 6 is discarded during scrolling.\n");
	for (i = 0; i <= 20; i++)
		printf("%d\n", i);

	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

	if (hStdout == INVALID_HANDLE_VALUE)
	{
		printf("GetStdHandle failed with %d\n", GetLastError());
		return 1;
	}

	// Get the screen buffer size. 

	if (!GetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx))
	{
		printf("GetConsoleScreenBufferInfoEx failed %d\n", GetLastError());
		return 1;
	}

	// Set the screen buffer size. 

	csbiInfoEx.dwSize = size;
	csbiInfoEx.srWindow = SMALL_RECT{ 0,0,size.X,size.Y };
	csbiInfoEx.dwMaximumWindowSize = size;

	if (!SetConsoleScreenBufferInfoEx(hStdout, &csbiInfoEx))
	{
		printf("SetConsoleScreenBufferInfoEx failed %d\n", GetLastError());
		return 1;
	}

	// Set the screen size. 

	winRect = { 0,0,(short)(size.X - 1),(short)(size.Y - 1) };

	if (!SetConsoleWindowInfo(hStdout, true, &winRect))
	{
		printf("SetConsoleWindowInfo failed %d\n", GetLastError());
		return 1;
	}

	// The scrolling rectangle is the bottom 15 rows of the 
	// screen buffer. 

	srctScrollRect.Top = csbiInfoEx.dwSize.Y - 16;
	srctScrollRect.Bottom = csbiInfoEx.dwSize.Y - 1;
	srctScrollRect.Left = 0;
	srctScrollRect.Right = csbiInfoEx.dwSize.X - 1;

	// The destination for the scroll rectangle is one row up. 

	coordDest.X = 0;
	coordDest.Y = csbiInfoEx.dwSize.Y - 17;

	// The clipping rectangle is the same as the scrolling rectangle. 
	// The destination row is left unchanged. 

	srctClipRect = srctScrollRect;

	// Fill the bottom row with green blanks. 

	chiFill.Attributes = BACKGROUND_GREEN | FOREGROUND_RED;
	chiFill.Char.UnicodeChar = ' ';

	// Scroll up one line. 

	if (!ScrollConsoleScreenBuffer(
		hStdout,         // screen buffer handle 
		&srctScrollRect, // scrolling rectangle 
		&srctClipRect,   // clipping rectangle 
		coordDest,       // top left destination cell 
		&chiFill))       // fill character and color
	{
		printf("ScrollConsoleScreenBuffer failed %d\n", GetLastError());
		return 1;
	}

	(void)getchar(); //Explicitly ignore return value.

	return 0;
}

Expected Behavior

With Windows Console Host I get the right size:

imagem

Actual Behavior

With Windows Terminal I don't get the right size screen as expected. See the point missing after the word scrolling. Look at the green blanks filled at the bottom which occupies all the incorrect console size. If I'm doing something wrong, please let me know. Thanks.

imagem

BDisp avatar Dec 15 '22 17:12 BDisp