phd17621680432
A彡安静氵
2017-12-06 01:04

吕鑫老师的MFC透明动画技术为什么我实现不了

10
  • 对话框
  • 动画
  • mfc
  • visual studio

这是吕鑫老师CMemDC的封装类,可以用来实现图片的背景透明,但是我却实现不了,请大神看看因为什么,我用的是vs2010版本。

#pragma once
#include <afxwin.h>

class CGMemDC :public CDC
{
    CSize m_size;
public:
    CGMemDC(void)
    {
        m_size.cx = m_size.cy = 0;
    }


    BOOL LoadBitmap(UINT nBitmapID, CDC * pDC=NULL)
    {
        CBitmap bitmap;
        bitmap.LoadBitmapW(nBitmapID);
        BITMAP bm;
        bitmap.GetBitmap(&bm);
        m_size.cx = bm.bmWidth;
        m_size.cy = bm.bmHeight;
        CreateCompatibleDC(pDC);
        SelectObject(bitmap);

        return true;
    }


    CGMemDC(UINT nBitmapID, CDC * pDC=NULL)
    {
        LoadBitmap(nBitmapID,pDC);
    }


    BOOL LoadBitmap(LPCWSTR szBitmapFile, CDC * pDC=NULL)
    {
        HBITMAP hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),szBitmapFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
        BITMAP bm;
        GetObject(hBitmap,sizeof(bm),&bm);
        m_size.cx = bm.bmWidth;
        m_size.cy = bm.bmHeight;
        CreateCompatibleDC(pDC);
        SelectObject(hBitmap);
        return true;
    }


    CGMemDC(LPCWSTR szBitmapFile, CDC* pDC=NULL)
    {
        LoadBitmap(szBitmapFile,pDC);
    }


    BOOL Create(int cx, int cy, CDC* pDC=NULL)
    {
        CBitmap bitmap;
        bitmap.CreateCompatibleBitmap(pDC,cx,cy);
        m_size.cx = cx;
        m_size.cy = cy;
        CreateCompatibleDC(pDC);
        SelectObject(bitmap);
        return true;
    }


    CGMemDC(int cx, int cy, CDC* pDC=NULL)
    {
        Create(cx,cy,pDC);
    }


    BOOL DeleteDC(void)
    {
        if(!GetSafeHdc())
            return true;
        CBitmap *pBitmap = GetCurrentBitmap();
        pBitmap->DeleteObject();

        return CDC::DeleteDC();
    }

    ~CGMemDC(void)
    {
        DeleteDC();
    }

    inline int Width()
    {
        return m_size.cx;
    }

    inline int Height()
    {
        return m_size.cy;
    }

    /////增加显示透明位图的代码
    void BitTrans(
        int nXDest,
        int nYDest,
        int nWidthDest,
        int nHeightDest,
        CDC * pDC,
        int nXSrc,
        int nYSrc,
        COLORREF crTrans)
    {
        CGMemDC dcImage(nWidthDest,nHeightDest,pDC);
        CBitmap bmpMask;
        bmpMask.CreateBitmap(nWidthDest,nHeightDest,1,1,NULL);
        CDC dcMask;
        dcMask.CreateCompatibleDC(pDC);
        dcMask.SelectObject(bmpMask);

        dcImage.BitBlt(0,0,nWidthDest,nHeightDest,this,nXSrc,nYSrc,SRCCOPY);

        dcImage.SetBkColor(crTrans);

        dcMask.BitBlt(0,0,nWidthDest,nHeightDest,&dcImage,0,0,SRCCOPY);

        dcImage.SetBkColor(RGB(0,0,0));
        dcImage.SetTextColor(RGB(255,255,255));
        dcImage.BitBlt(0,0,nWidthDest,nHeightDest,&dcMask,0,0,SRCAND);

        pDC->SetBkColor(RGB(255,255,255));
        pDC->SetTextColor(RGB(0,0,0));
        pDC->BitBlt(nXDest,nYDest,nWidthDest,nHeightDest,&dcMask,0,0,SRCAND);
        pDC->BitBlt(nXDest,nYDest,nWidthDest,nHeightDest,&dcImage,0,0,SRCPAINT);
    }
};
 #include "GMemDC.h"
// CTrans 对话框

class CTrans : public CDialogEx
{
    CGMemDC m_dc;

dc.BitBlt函数是将原图输出,dc.BitTrans是透明输出,敲得代码跟书上一样,但是我的却没有实现,是因为版本不一样吗?

 void CTrans::OnPaint()
{
    CPaintDC dc(this); // device context for painting

    if(!m_dc)
        m_dc.LoadBitmapW(IDB_BITMAP3);

    dc.BitBlt(0,0,m_dc.Width(),m_dc.Height(),&m_dc,0,0,SRCCOPY);

    m_dc.BitTrans(m_dc.Width(),0,m_dc.Width(),m_dc.Height(),&dc,0,0,RGB(255,0,255));

}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