2010-01-22 68 views
0

我想在我的编辑框中设置一些文字,但它应该是灰色的。如何在编辑框中编写灰色文本?

有没有办法做到这一点?

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

有什么建议吗?

+0

你的意思是,文本与灰色阴影或只是文本颜色灰色? – YOU 2010-01-22 05:42:23

+0

带灰色的文字。 – anand 2010-01-22 05:44:19

回答

0

正常SetTextColor怎么样?

例如,

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

这是从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; 
} 
0

刚才我假设你指的是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!")); 
    }