问题描述
在下面的代码中,每次调用CreateCompatibleDC时,生成的设备上下文只有两种颜色:
In the following code, anytime CreateCompatibleDC is called, the resulting device context only has two colors: black and white.
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之后,都会发生相同的问题。
GetDeviceCaps(memdc,SIZEPALETTE) returns 0. Same for hdc, so I can't change the palette manually. The color depth for both device contexts is 32 bits. GetLastError is 0 immediately after CreateCompatibleDC. GetNearestColor(memdc,RGB(any color)) is either black or white. After calling CreateCompatiobleDC on any device context (not just hdc), the same problem occurs.
有任何想法吗?
推荐答案
更改此:
HBITMAP membm=CreateCompatibleBitmap(memdc,width,height);
到此:
HBITMAP membm=CreateCompatibleBitmap(hdc,width,height);
创建兼容的DC时,它是用位图创建的-但是该位图始终为1x1单色位图(即,一个黑色或白色像素),无论它兼容哪种DC。
When you create a compatible DC, it's created with a bitmap--but that bitmap is always a 1x1 monochrome bitmap (i.e., a single pixel that's either black or white), regardless of what sort of DC it's compatible with.
因此,如果创建兼容的位图
As a result, if you create a bitmap compatible with that DC, you'll get a larger monochrome bitmap.
但是,如果您创建与原始 DC兼容的位图,则您将获得所需大小的 和原始DC的颜色深度的位图。
If, however, you create a bitmap compatible with the original DC, then you'll get a bitmap of the requested size and the color depth of the original DC.
这篇关于CreateCompatibleDC的结果只有两种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!