【发布时间】:2011-10-31 08:36:19
【问题描述】:
在以下代码中,只要调用 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 位。 GetLastError 在 CreateCompatibleDC 之后立即为 0。 GetNearestColor(memdc,RGB(any color)) 是黑色或白色。在任何设备上下文(不仅仅是 hdc)上调用 CreateCompatiobleDC 后,都会出现同样的问题。
有什么想法吗?
【问题讨论】:
-
自九十年代初以来,几乎每个 GDI 新手都会遇到这个问题。 :)