none
c++ 关于镂空图形剪切板的问题 RRS feed

  • 问题

  • 函数是这样的:

    void CTransparentImage::DrawTransBitmap(CDC *pDC, HBITMAP hBitmap, CRect destRect, CRect srcRect, COLORREF maskColor)
    {
    CRect rect = destRect;
    if(hBitmap == NULL) return;

    CDC bufferDC;
    bufferDC.CreateCompatibleDC(pDC);
    CBitmap  bufferBitmap;
    bufferBitmap.CreateCompatibleBitmap(pDC, destRect.Width(), destRect.Height());
    CBitmap* pOldBufferBitmap = bufferDC.SelectObject(&bufferBitmap);
    bufferDC.FillSolidRect(&rect, maskColor);

    CDC maskDC;
    maskDC.CreateCompatibleDC(pDC);
    CBitmap  maskBitmap;
    maskBitmap.CreateBitmap(destRect.Width(), destRect.Height(), 1, 1, NULL);
    CBitmap* pOldMaskBitmap = maskDC.SelectObject(&maskBitmap) ;

    CDC memoryDC;
    memoryDC.CreateCompatibleDC(pDC);
    CBitmap* pOldMemoryBitmap = memoryDC.SelectObject(CBitmap::FromHandle(hBitmap));
    bufferDC.StretchBlt(destRect.left, destRect.top, destRect.Width(), destRect.Height(),
      &memoryDC, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(), SRCCOPY);
    memoryDC.SelectObject(pOldMemoryBitmap);

    COLORREF oldBackColor = bufferDC.SetBkColor(maskColor);
    maskDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &bufferDC, 0, 0, SRCCOPY);
    bufferDC.SetBkColor(oldBackColor);

    #define ROP4_TRANSPARENTBLIT  0xCCAA0020
    #define SRCMASK      0x00220326
    if(!pDC->MaskBlt(0, 0, rect.Width(), rect.Height(), &bufferDC, 0, 0, maskBitmap, 0, 0, ROP4_TRANSPARENTBLIT))
    {
      CDC copyDC ;
      copyDC.CreateCompatibleDC(pDC) ;
      CBitmap copyBitmap;
      copyBitmap.CreateCompatibleBitmap(pDC, destRect.Width(), destRect.Height());
      CBitmap* pOldCopyBitmap = copyDC.SelectObject(&copyBitmap);

      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), pDC,  0, 0, SRCCOPY);
      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &maskDC,   0, 0, SRCAND);
      bufferDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &maskDC,   0, 0, SRCMASK);
      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &bufferDC, 0, 0, SRCPAINT);
      pDC->BitBlt(0, 0, destRect.Width(), destRect.Height(), &copyDC,   0, 0, SRCCOPY);

      copyDC.SelectObject(pOldCopyBitmap);
    }

    maskDC.SelectObject(pOldMaskBitmap);
    bufferDC.SelectObject(pOldBufferBitmap);
    }

    调用的时候:

    DrawTransBitmap(&dcCompatible,hOldBitmap, lpRect, lpRect,RGB(51, 51, 102));

    //这里我想把红色设置为透明,加了这句以后可以正常编译和运行,可是结果没有如期变为透明色

    {
    CRect rect = destRect;
    if(hBitmap == NULL) return;

    CDC bufferDC;
    bufferDC.CreateCompatibleDC(pDC);
    CBitmap  bufferBitmap;
    bufferBitmap.CreateCompatibleBitmap(pDC, destRect.Width(), destRect.Height());
    CBitmap* pOldBufferBitmap = bufferDC.SelectObject(&bufferBitmap);
    bufferDC.FillSolidRect(&rect, maskColor);

    CDC maskDC;
    maskDC.CreateCompatibleDC(pDC);
    CBitmap  maskBitmap;
    maskBitmap.CreateBitmap(destRect.Width(), destRect.Height(), 1, 1, NULL);
    CBitmap* pOldMaskBitmap = maskDC.SelectObject(&maskBitmap) ;

    CDC memoryDC;
    memoryDC.CreateCompatibleDC(pDC);
    CBitmap* pOldMemoryBitmap = memoryDC.SelectObject(CBitmap::FromHandle(hBitmap));
    bufferDC.StretchBlt(destRect.left, destRect.top, destRect.Width(), destRect.Height(),
      &memoryDC, srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(), SRCCOPY);
    memoryDC.SelectObject(pOldMemoryBitmap);

    COLORREF oldBackColor = bufferDC.SetBkColor(maskColor);
    maskDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &bufferDC, 0, 0, SRCCOPY);
    bufferDC.SetBkColor(oldBackColor);

    #define ROP4_TRANSPARENTBLIT  0xCCAA0020
    #define SRCMASK      0x00220326
    if(!pDC->MaskBlt(0, 0, rect.Width(), rect.Height(), &bufferDC, 0, 0, maskBitmap, 0, 0, ROP4_TRANSPARENTBLIT))
    {
      CDC copyDC ;
      copyDC.CreateCompatibleDC(pDC) ;
      CBitmap copyBitmap;
      copyBitmap.CreateCompatibleBitmap(pDC, destRect.Width(), destRect.Height());
      CBitmap* pOldCopyBitmap = copyDC.SelectObject(&copyBitmap);

      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), pDC,  0, 0, SRCCOPY);
      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &maskDC,   0, 0, SRCAND);
      bufferDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &maskDC,   0, 0, SRCMASK);
      copyDC.BitBlt(0, 0, destRect.Width(), destRect.Height(), &bufferDC, 0, 0, SRCPAINT);
      pDC->BitBlt(0, 0, destRect.Width(), destRect.Height(), &copyDC,   0, 0, SRCCOPY);

      copyDC.SelectObject(pOldCopyBitmap);
    }

    maskDC.SelectObject(pOldMaskBitmap);
    bufferDC.SelectObject(pOldBufferBitmap);
    }

    DrawTransBitmap()函数是一个没有返回值的函数,我想要得到镂空后的hOldBitmap对象,DrawTransBitmap()里面的参数我不知道怎么写.


    2013年9月29日 7:02

