讀取和寫入字元和屬性區塊

重要

本文件說明已不再是生態系統 藍圖一部分的控制台平臺功能。 我們不建議您在新產品中使用此內容,但我們將繼續支持無限期的未來現有使用量。 我們慣用的新式解決方案著重於 虛擬終端機序列 ,以在跨平臺案例中達到最大相容性。 您可以在傳統 主控台與虛擬終端機 檔中找到此設計決策的詳細資訊。

ReadConsoleOutput 函式會將矩形的字元和色彩屬性數據區塊從控制台畫面緩衝區複製到目的地緩衝區。 函式會將目的地緩衝區視為CHAR_INFO結構的二維數位列。 同樣地, WriteConsoleOutput 函式會將矩形的字元和色彩屬性數據區塊從來源緩衝區複製到控制台畫面緩衝區。 如需讀取或寫入螢幕緩衝區儲存格矩形區塊的詳細資訊,請參閱 輸入和輸出方法

下列範例會使用 CreateConsoleScreenBuffer 函式來建立新的畫面緩衝區。 在 SetConsoleActiveScreenBuffer 函式將此設為使用中畫面緩衝區之後,會將 STDOUT 畫面緩衝區的前兩個數據列的字元和色彩屬性區塊複製到暫存緩衝區。 然後,數據會從暫存緩衝區複製到新的使用中畫面緩衝區。 當應用程式使用新的螢幕緩衝區完成時,它會呼叫 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;
}