文字と属性のブロックの読み取りと書き込み

重要

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

ReadConsoleOutput 関数は、四角形の文字ブロックと色属性データをコンソール スクリーン バッファーからコピー先バッファーにコピーします。 この関数は、コピー先バッファーをCHAR_INFO 構造体の 2 次元配列として扱います。 同様に、 WriteConsoleOutput 関数は、四角形の文字ブロックと色属性データをソース バッファーからコンソール スクリーン バッファーにコピーします。 スクリーン バッファー セルの四角形ブロックからの読み取りまたは書き込みの詳細については、「入出力メソッド」を参照してください。

次の例では、 CreateConsoleScreenBuffer 関数を使用して新しいスクリーン バッファーを作成します。 SetConsoleActiveScreenBuffer 関数がこれをアクティブなスクリーン バッファーにすると、文字と色属性のブロックが STDOUT スクリーン バッファーの上の 2 行から一時バッファーにコピーされます。 その後、データは一時バッファーから新しいアクティブなスクリーン バッファーにコピーされます。 アプリケーションは、新しいスクリーン バッファーを使用して完了すると、SetConsoleActiveScreenBuffer を呼び出して、元の STDOUT スクリーン バッファーを復元します。

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

int main(void)
{
    HANDLE hStdout, hNewScreenBuffer;
    SMALL_RECT srctReadRect;
    SMALL_RECT srctWriteRect;
    CHAR_INFO chiBuffer[160]; // [2][80];
    COORD coordBufSize;
    COORD coordBufCoord;
    BOOL fSuccess;

    // Get a handle to the STDOUT screen buffer to copy from and
    // create a new screen buffer to copy to.

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    hNewScreenBuffer = CreateConsoleScreenBuffer(
       GENERIC_READ |           // read/write access
       GENERIC_WRITE,
       FILE_SHARE_READ |
       FILE_SHARE_WRITE,        // shared
       NULL,                    // default security attributes
       CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
       NULL);                   // reserved; must be NULL
    if (hStdout == INVALID_HANDLE_VALUE ||
            hNewScreenBuffer == INVALID_HANDLE_VALUE)
    {
        printf("CreateConsoleScreenBuffer failed - (%d)\n", GetLastError());
        return 1;
    }

    // Make the new screen buffer the active screen buffer.

    if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )
    {
        printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
        return 1;
    }

    // Set the source rectangle.

    srctReadRect.Top = 0;    // top left: row 0, col 0
    srctReadRect.Left = 0;
    srctReadRect.Bottom = 1; // bot. right: row 1, col 79
    srctReadRect.Right = 79;

    // The temporary buffer size is 2 rows x 80 columns.

    coordBufSize.Y = 2;
    coordBufSize.X = 80;

    // The top left destination cell of the temporary buffer is
    // row 0, col 0.

    coordBufCoord.X = 0;
    coordBufCoord.Y = 0;

    // Copy the block from the screen buffer to the temp. buffer.

    fSuccess = ReadConsoleOutput(
       hStdout,        // screen buffer to read from
       chiBuffer,      // buffer to copy into
       coordBufSize,   // col-row size of chiBuffer
       coordBufCoord,  // top left dest. cell in chiBuffer
       &srctReadRect); // screen buffer source rectangle
    if (! fSuccess)
    {
        printf("ReadConsoleOutput failed - (%d)\n", GetLastError());
        return 1;
    }

    // Set the destination rectangle.

    srctWriteRect.Top = 10;    // top lt: row 10, col 0
    srctWriteRect.Left = 0;
    srctWriteRect.Bottom = 11; // bot. rt: row 11, col 79
    srctWriteRect.Right = 79;

    // Copy from the temporary buffer to the new screen buffer.

    fSuccess = WriteConsoleOutput(
        hNewScreenBuffer, // screen buffer to write to
        chiBuffer,        // buffer to copy from
        coordBufSize,     // col-row size of chiBuffer
        coordBufCoord,    // top left src cell in chiBuffer
        &srctWriteRect);  // dest. screen buffer rectangle
    if (! fSuccess)
    {
        printf("WriteConsoleOutput failed - (%d)\n", GetLastError());
        return 1;
    }
    Sleep(5000);

    // Restore the original active screen buffer.

    if (! SetConsoleActiveScreenBuffer(hStdout))
    {
        printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
        return 1;
    }

    return 0;
}