画面バッファーのウィンドウのスクロール

重要

このドキュメントでは、エコシステム ロードマップの一部ではなくなったコンソール プラットフォームの機能について説明します。 このコンテンツを新しい製品で使用することはお勧めしませんが、今後も既存の使用をサポートし続けます。 推奨される最新のソリューションでは、クロスプラットフォーム シナリオでの互換性を最大限に高める仮想ターミナル シーケンスに重点を置いています。 この設計決定の詳細については、クラシック コンソールと仮想ターミナルのドキュメントを参照してください。

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;
}

画面バッファーの内容のスクロール

スクリーン バッファーのスクロール