10行分スクロール

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

const int WINDOW_HEIGHT = 10;
const int TOTAL_LINES = 20;

void setConsoleWindowSize(int width, int height) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SMALL_RECT rect = { 0, 0, static_cast<SHORT>(width - 1), static_cast<SHORT>(height - 1) };
    SetConsoleWindowInfo(hConsole, TRUE, &rect);
}

void setConsoleCursorPosition(int x, int y) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
    SetConsoleCursorPosition(hConsole, pos);
}

void displayData(int startIndex) {
    system("cls"); // 画面をクリア

    for (int i = startIndex; i < startIndex + WINDOW_HEIGHT; ++i) {
        std::cout << "Line " << i + 1 << std::endl;
    }
}

int main() {
    int scrollPosition = 0;

    // コンソールウィンドウのサイズを設定
    setConsoleWindowSize(80, WINDOW_HEIGHT);

    while (true) {
        displayData(scrollPosition);

        // キー入力を待機し、上下キーでスクロール
        int key = _getch();
        if (key == 224) { // 上下キーの場合
            key = _getch(); // 2回_getch()を呼び出してキーコードを取得
            if (key == 72 && scrollPosition > 0) { // 上キー
                --scrollPosition;
            }
            else if (key == 80 && scrollPosition < TOTAL_LINES - WINDOW_HEIGHT) { // 下キー
                ++scrollPosition;
            }
        }
    }

    return 0;
}