Clearing console output/scroll buffer, without losing the scrollbar

Windows specific forum
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Clearing console output/scroll buffer, without losing the scrollbar

Post by PBJim »

I experimented with ways to clear the console scrollback buffer. I wasn't able to find an API function to just clear the buffer alone, so therefore the way I managed to do it successfully, is to set the buffer to the same size as the screen rows — in other words 30 rows. Consequently there's nothing to scroll back, so that's fine.

Nothing's ever easy though. The problem with this is that since there is no scrollable history, I lose the vertical scrollbar. This has an unpleasant effect on our application, because the scrollbar's disappearance makes the window width resize. Notably, the application has two modes :

1. Scrollable past history using the mouse wheel, or the scrollbar
2. A special function in which we don't want the scrollback to work, as it disrupts screen drawing and presentation (however we need to retain the vertical scrollbar, otherwise the window width decreases and looks unsatisfactory)

Does anyone know if there's a way to clear the output buffer while retaining the scrollbar? Or perhaps even a better way. Thanks.

Code: Select all

OpenConsole()
EnableExplicit

Define csbi.CONSOLE_SCREEN_BUFFER_INFO
Define hOutput
Define ok, a
Define newbuffer.COORD

newbuffer\x = 120
newbuffer\y = 31                                ; <------ Change screen buffer size to 30 and we lose the vert. scrollbar

hOutput = GetStdHandle_( #STD_OUTPUT_HANDLE )

ok = SetConsoleScreenBufferSize_(hOutput, newbuffer\x | (newbuffer\y << 16))
PrintN("[ OK = " + ok + " ]")

GetConsoleScreenBufferInfo_(hOutput, @csbi.CONSOLE_SCREEN_BUFFER_INFO)

ok = SetConsoleWindowInfo_(hOutput, 1, @csbi\srWindow)
PrintN("[ OK = " + ok + " ]")

For a= 1 To 28
  PrintN("" + a)
Next

Input()