VC为控件添加背景

为对话框添加背景图片

1.将要添加的图片经PS或画图工具,另存为位图(bmp),并在MFC中引入该BITMAP资源,ID为IDB_BITMAP1;

2.在该Dialog的OnPaint()函数中,添加如下代码:

  CPaintDC dc(this);
  CRect rect;
  GetClientRect(&rect);
  CDC dcMem;
  dcMem.CreateCompatibleDC(&dc);
  CBitmap bmpBackground;
  bmpBackground.LoadBitmap(IDB_BITMAP1);
  BITMAP bitmap;
  bmpBackground.GetBitmap(&bitmap);
  CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
  dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);

//若该对话框为主框,则在OnPaint()函数中if...else...语句中的else中添加以上代码;

 

为控件设置背景颜色

1.为控件所在对话框的类添加CBrush类型的变量,例如m_Brush;

   在该对话框的构造函数中,创建一个画刷,例如:

CDeleteFace::CDeleteFace(CWnd* pParent )
 : CDialog(CDeleteFace::IDD, pParent)
{
 //{{AFX_DATA_INIT(CDeleteFace)
 m_edit1 = _T("");
 //}}AFX_DATA_INIT
 m_brush.CreateSolidBrush(RGB(200,230,190));
}

为该对话框添加OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 函数:

在OnCtlColor中添加如下代码:

if (pWnd->GetDlgCtrlID()==IDC_EDIT1||pWnd->GetDlgCtrlID()==IDC_STATIC)
 {
  pDC->SetBkMode(TRANSPARENT);
  return m_brush;
 }

Begin---重载 OnCtrlColor.
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 
 if(nCtlColor == CTLCOLOR_DLG || nCtlColor == CTLCOLOR_STATIC)
 {
  //pDC->SetTextColor(RGB(0,0,255));//字体色
  pDC->SetBkColor(RGB(210,230,211));//字体背景色******
  HBRUSH B = CreateSolidBrush(RGB(210,230,211)); //背景色
  return B;
 }
 if(nCtlColor == CTLCOLOR_EDIT)
 {
  pDC->SetTextColor(RGB(0,0,255));//字体色
 }

 return hbr;
}

即可完成对控件的颜色设置!

End-----

2.上述方法对Button控件不适用;可用以下方法为Button控件设置背景:

(1).先将Button的Styles设置为owner draw,再在OnInitDialog中加入代码:
((CBitmapButton *)GetDlgItem(IDC_BUTTON1))->LoadBitmaps(IDB_BITMAP)

(2).为工程添加类CSXBtn,基类为CButton,为CSXBtn添加虚函数DrawItem(),完成对Button控件背景、字体颜色的设置;

代码如下:

void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 UINT uStyle = BS_DEFPUSHBUTTON ;//DFCS_BUTTONPUSH;
 
 // This code only works with buttons.
 ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
 
 // If drawing selected, add the pushed style to DrawFrameControl.
 if (lpDrawItemStruct->itemState & ODS_SELECTED)
  uStyle |= DFCS_PUSHED;
 
 // Draw the button frame.
 ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
  DFC_BUTTON, uStyle);
 
 CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
 
 // Get the button's text.
 CString strText;
 GetWindowText(strText);
 
 // Draw the button text using the text color red.
 CBrush B;
 CRect rect;
 CRect focusRect;
 focusRect.CopyRect(&lpDrawItemStruct->rcItem);
 DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
 focusRect.left += 4;
 focusRect.right -= 4;
 focusRect.top += 4;
 focusRect.bottom -= 4;
 
 rect.CopyRect(&lpDrawItemStruct->rcItem);
 pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
 B.CreateSolidBrush(RGB(200,230,190));
 ::FillRect(lpDrawItemStruct->hDC,&rect, (HBRUSH)B.m_hObject);
 ::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
 COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(0,0,0));
 ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
  &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
 ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

将Button控件属性中“所有者绘制”选中,并为控件添加变量,变量类型为CSXBtn;即可!

 

posted on 2012-09-07 11:19  默默地  阅读(397)  评论(0编辑  收藏  举报

导航