views:

176

answers:

5

Hi

I want to set some text on my edit box, but it should be greyed.

Is there some way to do that?

I am not able to find the proper API for this.

Any suggestions?

A: 

How about normal SetTextColor?

For example,

SetTextColor(hdc, RGB(0xc0, 0xc0, 0xc0));
S.Mark
+1  A: 

You might also be interested in the EM_SETCUEBANNER edit control message. It will cause an edit control to display directions in gray text without affecting user input.

sean e
+2  A: 

Respond to the WM_CTLCOLOREDIT message and use SetTextColor on the passed HDC to select the text color.

Hans Passant
A: 

This is from an MFC application (hence the pWnd), but it's relatively easy to change to pure SDK code:

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

   switch (nCtlColor)
   {
      case CTLCOLOR_EDIT:

         if (pWnd->GetDlgCtrlID () == IDC_MY_EDIT)
         {
            pDC->SetTextColor (COLOR_GRAYTEXT);
         }
         break;

      default:
         break;
   }
   return hbr;
}
Bob Moore
A: 

I have just assumed you are referring to Win32 API. If not please ignore my answer below.

If you want to edit/input text in the edit box in grey color or a different color, you can refer to the replies above that tells you how to overload OnCtlColor().

But if you just display text in a disabled edit box, then it will by default display the text in grey (Make sure the edit box is not read only so that you can write to the edit box control). For example, if you include the below lines in your OnInit() method of your dialog class, it will disable your editbox and display the text in grey:

  virtual void OnInit()
  {
    // Assuming IDC_MY_DISABLED_EDIT is the ID you entered for the editbox 
    // in the dialog designer.
    // the above state will disable the edit box and display text in grey.
    GetDlgCtrl(IDC_MY_DISABLED_EDIT)->EnabledWindow(FALSE);

    // Hello World! will be displayed in grey.
    GetDlgCtrl(IDC_MY_DISABLED_EDIT)->SetWindowText(_T("Hello World!"));
  }
anand.arumug