BOOL CBDialog::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);
if(!m_bitmap.m_hObject)
return true;
CRect rect;
GetClientRect(&rect);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dc.SelectObject(&m_bitmap);
int bmw, bmh ;
BITMAP bmap;
m_bitmap.GetBitmap(&bmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
int xo=0, yo=0;
if(m_style == StyleTile)//平铺
{
for (yo = 0; yo < rect.Height(); yo += bmh)
{
for (xo = 0; xo < rect.Width(); xo += bmw)
{
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}
}
}
if(m_style == StyleCenter) //居中
{
if(bmw < rect.Width())
xo = (rect.Width() - bmw)/2;
else
xo=0;
if(bmh < rect.Height())
yo = (rect.Height()-bmh)/2;
else
yo=0;
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}
if(m_style == StyleStretch)//拉伸
{
pDC->StretchBlt(xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0,bmw,bmh, SRCCOPY);
}
dc.SelectObject(pOldBitmap);
return true;
}
void CBDialog::SetBitmapStyle(int style)
{
if((style==StyleTile)||
(style==StyleCenter)||
(style==StyleStretch))
{
m_style = style;
}
}
int CBDialog::SetBitmap(UINT nIDResource)
{
if(m_bitmap.LoadBitmap(nIDResource))
return 0;
else
return 1;//error
}