Microsoft KB Archive/117778

= How to change the background color of an MFC edit control =

Article ID: 117778

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q117778

<div class="notice_section">

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
To change the background color of an edit control in an MFC application, you must override the OnCtlColor message-handling function of the window containing the edit control.

In the new OnCtlColor function, set the background color and return a handle to a brush that will be used for painting the background. This must be done in response to receiving both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX messages in the OnCtlColor function.

This is also documented in the "Class Library Reference" under CWnd::OnCtlColor.

<div class="moreinformation_section">

MORE INFORMATION
The sample code below uses a CDialog-derived class to demonstrate the process. Class Wizard was used to generate message-handling functions for the WM_CTLCOLOR and WM_DESTROY messages. These functions are called CEditDialog::OnCtlColor and CEditDialog::OnDestroy, respectively.

Note In Visual C++ .NET you can add the WM_CTLCOLOR and WM_DESTROY handlers for the dialog object from the Properties Window. All the available messages for the dialog are listed in the Messages tab.

Sample code
// 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

// Overrides // ClassWizard generated virtual function overrides //AFX_VIRTUAL

// Implementation protected:

// Generated message map functions //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

// 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 }

BEGIN_MESSAGE_MAP(CEditDialog, CDialog) //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; }

Additional query words: CEdit

Keywords: kbhowto kbuidesign kbcode KB117778

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.