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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color for Edit Box

Status
Not open for further replies.

nyjil

Programmer
Jul 14, 2000
32
US
With the help of Zaki, I managed to get a long way into my problem, but I'm stumbling on something. What I want to do is to have an edit-box control display a number in a different color. The edit-box is a 'read only', and merely displays information to the user. I want that number to become red when it gets 'too low'. I still want all of the rest of the functionality that an edit box offers. How do I do this? Some sample code would be wonderful if you have the time.

Many Thanks
Nyjil
 
Here is the SDK code for a dialog box which has a read only edit box with ID IDC_EDIT1:

Code:
// Mesage handler for the dialog box.
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  static HWND hWndEdit;
  switch (message)
  {
  case WM_INITDIALOG:
	hWndEdit = GetDlgItem(hDlg, IDC_EDIT1);
	SetWindowText(hWndEdit, _T("100"));
	return TRUE;
  case WM_CTLCOLORSTATIC:
    if((HWND)lParam == hWndEdit)
    {
	SetTextColor((HDC)wParam, RGB( 255, 0, 0));
	SetBkColor((HDC)wParam, GetSysColor(COLOR_3DFACE));
	return (LRESULT)GetSysColorBrush(COLOR_3DFACE);
    }
    return FALSE;	//call DefWindowProc 
  case WM_COMMAND:
    if (LOWORD(wParam) == IDOK ||
        LOWORD(wParam) == IDCANCEL) 
    {
	EndDialog(hDlg, LOWORD(wParam));
	return TRUE;
    }
    break;
  }
  return FALSE;
}
Marius Samoila
Brainbench MVP for Visual C++
 
Marius,

I see what you are doing here, but where do you put this code? And how do you call it? This LRESULT funct has to go somewhere right? And do I call it from my OnInitDialog() funct?

Please Advise,
Jeff
 
the easy way is :

override the WM_CTLCOLOR message handler to your dialog class and make the modification ( let's say your id for edit box is IDC_EDIT1 and m_value is connect to it through DDX):

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

if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_EDIT1)
if (m_Value < 100) pDC->SetTextColor(RGB(255,0,0)); // red
else pDC->SetTextColor(RGB(0,0,0)); // black
}
return hbr;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top