Chinaunix首页 | 论坛 | 博客
  • 博客访问: 381437
  • 博文数量: 715
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:46
文章分类

全部博文(715)

文章存档

2011年(1)

2008年(714)

我的朋友

分类:

2008-10-13 16:33:16

修改对话框背景方法有很多可以在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;
}

发表于 木子的blog 阅读(2409) | |  

--------------------next---------------------

阅读(179) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~