如何在编辑框中写入灰色文本?

发布于 2024-08-18 17:48:05 字数 93 浏览 5 评论 0原文

我想在编辑框上设置一些文本,但它应该是灰色的。

有什么办法可以做到这一点吗?

我无法为此找到合适的 API。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尘曦 2024-08-25 17:48:05

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.

谜兔 2024-08-25 17:48:05

响应 WM_CTLCOLOREDIT 消息并使用 SetTextColor在传递的 HDC 上选择文本颜色。

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

征﹌骨岁月お 2024-08-25 17:48:05

正常的 SetTextColor 怎么样?

例如,

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

How about normal SetTextColor?

For example,

SetTextColor(hdc, RGB(0xc0, 0xc0, 0xc0));
穿透光 2024-08-25 17:48:05

这是来自 MFC 应用程序(因此是 pWnd),但更改为纯 SDK 代码相对容易:

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

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;
}
梦里南柯 2024-08-25 17:48:05

我刚刚假设您指的是 Win32 API。如果没有请忽略我下面的回答。

如果你想在编辑框中以灰色或其他颜色编辑/输入文本,你可以参考上面的回复,告诉你如何重载OnCtlColor()。

但是,如果您只是在禁用的编辑框中显示文本,那么默认情况下它将以灰色显示文本(确保编辑框不是只读的,以便您可以写入编辑框控件)。例如,如果您在对话框类的 OnInit() 方法中包含以下几行,它将禁用您的编辑框并以灰色显示文本:

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

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!"));
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文