• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

MFC基础--怎样贴一张圆型的位图

发布: 2007-7-14 21:11 | 作者: 佚名    | 来源: 网络转载     | 查看: 24次 | 进入软件测试论坛讨论

领测软件测试网 如果你要做有关棋类的东东
又恰好有两张漂亮棋子的位图,
可惜两张位图是方型的,后面
有背景..在Delphi里只要将
背景色设成透明就行了,可在
MFC里怎样实现把背景去掉只
显示圆形的棋子呢?

实例如下:
//To invoke this function with the position

BOOL IsBlack = TRUE;//set the black stone defautly
CBitMap m_bStoneB;
CBitMap m_bStoneW;
//load bmp from resource
m_bStoneB.LoadBitmap(IDB_BLACKSTONE);
m_bStoneW.LoadBitmap(IDB_WHITESTONE);

void CxxDlg::OnPutStone(CPoint point)
{
CDC *pDC = GetDC(); //Get Device Context
CBitmap *pOldBitmap;
CBrush *pOldBrush;
CPen *pOldPen;

if(pDC == NULL)
return;

CDC dcimage;
if (!dcimage.CreateCompatibleDC(pDC)) //Create MemDC
return;

CRect rect; //Get the rectangle of stone;
rect.left = point.x - 25/3;
rect.right = point.x + 25/3;
rect.top = point.y - 25/3;
rect.bottom = point.y + 25/3;

if(IsBlack) //judge whether stone is black
{
//Select blackstone in memory and select out oldbitmap
pOldBitmap = dcimage.SelectObject(&m_bStoneB);
CBrush br(RGB(0, 0, 0)); //set brush to black
CPen pen(PS_SOLID,1,RGB(0, 0, 0));//set pen to black
pOldPen=pDC->SelectObject(&pen);//select in pen(black)
//and out oldpen

pOldBrush = pDC->SelectObject(&br);//select in brush(black)
pDC->Ellipse(&rect);//draw a ellipse in defined rectangle
BITMAP bm;
m_bStoneB.GetBitmap(&bm);//Get blackstone bitmap info

//paint memory bitmap(blackstone) with the operator OR
pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&dcimage,0,0,bm.
mWidth,bm.bmHeight,SRCPAINT);
}
else //the same to draw whitestone bitmap
{
//select in whitestone and select out blackstone
pOldBitmap = dcimage.SelectObject(&m_bStoneW);
CBrush br(RGB(255, 255, 255)); //set white brush
CPen pen(PS_SOLID,1,RGB(255, 255, 255));//set white pen
pOldPen=pDC->SelectObject(&pen);
pOldBrush = pDC->SelectObject(&br);
pDC->Ellipse(&rect);
BITMAP bm;
m_bStoneW.GetBitmap(&bm);
//paint memory bitmap(whitestone) with the operator AND
pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),

&dcimage,0,0,bm.
mWidth,bm.bmHeight,SRCAND);
}

dcimage.SelectObject(pOldBitmap); //select old bitmap
pDC->SelectObject(pOldPen);//select old pen
pDC->SelectObject(pOldBrush);//select old brush

IsBlack = !IsBlack; //switch to white or black
ReleaseDC(pDC);//relieve GDI resource(important)
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网