Question

I have an Edit control that is read only that has text in it. I would like to switch the default gray background to white but have been having limited luck. In my first go, I was executing the following code during initialization of the dialog:

CEdit *m_ctrlEditOne = (CEdit*) GetDlgItem(IDC_EDIT1);
CDC *m_ctrlEEditWee = m_ctrlEditOne->GetDC();
m_ctrlEEditWee->SetBkColor(RGB(255,0,0));
Invalidate(true);

Another solution I tried was:

HBRUSH CTestingDlg::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
if (pWnd->GetStyle() & ES_READONLY)
//if(pDC->GetRuntimeClass == & ES_READONLY)
{
    switch (nCtlColor)
    {
    case CTLCOLOR_STATIC:
        pDC->SetBkColor(RGB(255,255,255));
        return (HBRUSH)GetStockObject(NULL_BRUSH);
    default:
        //return NULL;
        return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
}
//return NULL;
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

In the screenshot below, you can see that the text is inserted after the fact (this is what needs to happen) and appears highlighted in blue - I have no idea where to begin on how to make it just appear as normal, non-highlighted text. When clicking on it, it appears normally. In the bottom left corners of each edit control, one can see a square which should not be appearing there. Also, you can see some artifacts of what looks like to be a combo box dropdown selection appearing in the larger boxes.

enter image description here

I would appreciate any pointers on how to get rid of the artifacts and fix the highlighting issue with inserted text.

Was it helpful?

Solution

I do as shown below. It will change the background of the read only edit control IDC_EDIT1 to white. This is a copy-paste straight out of one of my projects.

m_whitebrush is a private member of CTestOnCtlClorDlg of type HBRUSH and must be initialized to NULL in the constructor of CTestOnCtlClorDlg.

HBRUSH CTestOnCtlClorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  int id = pWnd->GetDlgCtrlID( ) ;

  if (id == IDC_EDIT1)
  {
    pDC->SetTextColor(RGB(0, 0, 0));
    pDC->SetBkColor(RGB(255,255,255));

    if (!m_whitebrush)
      m_whitebrush = CreateSolidBrush(RGB(255,255,255)) ;

    hbr = m_whitebrush ;
  } 

  return hbr;
}

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

  if (m_whitebrush !=NULL)
  {
    DeleteObject(m_whitebrush) ;
    m_whitebrush = NULL ;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top