1.问题:
控件只读但不要背景变灰。 解决方案: (1)重载控件的WM_CTLCOLOR消息响应(ON_WM_CTLCOLOR_REFLECT()) HBRUSH CReadOnlyEdit::CtlColor(CDC* pDC, UINT nCtlColor) { // TODO: Return a non-NULL brush if the parent's //handler should not be called //set text color pDC->SetTextColor(m_crText); //set the text's background color pDC->SetBkColor(m_crBackGnd); //return the brush used for background this sets control background return m_brBackGnd; } |
Essentially, deriving your own class from CEdit would be the ideal way, but you say you don't want it. So, I have a silly hack for you.
Add a handler for EN_SETFOCUS
and voluntarily give away the focus there. This way, the user won't be able to type anything into it or modify it. Because he just cannot set the focus. But, with a member variable for the control, you will be able to manipulate it from within your program, essentially making it "read only".
void CTestDlg::OnEnSetfocusEdit1()
{
::SetFocus(m_hWnd);
}
void CTestDlg::OnBnClickedSetText()
{
m_Edit.SetWindowText(_T("Sample text"));
}