问题
Hello Stack Overflow users. It seems that I am not using CreateCompatibleBitmap() properly in the following code:
#include <windows.h>
using namespace std;
int main() {HDC hdc=GetDC(HWND_DESKTOP); HDC MemDC=CreateCompatibleDC(hdc);
HBITMAP hBit=CreateCompatibleBitmap(hdc,1366,768);
SelectObject(MemDC,hBit);
BitBlt(hdc,0,0,1366,768,MemDC,0,0,SRCCOPY); //Screen turns black
DeleteObject(hBit);
ReleaseDC(HWND_DESKTOP,hdc);
ReleaseDC(NULL,MemDC);
DeleteDC(MemDC);
DeleteDC(hdc);
}
I thought CreateCompatibleBitmap() was to return a 1366x768 section of the Desktop DC, but a black screen is displayed after BitBlt(). Instead of using CreateCompatibleBitmap I load an bitmap file into hBit and everything is as desired, so I guess the problem is with CreateCompatibleBitmap() only. Am I using this function properly? Is there something I am not doing that I am supposed to do?
回答1:
CreateCompatibleBitmap
created bitmap for you, but it's not supposed to be initialized with a part of desktop or anything else. You blit it into desktop without initializing, hence blackness is not something unexpected. If you want it to hold desktop image, you need to blit in reverse direction, from desktop DC into DC with the created bitmap selected.
来源:https://stackoverflow.com/questions/16425695/createcompatiblebitmap-returns-black-hbitmap