您的位置:首页 > 其它

GDI+ / PNG 实现半透明窗口,正常情况下没有问题 将系统设置为16位色后UpdateLayeredWindow函数失败,GetLastError返回8(ERROR_NOT_ENOUGH_MEMORY)

2009-07-24 15:25 639 查看
问题出在你创建的Bitmap上,一般情况下用CreateCompatibleBitmap来创建,但是这时候创建的Bitmap跟DC走了,是16位色的,这样就会丢失了Alpha通道。所以应该这样:

RECT rtWnd;
hWnd = GetSafeHwnd();
if(hWnd == NULL)
break;
if(!::GetWindowRect(hWnd, &rtWnd))
break;

hWndDC = ::GetDC(hWnd);
if(hWndDC == NULL)
break;

hMemDC = ::CreateCompatibleDC(hWndDC);
if(hMemDC == NULL)
break;

//hMemBitmap = ::CreateCompatibleBitmap(hWndDC, rtWnd.right - rtWnd.left, rtWnd.bottom - rtWnd.top); ////这样就会失败

BYTE* pBits = NULL;
BITMAPINFOHEADER bmih = { sizeof (BITMAPINFOHEADER) };

bmih.biWidth = rtWnd.right - rtWnd.left;
bmih.biHeight = rtWnd.bottom - rtWnd.top ;
bmih.biPlanes = 1 ;
bmih.biBitCount = 32; //这里一定要是32
bmih.biCompression = BI_RGB ;
bmih.biSizeImage = 0 ;
bmih.biXPelsPerMeter = 0 ;
bmih.biYPelsPerMeter = 0 ;
bmih.biClrUsed = 0 ;
bmih.biClrImportant = 0 ;

hMemBitmap = CreateDIBSection (NULL, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (VOID**)&pBits, NULL, 0) ;

if(hMemBitmap == NULL)
break;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: