MFC透明贴图问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36675830/article/details/78259393

对于透明贴图从网上找了很多资料,但是只有收获甚小,还是从一位老铁的开源项目中学到透明贴图的解决方案:

(一)首先说下我的搜索结果吧,我的解决方案中是用一个函数搞定的:
TransparentBlt(pDC->m_hDC,point.x, point.y, 109, 86,memDC.m_hDC,0, 0, 109, 86, RGB(0, 0, 0));该函数的参数原型为:

BOOL TransparentBlt(
  HDC hdcDest,        // handle to destination DC
  int nXOriginDest,   // x-coord of destination upper-left corner
  int nYOriginDest,   // y-coord of destination upper-left corner
  int nWidthDest,     // width of destination rectangle
  int hHeightDest,    // height of destination rectangle
  HDC hdcSrc,         // handle to source DC
  int nXOriginSrc,    // x-coord of source upper-left corner
  int nYOriginSrc,    // y-coord of source upper-left corner
  int nWidthSrc,      // width of source rectangle
  int nHeightSrc,     // height of source rectangle
  UINT crTransparent  // color to make transparent
);

来自MSDN ,参数有点多,自行百度,感觉这个方法虽然简单,但是总感觉不是那么好。
(二)老铁的方案其实也算是一种较好的方案吧,使用CImageListk类,该类是一个存储图像的链表类,该类成员函数较多,但是用到的也就那么几个,首先是Create函数,Add函数,Draw函数,下面就针对该类的使用进行介绍:
(1) 首先我们需要一个dc,这里就用双缓冲吧

CDC *pDC = GetDC();
CRect rect;
//获得客户区矩形区域
GetClientRect(&rect);
//内存缓冲CDC
CDC cdc;
//内存中承载临时图像的缓冲位图
CBitmap* cacheBitmap = new CBitmap;
CBitmap bm;
bm.LoadBitmapW(IDB_BITMAP2);
BITMAP m;
bm.GetBitmap(&m);
//用当前设备CDC初始化缓冲CDC
cdc.CreateCompatibleDC(pDC);
//获得位图
CBitmap *pOldBit = cdc.SelectObject(&bm);

(2)定义一个CImageList链表类并创建,

//创建一个该类的链表  cx,cy 图片的宽度
if (!images.Create(120, 90, ILC_COLOR24 | ILC_MASK, 14,0))
    return;

(3)加入位图

CBitmap me;
//加载一个将要存储到链表的位图
if (!me.LoadBitmapW(IDB_BITMAP1))
    return;
images.Add(&me, RGB(0, 0, 0));

(4)调用Draw()函数画出图像(其实只是把图像放到dc中,还不能画出)

images.Draw(&cdc, 1, mPoint, ILD_TRANSPARENT);

(5)画出并释放链表对象

pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &cdc, 0, 0, SRCCOPY);
//释放二级cdc
cdc.DeleteDC();
cacheBitmap->DeleteObject();
ReleaseDC(pDC);
images.DeleteImageList();//千万不能忘,否则内存泄漏,崩溃
展开阅读全文

CImage透明贴图问题

10-30

void TransparentPNG(CImage *png)rnrn for (int i = 0; i GetWidth(); i++)rn rn for (int j = 0; j GetHeight(); j++)rn rn unsigned char* pucColor = reinterpret_cast(png->GetPixelAddress(i, j));rn pucColor[0] = pucColor[0] * pucColor[3] / 255;rn pucColor[1] = pucColor[1] * pucColor[3] / 255;rn pucColor[2] = pucColor[2] * pucColor[3] / 255;rn rn rnrnvoid CChildView::OnPaint() rnrn CPaintDC dc(this); // 用于绘制的设备上下文rn CDC *cDC = this->GetDC(); //获得当前窗口的DC rn GetClientRect(&m_client); //获得窗口的尺寸 rn CDC m_bgcDC;rn m_bgcDC.CreateCompatibleDC(NULL);rn CBitmap m_bgBitmap;rn m_bgBitmap.LoadBitmap(IDB_BITMAP1);rn m_bgcDC.SelectObject(&m_bgBitmap);rn cDC->BitBlt(0, 0, m_client.Width(), m_client.Height(), &m_bgcDC, 0, 0, SRCCOPY);rn BOOL BitBlt(rn int nXDest, //目的DC的起始x rn int nYDest, //起始y rn int nWidth, //宽度 rn int nHeight, //高度 rn HDC hdcSrc, //源DC rn int nXSrc, //源DC起始x rn int nYSrc, //起始y rn DWORD dwRop //贴图方式,大家设置为SRCCOPY就可以了 rn );rn CImage m_hero;rn m_hero.Load("E:\\Personal\\billowy\\123.png");rn TransparentPNG(&m_hero);rn m_hero.Draw(*cDC, 100, 400, 60, 60);rn //加入我们要绘制的代码。。。。。。 rn ReleaseDC(cDC); //释放DCrn // TODO: 在此处添加消息处理程序代码rn rn // 不要为绘制消息而调用 CWnd::OnPaint()rnrn上面是代码,我已经用了封装函数传递了透明效果,为什么运行的时候图片仍然是白底,到底哪里错了? 论坛

