随笔-90  评论-947  文章-0  trackbacks-0

最近遇到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);

}

 

posted on 2013-01-18 11:33 溪流 阅读(2298) 评论(1)  编辑 收藏 引用 所属分类: WindowsIssues

评论:
# re: UpdateLayeredWindow在远程协助下失败的问题 2013-01-23 12:36 | 饭中淹
赞。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理