开发者

How to write grey text on edit box?

开发者 https://www.devze.com 2022-12-18 05:59 出处:网络
I want to set some text on my edit box, but it should be greyed. Is开发者_如何学编程 there some way to do that?

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?


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.


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


How about normal SetTextColor?

For example,

SetTextColor(hdc, RGB(0xc0, 0xc0, 0xc0));


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;
}


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!"));
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号