How to create a CEdit with white color and ES_READONLY style

Posted on 2005-04-20
Last Modified: 2013-11-20
            return -1;

i create a CEdit use the code above, its color is grey. but i want a edit control with white color. how can i do?

thank you.
Question by:lijinghe
    LVL 2

    Accepted Solution

    LVL 3

    Assisted Solution

    Refer to following MSDN Article:

    HOWTO: Change the Background Color of an MFC Edit Control

    The information in this article applies to:

    The Microsoft Foundation Classes (MFC), used with:
    Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
    Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 2.1, 4.0, 4.1, 4.2, 5.0, 6.0


    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().

    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.

    Sample Code

          // editdlg.h : header file

          // CEditDialog dialog

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

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

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

          // Overrides
          // ClassWizard generated virtual function overrides
          virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

          // Implementation

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

          // 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__;

          // CEditDialog dialog

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

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

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

          BEGIN_MESSAGE_MAP(CEditDialog, CDialog)

          // 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
                  pDC->SetTextColor(RGB(0, 255, 0));
                  pDC->SetBkColor(RGB(0, 0, 0));
                  return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

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

          void CEditDialog::OnDestroy()

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

    Additional query words: CEdit

    Keywords : kbcode kbMFC KbUIDesign kbVC100 kbVC150 kbVC151 kbVC200 kbVC410 kbVC420 kbVC500 kbVC600 kbGrpMFCATL
    Version : winnt:1.0,2.0,2.1,4.0,4.1,4.2,5.0,6.0
    Platform : winnt
    Issue type : kbhowto
    Technology : kbvc

    Last Reviewed: March 10, 2000
    © 2000 Microsoft Corporation. All rights reserved. Terms of Use.

    Send feedback to MSDN.Look here for MSDN Online resources.

    Author Comment

    thank you

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now