MFC CEdit背景透明,文字透明实现
2018-6-17
需求:不规则输入框实现
实现:
1. 响应对话框的WM_CTLCOLOR消息
获取EDIT对应的窗口背景色,设置画刷为NULL
if (nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(255, 255, 255));
pDC->SetBkMode(TRANSPARENT);
CDC* dc = GetDC();
CRect rc;
pWnd->GetWindowRect(rc);
ScreenToClient(rc);
pDC->BitBlt(0, 0, rc.Width(), rc.Height(), dc, rc.left, rc.top, SRCCOPY);
return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
2. 响应EN_CHANGE消息
获取控件所在的位置,通知窗口进行刷新
CRect rc;
GetDlgItem(IDC_EDIT_USER_NAME)->GetWindowRect(&rc);
ScreenToClient(&rc);
InvalidateRect(&rc, TRUE);