다음을 통해 공유


화면 버퍼의 창 스크롤

Important

이 문서에서는 더 이상 에코시스템 로드맵의 일부가 되지 않는 콘솔 플랫폼 기능에 대해 설명합니다. 이 콘텐츠를 신제품에서 사용하지 않는 것이 좋지만, 무기한 앞으로도 기존 사용을 계속 지원할 것입니다. 선호하는 최신 솔루션은 플랫폼 간 시나리오에서 최대 호환성을 위해 가상 터미널 시퀀스에 중점을 둡니다. 이 디자인 결정에 대한 자세한 내용은 클래식 콘솔과 가상 터미널 문서에서 확인할 수 있습니다.

SetConsoleWindowInfo 함수를 사용하여 콘솔 창에서 화면 버퍼의 내용을 스크롤할 수 있습니다. 이 함수는 창 크기를 변경할 수도 있습니다. 이 함수는 콘솔 화면 버퍼 창의 새 왼쪽 위와 오른쪽 아래 모서리를 절대 화면 버퍼 좌표로 지정하거나 현재 창 좌표의 변경 내용을 지정할 수 있습니다. 지정된 창 좌표가 콘솔 화면 버퍼의 경계를 벗어나면 함수가 실패합니다.

다음 예제에서는 GetConsoleScreenBufferInfo 함수에서 반환된 창 좌표를 수정하여 콘솔 화면 버퍼의 보기를 위로 스크롤합니다. 이 함수는 ScrollByAbsoluteCoord 절대 좌표를 지정하는 방법을 보여 주며, ScrollByRelativeCoord 함수는 상대 좌표를 지정하는 방법을 보여 줍니다.

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

HANDLE hStdout;

int ScrollByAbsoluteCoord(int iRows)
{
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    SMALL_RECT srctWindow;

    // Get the current screen buffer size and window position.

    if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
    {
        printf("GetConsoleScreenBufferInfo (%d)\n", GetLastError());
        return 0;
    }

    // Set srctWindow to the current window size and location.

    srctWindow = csbiInfo.srWindow;

    // Check whether the window is too close to the screen buffer top

    if ( srctWindow.Top >= iRows )
    {
        srctWindow.Top -= (SHORT)iRows;     // move top up
        srctWindow.Bottom -= (SHORT)iRows;  // move bottom up

        if (! SetConsoleWindowInfo(
                   hStdout,          // screen buffer handle
                   TRUE,             // absolute coordinates
                   &srctWindow))     // specifies new location
        {
            printf("SetConsoleWindowInfo (%d)\n", GetLastError());
            return 0;
        }
        return iRows;
    }
    else
    {
        printf("\nCannot scroll; the window is too close to the top.\n");
        return 0;
    }
}

int ScrollByRelativeCoord(int iRows)
{
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    SMALL_RECT srctWindow;

    // Get the current screen buffer window position.

    if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
    {
        printf("GetConsoleScreenBufferInfo (%d)\n", GetLastError());
        return 0;
    }

    // Check whether the window is too close to the screen buffer top

    if (csbiInfo.srWindow.Top >= iRows)
    {
        srctWindow.Top =- (SHORT)iRows;     // move top up
        srctWindow.Bottom =- (SHORT)iRows;  // move bottom up
        srctWindow.Left = 0;         // no change
        srctWindow.Right = 0;        // no change

        if (! SetConsoleWindowInfo(
                   hStdout,          // screen buffer handle
                   FALSE,            // relative coordinates
                   &srctWindow))     // specifies new location
        {
            printf("SetConsoleWindowInfo (%d)\n", GetLastError());
            return 0;
        }
        return iRows;
    }
    else
    {
        printf("\nCannot scroll; the window is too close to the top.\n");
        return 0;
    }
}

int main( void )
{
    int i;

    printf("\nPrinting twenty lines, then scrolling up five lines.\n");
    printf("Press any key to scroll up ten lines; ");
    printf("then press another key to stop the demo.\n");
    for(i=0; i<=20; i++)
        printf("%d\n", i);

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    if(ScrollByAbsoluteCoord(5))
        _getch();
    else return 0;

    if(ScrollByRelativeCoord(10))
        _getch();
    else return 0;
}

화면 버퍼의 콘텐츠 스크롤

화면 버퍼 스크롤