mfc贴图问题~~~

09-08

图片导入了,可是运行时贴不出来,不知道为什么,新手求教[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/3.gif][/img]rnrnrn代码如下:rnrn// 贴图View.cpp : implementation of the CMyView classrn//rnrn#include "stdafx.h"rn#include "贴图.h"rnrn#include "贴图Doc.h"rn#include "贴图View.h"rnrn#ifdef _DEBUGrn#define new DEBUG_NEWrn#undef THIS_FILErnstatic char THIS_FILE[] = __FILE__;rn#endifrnrn/////////////////////////////////////////////////////////////////////////////rn// CMyViewrnrnIMPLEMENT_DYNCREATE(CMyView, CView)rnrnBEGIN_MESSAGE_MAP(CMyView, CView)rn //AFX_MSG_MAP(CMyView)rn // NOTE - the ClassWizard will add and remove mapping macros here.rn // DO NOT EDIT what you see in these blocks of generated code!rn //AFX_MSG_MAPrn // Standard printing commandsrn ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)rn ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)rn ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)rnEND_MESSAGE_MAP()rnrn/////////////////////////////////////////////////////////////////////////////rn// CMyView construction/destructionrnrnCMyView::CMyView()rnrn // TODO: add construction code herernrnrnrnCMyView::~CMyView()rnrnrnrnBOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)rnrn // TODO: Modify the Window class or styles here by modifyingrn // the CREATESTRUCT csrnint cardNo; rnint i; rn cardNo=IDB_BITMAP1; //获取第一幅图片的ID号rn for (i=0; i<6; i++)rn m_card[i].LoadBitmap(cardNo); //加载图片rn cardNo++; //指向下一幅图片rn rn //创建内存设备环境rn CDC pDC;rn for (i=0; i<6; i++) //循环创建13张图片的内存设备环境rn card[i].CreateCompatibleDC (&pDC); rn return CView::PreCreateWindow(cs);rnrnrn/////////////////////////////////////////////////////////////////////////////rn// CMyView drawingrnrnvoid CMyView::OnDraw(CDC* pDC)rnrn CMyDoc* pDoc = GetDocument();rn ASSERT_VALID(pDoc);rn // TODO: add draw code for native data herern int i=0,j=0,k=0;//随机化数组rn for(i=0;i<6;i++)rn rn j=rand()%(6-i)+i;rn k=sort[i];rn sort[i]=sort[j];rn sort[j]=k;rn rn //粘贴图片srn for (i=0; i<6; i++)rn card[sort[i]].SelectObject(m_card[sort[i]]);rn if(i<=2)rn pDC->BitBlt (20+i*160, 60, 300, 300, &card[sort[i]], 0, 0, SRCCOPY);rn elsern pDC->BitBlt (20+(i-3)*160, 300, 300, 300, &card[sort[i]], 0, 0, SRCCOPY);rn rn rnrnrn/////////////////////////////////////////////////////////////////////////////rn// CMyView printingrnrnBOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)rnrn // default preparationrn return DoPreparePrinting(pInfo);rnrnrnvoid CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)rnrn // TODO: add extra initialization before printingrnrnrnvoid CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)rnrn // TODO: add cleanup after printingrnrnrn/////////////////////////////////////////////////////////////////////////////rn// CMyView diagnosticsrnrn#ifdef _DEBUGrnvoid CMyView::AssertValid() constrnrn CView::AssertValid();rnrnrnvoid CMyView::Dump(CDumpContext& dc) constrnrn CView::Dump(dc);rnrnrnCMyDoc* CMyView::GetDocument() // non-debug version is inlinernrn ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));rn return (CMyDoc*)m_pDocument;rnrn#endif //_DEBUGrnrn/////////////////////////////////////////////////////////////////////////////rn// CMyView message handlers 论坛

没有更多推荐了,返回首页