全部回复

  • 你好:

    我建议你可以看看C++透明位图。参考连接:http://htcneil.w51.mc-test.com/Neil/1193.html

    如果对于这个问题,你还有什么不明白的地方,请提供详细信息(比如截图你想要的效果图)随时通知我们。


    Sunny Cao
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2013年9月30日 9:45
    版主
  • 你好:

    我建议你可以看看C++透明位图。参考连接:http://htcneil.w51.mc-test.com/Neil/1193.html

    如果对于这个问题,你还有什么不明白的地方,请提供详细信息(比如截图你想要的效果图)随时通知我们。


    Sunny Cao
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    我是看过那个链接的,然后根据那个连接里面的写的代码,问题运行没问题,结果不是我要的,我要的是截图到剪切板的时候去掉某一个颜色,比方白色或者黑色.

    DrawTransBitmap(&dcCompatible,hOldBitmap, lpRect, lpRect,RGB(51, 51, 102));  这么调用,里面填的参数是我想当然的写进去的,我自己也不知道对不对就填了那些参数,具体的我也不明白,所以特此寻求帮助.


    2013年10月10日 8:09
  • 你好:

    DrawTransBitmap(&dcCompatible,hOldBitmap, lpRect, lpRect,RGB(51, 51, 102));

    //这里我想把红色设置为透明,加了这句以后可以正常编译和运行,可是结果没有如期变为透明色

    这句话是没起到任何作用么,如果是让红色变成了其他颜色而不是透明色,我想可能你的RGB值设置有问题。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月11日 2:15
    版主
  • http://yycred.com/c11/tmp.zip

    这是整个项目,里面有MAKEFILE,楼主帮我看看



    代码在 CatchScreenDlg.cpp 的 406 --416 行
    2013年10月11日 8:07
  • 你好:

    根据我们论坛支持的政策,你可以把你的project放在skydriver上。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月11日 8:47
    版主
  • 你好:

    根据我们论坛支持的政策,你可以把你的project放在skydriver上。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    https://skydrive.live.com/?v=FirstRunView#!/?cid=60B6E3057905387C&id=60B6E3057905387C!192!cid=60B6E3057905387C&id=60B6E3057905387C!241

    放skydriver上了,不知道怎么发文件链接地址,这个地址不知道你们能不能打开

    2013年10月11日 13:58
  • 有解吗?
    2014年1月15日 2:55