CreateCompatibleBitmap() returns black HBITMAP

北战南征 提交于 2020-04-30 07:46:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!