VC/MFC如何设置对话框背景颜色 控件背景色

转自:https://www.cnblogs.com/staring-hxs/archive/2013/01/09/2853126.html
这里大家要注意,OnCtlColor能改变Static等子控件的颜色,对于Button必须设置其属性Owner Draw为True,才能改变Button按钮背景色(CButton 文本的字体颜色并不能通过SetBkColor来改变,需要自己重绘CButton,在DrawItem中进行实现

HBRUSH  CXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT  nCtlColor)
{
  HBRUSH  hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  // TODO:  在此更改 DC 的任何属性
  if (nCtlColor==CTLCOLOR_BTN)          //更改按钮颜色
  {
   //pDC->SetBkMode(TRANSPARENT);
   pDC->SetTextColor(RGB(0,0,0));
   pDC->SetBkColor(RGB(121,121,255));  
   HBRUSH  b=CreateSolidBrush(RGB(121,121,255));
   return  b;
  }
  else  if (nCtlColor==CTLCOLOR_SCROLLBAR)  //
  {
   //pDC->SetBkMode(TRANSPARENT);
   pDC->SetTextColor(RGB(0,0,0));
   pDC->SetBkColor(RGB(233,233,220));
   HBRUSH  b=CreateSolidBrush(RGB(233,233,220));
   return  b;
  }
  else  if (nCtlColor==CTLCOLOR_EDIT)   //更改编辑框
  {
   //pDC->SetBkMode(TRANSPARENT);
   pDC->SetTextColor(RGB(0,0,0));
   pDC->SetBkColor(RGB(165,254,236));
   HBRUSH  b=CreateSolidBrush(RGB(165,254,236));
   return  b;
  }
  else  if (nCtlColor==CTLCOLOR_STATIC)  //更改静态文本
  {
   pDC->SetTextColor(RGB(0,0,0));
   pDC->SetBkColor(RGB(166,254,1));
   HBRUSH  b=CreateSolidBrush(RGB(166,254,1));
   return  b;
  }
  else  if (nCtlColor==CTLCOLOR_DLG)   //更改对话框背景色
  {
   pDC->SetTextColor(RGB(0,0,0));
   pDC->SetBkColor(RGB(166,254,1));
   HBRUSH  b=CreateSolidBrush(RGB(166,254,1));
   return  b;
  }
  // TODO:  如果默认的不是所需画笔,则返回另一个画笔
  return  hbr;
}

对Button按钮修改需要通过重写DrawItem方法,所以写一个类CSXBtn,继承于CButton类。CSXBtn类实现了鼠标在button和不在button按钮时变换背景色功能

void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    static int i=0;
    UINT uStyle = BS_DEFPUSHBUTTON;
    
    // 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 focusRect;
    focusRect.CopyRect(&lpDrawItemStruct->rcItem);     
    DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
    pDC->Draw3dRect(focusRect, ::GetSysColor(COLOR_BTNHILIGHT), ::GetSysColor(COLOR_BTNSHADOW));
    if(m_flag)//判断鼠标是否在button按钮上
    {
        B.CreateSolidBrush(RGB(0,255,0));
    }
    else
    {
        B.CreateSolidBrush(RGB(0,0,255));
    }
    ::FillRect(lpDrawItemStruct->hDC,&focusRect, (HBRUSH)B.m_hObject);
    ::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
    COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
    ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
        &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}




void CSXBtn::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_flag=true;
    TRACKMOUSEEVENT tme;
    tme.cbSize=sizeof(tme);
    tme.dwFlags=TME_LEAVE;
    tme.hwndTrack=this->m_hWnd;
    ::_TrackMouseEvent(&tme);
    CButton::OnMouseMove(nFlags, point);
    Invalidate();
}




void CSXBtn::OnMouseLeave()
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_flag=false;
    
    CButton::OnMouseLeave();
    Invalidate();
    UpdateWindow();
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值