如何让edit control具有不同的文本颜色和背景颜色

本文介绍如何在MFC程序中通过CZilchDlg类的OnCtlColor方法改变静态控件的颜色,并提供了代码示例。此外,还介绍了在SDK程序中通过WM_CTLCOLOREDIT消息实现相同功能的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果是MFC程序,优快云里有一段sample code:

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're 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;
}

如果是SDK的程序,可以这样来写:

switch (message)

{

...

...

...

  case WM_CTLCOLOREDIT:
  SetTextColor(reinterpret_cast<HDC>(wParam), RGB(255,0,0));//设置文本颜色为红色
  SetBkMode(reinterpret_cast<HDC>(wParam), TRANSPARENT);//这一行不能少!!!
  return reinterpret_cast<HRESULT>(hbr);//返回edit control的背景画刷

...

...

...

 

很简单吧

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值