Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the Background color on Edit Box. 2

Status
Not open for further replies.

sparafucile17

Programmer
Apr 5, 2002
40
US
Does anyone know how to set the Background color of an edit box? I am trying to create a read only edit box called IDC_LOG but I don't like the read only default color. I have seen other thread that talk about some function called:

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

What is this function and where should it be? Not to mention what do you so with it once it there? Would you call in from the OnInitDialog() function? And how would you tie that to my IDC_LOG?

This part of my program is very crucial, as the majority of the program is the logging capability. Someone please help!



Jeff Tackett
Visteon Corp.
Detroit, Mi
 
1. override the WM_CTLCOLOR message handler to your dialog log class
2. add a new member in your dialog class
CBrush m_redbrush;
3. initialize m_redbrush in your OnInitDialog()
m_redbrush.CreateSolidBrush(RGB(255,0,0));

4. modify your code:(say IDC_EDIT1 is the ID of your Edit box)

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

if (nCtlColor == CTLCOLOR_EDIT && pWnd->GetDlgCtrlID() == IDC_EDIT1){
pDC->SetBkColor(RGB(255,0,0));
// pDC->SetTextColor(RGB(255,0,0));

hbr = (HBRUSH) m_redbrush; // apply the red
}
return hbr;
}
 
Jeffray,

I see... that helps me out as to where and how. But I still have the problem. I have implemented your code just as you suggested. However, from what I can see, as long as the "read only" option is checked on my edit box, the background is not changed to red. When I remove the "read only" option, the color is indeed changed to red. But I need to prevent the user from changed the edit box info.

My suspicion here is that when you specify that an edit box is read only, it has a default color (which I do not like) and therefore does not request a ctrlcolor change. Thus, never getting to my if statement to change the color.

Any suggestions?

Jeff
 
The DialogProc is used when you create a Dialog box using the DialogBox Macro.
William
Software Engineer
ICQ No. 56047340
 
if your edit box is read_only, the control kind-of becomes a static control, try to locate it as CTLCOLOR_STATIC intead of CTLEDIT. it works.
 
Actually,

I was wrong when I said that the edit box was not calling the OnCtlColor() function, it was... but not with the control ID that I (and Jeffray) thought! I thought that it was CTLCOLOR_EDIT, but since my edit box was "read only" that changed the ID to CTLCOLOR_STATIC. So I replaced the above if statement to look like:

if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_EDIT1){
pDC->SetBkColor(RGB(255,0,0));
// pDC->SetTextColor(RGB(255,0,0));

hbr = (HBRUSH) m_redbrush; // apply the red
}

And, yep you guessed it, it worked! Thanks guys for leading me in the right direction, I appreciate it.

Jeff Tackett
Visteon Corp.
 
Hi,

I could change the coulur of my edit boxes but not my push button ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top