您的位置:首页 > 其它

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

2013-07-13 11:55 381 查看


概要

若要更改的 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