改变控件颜色

Example

// This OnCtlColor handler will change the color of a static control

// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog

// class has an initialized and created CBrush member named m_brush.

// The control will be painted with red text and a background

// color of m_brush.

 

HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

  // Call the base class implementation first! Otherwise, it may

  // undo what we are trying to accomplish here.

  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 

  // Are we painting the IDC_MYSTATIC control? We can use

  // CWnd::GetDlgCtrlID() to perform the most efficient test.

  if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)

  {

    // Set the text color to red.

    pDC->SetTextColor(RGB(255, 0, 0));

 

    // Set the background mode for text to transparent

    // so background will show thru.

    pDC->SetBkMode(TRANSPARENT);

 

    // Return handle to our CBrush object.

    hbr = m_brush;

  }

 

  return hbr;

}

 

 

假设你已有了名为 My 的对话框工程,你有了一个 STATIC 的控件, IDC_STATIC1

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

      

       // TODO: Change any attributes of the DC here

       if(nCtlColor==CTLCOLOR_STATIC)

       {

              pDC->SetTextColor(RGB(255,0,0));// 字体颜色

              pDC->SetBkColor(RGB(0,0,255));// 字体背景颜色

       }

       // TODO: Return a different brush if the default is not desired

       return hbr;

}

如果要指定某个特定控件可以这样写:

ID IDC_STATIC1

if(pWnd->GetDlgCtlID()==IDC_STATIC1)

{

pDC->SetTextColor(RGB(255,0,0));//

pDC->SetBkMode(TRANSPARENT);// 设置字体背景为透明

return (HBRUSH)::GetStockObject(BLACK_BRUSH);// 设置背景色

}

Return hbr;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值