关于CreateCompatibleDC
HDC hdc=GetDC(Paint);
PatBlt(hdc,0,0,100,100,WHITENESS);
HDC hdcHelp=CreateCompatibleDC(hdc);
PatBlt(hdcHelp,0,0,100,100,BLACKNESS);
BitBlt(hdc,0,0,100,100,hdcHelp,0,0,SRCCOPY);
DeleteDC(hdcHelp);
ReleaseDC(Paint,hdc);
就是这段代码 为什么执行过后Paint上是白色的,按道理应该是黑色的才对啊
[解决办法]
MSDN上说了:
When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required.
意思大概是:内存DC在创建后,只有1*1像素的尺寸,SelectObject选择bitmap以后才可以进行绘图
HDC hdc=GetDC(Paint);
PatBlt(hdc,0,0,100,100,WHITENESS);
HDC hdcHelp=CreateCompatibleDC(hdc);
RECT rect;
GetClientRect(Paint, &rect);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcHelp, hBitmap);
PatBlt(hdcHelp,0,0,100,100,BLACKNESS);
BitBlt(hdc,0,0,100,100,hdcHelp,0,0,SRCCOPY);
SelectObject(hdcHelp, hOldBitmap);
DeleteObject(hBitmap);
DeleteDC(hdcHelp);
ReleaseDC(Paint,hdc);