只是为了存下来,慢慢研究
实现方法1:
直接在对话框中:
- HBRUSH CLoginDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- if(nCtlColor == CTLCOLOR_DLG)
- return m_brush;
- if(nCtlColor == CTLCOLOR_EDIT)
- {
- HBRUSH hNullBr = (HBRUSH)::GetStockObject(NULL_BRUSH);
- pDC->SetTextColor(RGB(0, 255, 0));
- pDC->SetBkMode(TRANSPARENT);//设置背景透明
- return hNullBr;
- }
- if(nCtlColor == CTLCOLOR_STATIC)
- {
- //HBRUSH hNullBr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
- //pDC->SetTextColor(RGB(0, 0, 0));
- //pDC->SetBkMode(TRANSPARENT); //背景设为透明
- //return hNullBr;
- }
- return hbr;
- }
如果只是这样的话,编辑框无法回格删除,所以我在OnEnChange中刷新一下:
- void CLoginDlg::OnEnChangeEditUser()
- {
- // TODO: 如果该控件是 RICHEDIT 控件,它将不
- // 发送此通知,除非重写 CDialog::OnInitDialog()
- // 函数并调用 CRichEditCtrl().SetEventMask(),
- // 同时将 ENM_CHANGE 标志“或”运算到掩码中。
- CRect rc;
- GetDlgItem(IDC_EDIT_USER)->GetWindowRect(&rc);
- ScreenToClient(&rc);
- InvalidateRect(&rc, FALSE);
- }
这个实现的方法前提是我的编辑框属性必须为多行, 但我的应用中其中一个编辑框是用来输入密码的,设置多行之后就不能设置密码属性; 所以放弃
实现方法2
CMyEdit继承CEdit, 重载CtlColor(),设置编辑框的字体以及背景透明;
- HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- HBRUSH hNullBr = (HBRUSH)::GetStockObject(NULL_BRUSH);
- pDC->SetTextColor(RGB(0, 255, 0));
- pDC->SetBkMode(TRANSPARENT);
- return hNullBr;
- //return NULL;
- }
同样我要刷新一下:
- void CMyEdit::OnEnChange()
- {
- CRect rc;
- CLoginDlg* pParent = (CLoginDlg*)GetParent();
- GetWindowRect(&rc);
- pParent->ScreenToClient(&rc);
- pParent->InvalidateRect(&rc, FALSE);
- }