
最近遇到UpdateLayeredWindow在远程协助下会失败,但是GetLastError返回0。
后来看了http://blog.csdn.net/debehe/article/details/4767472,解决了,记一笔。
原代码:
void Update()
{
CDC dc = GetDC(m_hWnd);
CDC dcMemory;
dcMemory.CreateCompatibleDC(dc);
CRect rect;
GetClientRect(m_hWnd, &rect);
CBitmap bmp = CreateCompatibleBitmap(dc, rect.Width(), rect.Height());
dcMemory.SelectBitmap(bmp);
{
Graphics g(dcMemory);
g.SetInterpolationMode(InterpolationModeNearestNeighbor);
g.SetPixelOffsetMode(PixelOffsetModeHalf);
// Rendering...
}
POINT pt = { 0, 0};
BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
SIZE szWindow = { rect.Width(), rect.Height() };
UpdateLayeredWindow(m_hWnd, dc, NULL, &szWindow, dcMemory, &pt, 0, &stBlend, ULW_ALPHA);
}
新代码:
void Update()
{
CDC dc = GetDC(m_hWnd);
CDC dcMemory;
dcMemory.CreateCompatibleDC(dc);
CRect rect;
GetClientRect(m_hWnd, &rect);
//
// 注意:一定要自己创建一个 32 位位图,不要 CreateCompatibleBitmap,否则远程协助下可能无法正常显示
//
BITMAPINFOHEADER bmih = { sizeof(BITMAPINFOHEADER) };
bmih.biWidth = rect.Width();
bmih.biHeight = rect.Height();
bmih.biPlanes = 1;
bmih.biBitCount = 32; // !注意:要 32 位
bmih.biCompression = BI_RGB;
BYTE *pBits = NULL;
HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO *)&bmih, 0, (LPVOID *)&pBits, NULL, 0);
if (hBitmap == NULL)
{
return;
}
CBitmap bmp(hBitmap);
dcMemory.SelectBitmap(bmp);
{
Graphics g(dcMemory);
g.SetInterpolationMode(InterpolationModeNearestNeighbor);
g.SetPixelOffsetMode(PixelOffsetModeHalf);
// Rendering...
}
POINT pt = { 0, 0};
BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
SIZE szWindow = { rect.Width(), rect.Height() };
UpdateLayeredWindow(m_hWnd, dc, NULL, &szWindow, dcMemory, &pt, 0, &stBlend, ULW_ALPHA);
}
首发:http://www.cppblog.com/Streamlet/archive/2013/01/18/197382.html




近期评论