Bagikan melalui


Menggulir Jendela Buffer Layar

Penting

Dokumen ini menjelaskan fungsionalitas platform konsol yang tidak lagi menjadi bagian dari peta jalan ekosistem kami. Kami tidak menyarankan penggunaan konten ini dalam produk baru, tetapi kami akan terus mendukung penggunaan yang ada untuk masa depan yang tidak terbatas. Solusi modern pilihan kami berfokus pada urutan terminal virtual untuk kompatibilitas maksimum dalam skenario lintas platform. Anda dapat menemukan informasi selengkapnya tentang keputusan desain ini di konsol klasik vs. dokumen terminal virtual kami.

Fungsi SetConsoleWindowInfo dapat digunakan untuk menggulir konten buffer layar di jendela konsol. Fungsi ini juga dapat mengubah ukuran jendela. Fungsi ini dapat menentukan sudut kiri atas dan kanan bawah baru dari jendela buffer layar konsol sebagai koordinat buffer layar absolut atau menentukan perubahan dari koordinat jendela saat ini. Fungsi gagal jika koordinat jendela yang ditentukan berada di luar batas buffer layar konsol.

Contoh berikut menggulir tampilan buffer layar konsol ke atas dengan memodifikasi koordinat jendela yang dikembalikan oleh fungsi GetConsoleScreenBufferInfo. Fungsi ini ScrollByAbsoluteCoord menunjukkan cara menentukan koordinat absolut, sementara ScrollByRelativeCoord fungsi menunjukkan cara menentukan koordinat relatif.

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

Menggulir Konten Buffer Layar

Menggulir Penyangga Layar