vc/mfc 对话框中文本控件textout输出字体消失的解决办法

来源:互联网 发布:苹果系统mac破解版cs6 编辑:程序博客网 时间:2024/05/29 10:13

由于对话框上的文本控件是在对话框显示后才画上去的,所以用textout直接输出字体会被覆盖。

解决的思路是先让文本控件的背景为透明的;

代码:OnCtlColor为消息

HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 
 // TODO: Change any attributes of the DC here
 if(pWnd->GetDlgCtrlID()   ==   IDC_EDIT)  
 {  
  pDC->SetBkMode(TRANSPARENT);  
  CBrush   brush;
  brush.CreateStockObject(NULL_BRUSH);  
  return   brush;  
 }

 // TODO: Return a different brush if the default is not desired
 return hbr;
}

然后用白色的画刷去填充文本区域:

代码:

放在onpaint中

 CClientDC cdc(this);
 CRect rect;
 GetDlgItem(IDC_EDIT)->GetClientRect(&rect);
 CBrush brush(RGB(255,255,255));
 CClientDC dc(GetDlgItem(IDC_EDIT));
 dc.FillRect(&rect,&brush);
 dc.TextOut(0,0,"你好啊啊");

这样输出的字体就不会消失了。

我建议用richedit空间,就在edit空间的下面