MFC基礎--怎樣貼一張圓型的點陣圖 (轉)

worldblog發表於2007-12-04
MFC基礎--怎樣貼一張圓型的點陣圖 (轉)[@more@]

 

MFC基礎--怎樣貼一張圓型的點陣圖


如果你要做有關棋類的東東
又恰好有兩張漂亮棋子的點陣圖,
可惜兩張點陣圖是方型的,後面
有背景..在裡只要將
背景色設成透明就行了,可在
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 re
m_bStoneB.Loaitmap(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
{
// blackstone in memory and select out oldbitmap
pOldBitmap = dcimage.Select(&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)
}

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987950/,如需轉載,請註明出處,否則將追究法律責任。

相關文章