如何更改一个 MFC 编辑控件的背景色

概要

若要更改的 MFC 应用程序中编辑控件的背景色,您必须重写包含编辑控件的窗口的 OnCtlColor() 消息处理函数。

在新的 OnCtlColor() 函数中,设置的背景颜色,并返回到将用于绘制背景的画笔的句柄。 这必须在 OnCtlColor() 函数中接收的 CTLCOLOR_EDIT 和 CTLCOLOR_MSGBOX 消息的响应。

它也记录在"类库引用"下 CWnd::OnCtlColor()。

更多信息

下面的代码示例使用 CDialog 派生的类来演示该过程。 类向导用于生成的 WM_CTLCOLOR 和 WM_DESTROY 消息的消息处理函数。 这些函数分别称为 CEditDialog::OnCtlColor() 和 CEditDialog::OnDestroy()。

请注意 Visual C++.NET 中您可以从属性窗口中添加对话框对象的 WM_CTLCOLOR 和 WM_DESTROY 处理程序。 对话框中可用的所有邮件的邮件选项卡中都列出。

示例代码

      // editdlg.h : header file
      // 

 
      /// 
      // CEditDialog dialog

      class CEditDialog : public CDialog
      {
      // Construction
      public:
          CEditDialog(CWnd* pParent = NULL);    // standard constructor

      // Add a CBrush* to store the new background brush for edit controls.
          CBrush* m_pEditBkBrush;

      // Dialog Data
          //{{AFX_DATA(CEditDialog)
          enum { IDD = IDD_EDITDIALOG };
              // NOTE: The ClassWizard will add data members here.
          //}}AFX_DATA

      // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CEditDialog)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
      //}}AFX_VIRTUAL

      // Implementation
         protected:

          // Generated message map functions
          //{{AFX_MSG(CEditDialog)
          afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
          afx_msg void OnDestroy();
          //}}AFX_MSG
          DECLARE_MESSAGE_MAP()
      };

      // editdlg.cpp : implementation file
      // 

      #include "stdafx.h"
      #include "mdi.h"
      #include "editdlg.h"

      #ifdef _DEBUG
      #undef THIS_FILE
      static char BASED_CODE THIS_FILE[] = __FILE__;
      #endif

   // 
      // CEditDialog dialog

      CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
          : CDialog(CEditDialog::IDD, pParent)
      {
          //{{AFX_DATA_INIT(CEditDialog)
              // NOTE: The ClassWizard will add member initialization here.
          //}}AFX_DATA_INIT

          // Instantiate and initialize the background brush to black.
          m_pEditBkBrush = new CBrush(RGB(0, 0, 0));
      }

      void CEditDialog::DoDataExchange(CDataExchange* pDX)
      {
          CDialog::DoDataExchange(pDX);
          //{{AFX_DATA_MAP(CEditDialog)
              // NOTE: The ClassWizard will add DDX and DDV calls here.
          //}}AFX_DATA_MAP
      }

      BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
          //{{AFX_MSG_MAP(CEditDialog)
          ON_WM_CTLCOLOR()
          ON_WM_DESTROY()
          //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

// 
      // CEditDialog message handlers

      HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
          switch (nCtlColor) {

          case CTLCOLOR_EDIT:
          case CTLCOLOR_MSGBOX:
              // Set color to green on black and return the background
                 brush.
              pDC->SetTextColor(RGB(0, 255, 0));
              pDC->SetBkColor(RGB(0, 0, 0));
              return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

          default:
              return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
          }
      }

      void CEditDialog::OnDestroy()
      {
          CDialog::OnDestroy();

          // Free the space allocated for the background brush
          delete m_pEditBkBrush;
      }


转自:http://support.microsoft.com/kb/117778

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值