分类:
2008-10-13 16:33:03
修改对话框背景方法有很多可以在OnDraw().OnCtlColor().和OnEraseBkgnd()中进行.其中OnCtlColor()是画笔.可以对控件。而对话框可以在OnEraseBkgnd().
其中可以通过修改画笔填空和BitBlt两种方法!
void CInfoView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
//*//////////////
CBitmap bmp; //IDR_BMP1;
if(bmp.LoadBitmap(IDB_BITMAP2))
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
int nX = rect.left +(rect.Width()- bmpInfo.bmWidth) / 2;
int nY = rect.top +(rect.Height()- bmpInfo.bmHeight) / 2;
CBitmap *pOldBmp;
pOldBmp=dcMem.SelectObject(&bmp);
//pDC->BitBlt(nX,nY,bmpInfo.bmWidth,bmpInfo.bmHeight,&dcMem,0,0, SRCCOPY);
pDC->StretchBlt(rect.left,rect.top ,rect.Width(),rect.Height(),&dcMem, 0, 0,bmpInfo.bmWidth,bmpInfo.bmHeight, SRCCOPY);
dcMem.SelectObject(pOldBmp);
bmp.DeleteObject();
}
//*//////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
BOOL CInfoView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
//*//////////////
CBitmap bmp; //IDR_BMP1;
if(bmp.LoadBitmap(IDB_BITMAP2))
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CRect rect;
GetClientRect(&rect);
////////////////////////////////////////////
以下直接用CBrush 在当前DC中操作,则简单。有可能有多个图片(若背景小于)
CBrush m_brush(&bmp); //用CBitmap初始化画笔
pDC->FillRect(&rect,&m_brush); //用画笔填空
///////////////////////////////////////////////////
/*/////////
//以下通过方法二是通过BitBlt来进行
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
int nX =rect.right- rect.left;// rect.left+ (rect.Width() - bmpInfo.bmWidth) / 2;
int nY =rect.bottom-rect.top; //rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
CBitmap *pOldBmp;
pOldBmp=dcMem.SelectObject(&bmp);
pDC->StretchBlt(0,0 ,nX,nY,&dcMem, 0, 0,bmpInfo.bmWidth,bmpInfo.bmHeight, SRCCOPY);
dcMem.SelectObject(pOldBmp);
bmp.DeleteObject();
//*/////////
}
//*//////////////
return FALSE;//要修改返非0值
//return CFormView::OnEraseBkgnd(pDC);
}
}
//////////////////////////////////////////////
//////////////////////////////////////////////
HBRUSH CInfoView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{//以下是修改画笔
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_EDIT||nCtlColor==CTLCOLOR_BTN)
{
CBitmap bmp;
if(bmp.LoadBitmap(IDB_BITMAP2))
{
CRect rect;
pWnd->GetClientRect(&rect);
CBrush m_brush(&bmp);
//pDC->FillRect(&rect,&m_brush);//方法一可以填空
//pDC->SetBkMode(OPAQUE);
return (HBRUSH)m_brush;//方法二修改画笔
}
else
pDC->SetBkColor(RGB(255, 0, 0));
}
if(nCtlColor==CTLCOLOR_STATIC)
{//以下可使Static变成透明
pDC->SetTextColor( RGB( 255,170,70 ) );
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}