首页 / 技术类 / 问题 / UpdateLayeredWindow在远程协助下失败的问题

UpdateLayeredWindow在远程协助下失败的问题

2013-01-18 11:33:00

最近遇到UpdateLayeredWindow在远程协助下会失败,但是GetLastError返回0。 后来看了http://blog.csdn.net/debehe/article/details/4767472,解决了,记一笔。 原代码:

 1void Update()
 2{
 3    CDC dc = GetDC(m_hWnd);
 4    CDC dcMemory;
 5    dcMemory.CreateCompatibleDC(dc);
 6
 7    CRect rect;
 8    GetClientRect(m_hWnd, &rect);
 9
10    CBitmap bmp = CreateCompatibleBitmap(dc, rect.Width(), rect.Height());
11    dcMemory.SelectBitmap(bmp);
12
13    {
14        Graphics g(dcMemory);
15        g.SetInterpolationMode(InterpolationModeNearestNeighbor);
16        g.SetPixelOffsetMode(PixelOffsetModeHalf);
17
18        // Rendering...
19    }
20
21    POINT pt = { 0, 0};
22    BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
23    SIZE szWindow = { rect.Width(), rect.Height() };
24    UpdateLayeredWindow(m_hWnd, dc, NULL, &szWindow, dcMemory, &pt, 0, &stBlend, ULW_ALPHA);
25}

新代码:

 1void Update()
 2{
 3    CDC dc = GetDC(m_hWnd);
 4    CDC dcMemory;
 5    dcMemory.CreateCompatibleDC(dc);
 6
 7    CRect rect;
 8    GetClientRect(m_hWnd, &rect);
 9
10    //
11    // 注意:一定要自己创建一个 32 位位图,不要 CreateCompatibleBitmap,否则远程协助下可能无法正常显示
12    //
13
14    BITMAPINFOHEADER bmih = { sizeof(BITMAPINFOHEADER) };
15    bmih.biWidth          = rect.Width();
16    bmih.biHeight         = rect.Height();
17    bmih.biPlanes         = 1;
18    bmih.biBitCount       = 32; // !注意:要 32 位
19    bmih.biCompression    = BI_RGB;
20
21    BYTE *pBits = NULL;
22    HBITMAP hBitmap = CreateDIBSection(NULL, (BITMAPINFO *)&bmih, 0, (LPVOID *)&pBits, NULL, 0);
23  
24    if (hBitmap == NULL)
25    {
26        return;
27    }
28
29    CBitmap bmp(hBitmap);
30    dcMemory.SelectBitmap(bmp);
31
32    {
33        Graphics g(dcMemory);
34        g.SetInterpolationMode(InterpolationModeNearestNeighbor);
35        g.SetPixelOffsetMode(PixelOffsetModeHalf);
36
37        // Rendering...
38    }
39
40    POINT pt = { 0, 0};
41    BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
42    SIZE szWindow = { rect.Width(), rect.Height() };
43    UpdateLayeredWindow(m_hWnd, dc, NULL, &szWindow, dcMemory, &pt, 0, &stBlend, ULW_ALPHA);
44}

首发:http://www.cppblog.com/Streamlet/archive/2013/01/18/197382.html



NoteIsSite/0.4