CodeProject上的一些摘抄

来源:互联网 发布:php 文章内链 编辑:程序博客网 时间:2024/05/14 06:27

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 colorpDC->SetTextColor(m_crText);//set the text's background colorpDC->SetBkColor(m_crBackGnd);  //return the brush used for background this sets control background  return m_brBackGnd;}

(2)重载控件的EN_SETFOCUS消息响应

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". Smile | :) 

void CTestDlg::OnEnSetfocusEdit1(){::SetFocus(m_hWnd);} void CTestDlg::OnBnClickedSetText(){m_Edit.SetWindowText(_T("Sample text"));}

0 0
原创粉丝点击