在以下代码中,任何时候调用CreateCompatibleDC时,生成的设备上下文只有两种颜色:黑色和白色。CreateCompatibleDC的结果只有两种颜色
case WM_PAINT:
{
PAINTSTRUCT ps;
ps.hdc=GetDC(g_CSkeletalViewerApp.m_hWnd);
ps.fErase=true;
RECT rc;
GetWindowRect(g_CSkeletalViewerApp.m_hWnd, &rc);
ps.rcPaint=rc;
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
HDC hdc=BeginPaint(hWnd,&ps);
HDC memdc=CreateCompatibleDC(hdc);
HBITMAP membm=CreateCompatibleBitmap(memdc,width,height);
SelectObject(memdc,membm);
for(int i=rc.left; i<rc.right; i++) {
for(int j=rc.top; j<rc.bottom; j++)
SetPixel(memdc,i,j,RGB((i+j)%255,(i+j)%255,(i+j)%255));
}
BitBlt(hdc,0,0,width,height,memdc,0,0,SRCCOPY);
DeleteDC(memdc);
EndPaint(hWnd,&ps);
}
break;
GetDeviceCaps(memdc,SIZEPALETTE)返回0同样为HDC,所以我不能手动更改调色板。两种设备上下文的颜色深度均为32位。在CreateCompatibleDC之后,GetLastError立即为0。 GetNearestColor(memdc,RGB(任何颜色))可以是黑色或白色。在任何设备上下文(不仅仅是hdc)上调用CreateCompatiobleDC之后,会出现同样的问题。
任何想法?
自九十年代初以来,这个问题几乎吸引了每个GDI新手。 :) –