BitBlt 仅捕获部分屏幕

BitBlt captures only partial screen

我正在尝试在 Windows10 下使用 GDI 捕获屏幕的方形部分。我是这样尝试的:

//Get screen DC
desktop = GetDC(NULL);

//Create a compatible bitmap of 32 x 32 px.
HDC hCaptureDC = CreateCompatibleDC(desktop);
HBITMAP captureBmp = CreateCompatibleBitmap(hCaptureDC, 32, 32);
SelectObject(hCaptureDC, captureBmp);

//loop:
BitBlt(hCaptureDC, 0, 0, 32, 32, desktop, sourceX, sourceY, SRCCOPY);
BitBlt(desktop, 0, 0, 32, 32, hCaptureDC, 0, 0, SRCCOPY);

所以我在 (sourceX, sourceY) 处捕获了一个 32x32 像素的矩形到位图中并将其绘制到屏幕上(左上角)。

但是,这会产生大部分黑色方块。只有 GDI 之前绘制的东西才会被捕获。我很确定第一个 BitBlt 是问题所在。如果我指定 WHITENESS 作为最后一个参数,我会得到一个白色方块,这正是我所期望的。所以从屏幕捕获到位图似乎是有问题的。

如果我使用

直接从一个屏幕复制到另一个屏幕
BitBlt(desktop, 0, 0, 32, 32, desktop, sourceX, sourceY, SRCCOPY);

,一切正常。

我也试过flag CAPTUREBLT.

如何将整个区域捕获到位图中?

这一行

HBITMAP captureBmp = CreateCompatibleBitmap(hCaptureDC, 32, 32);

应该是

HBITMAP captureBmp = CreateCompatibleBitmap(desktop, 32, 32);

原因在documentation page上解释为CreateCompatibleBitmap

When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context