UpdateLayeredWindow()失败,GetLastError()返回0

来源:互联网 发布:tomcat项目多域名访问 编辑:程序博客网 时间:2024/05/22 10:47
在绘制分层窗口时,使用了如下代码段绘制分层窗口:
HDC hdcMem = ::CreateCompatibleDC();
HBITMAP hbmpMem = ::CreateCompatibleBitmap();
::SelectObject(hdcMem, hbmpMem);
/*****  paint on memory dc hdcMem ****/
UpdateLayeredWindow();


使用一段时间后,发现在有一台电脑上面没有绘制出分层窗口。调试发现是因为UpdateLayeredWindow失败了。
在网上查了一下,没有找到出错的具体原因是为什么(一说是windows xp 的一个bug)。但是找到了一个替代方案:
不使用CreateCompatibleBitmap创建兼容位图,而是使用CreateDIBSection创建。

代码如下:


#if 0
// 老的方法
// 此方法不能保证后续的UpdateLayeredWindow方法在所有系统上都生效
HBITMAP hBitMap = 
::CreateCompatibleBitmap(
hdcTemp, 
rcWindow.Width(),
rcWindow.Height());
#else
// 新的方法
BITMAPINFO stBmpInfo = {0};
stBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);   // the number of bytes required by the structure
stBmpInfo.bmiHeader.biWidth = rcWindow.Width();                    // the width of the bitmap, in pixels
stBmpInfo.bmiHeader.biHeight = rcWindow.Height();                  // the height of the bitmap, in pixels
stBmpInfo.bmiHeader.biPlanes = 1;                                             // The count of color planes is always 1
stBmpInfo.bmiHeader.biBitCount = 32;                                        // the number of bits-per-pixel,必须是32位,才能有透明效果
stBmpInfo.bmiHeader.biCompression = BI_RGB;                       // An uncompressed format
stBmpInfo.bmiHeader.biClrUsed = 0;                                          // the number of color indexes in the color table 
                                                                                                    // that are actually used by the bitmap
stBmpInfo.bmiHeader.biSizeImage = 0;                                      // the size, in bytes, of the image
                                                                                                    // This may be set to zero for BI_RGB bitmaps.
HBITMAP hBitMap = 
::CreateDIBSection(
hdcTemp,
&stBmpInfo,
DIB_RGB_COLORS,
NULL,
NULL,
0);

#endif


参考: UpdateLayeredWindow()失败,GetLastError()返回0


原创粉丝点击