MFC计算字符串长度,获取字符长度

CClientDC dc(GetDlgItem(IDC_EDIT_ShowMsg));
CFont *pOldFont = dc.SelectObject(this->GetFont());
CRect rcShow;
GetDlgItem(IDC_EDIT_ShowMsg)->GetClientRect(rcShow);
int maxWidth = rcShow.Width() - 15;

CString strSpace;
CString strTmp;
strTmp.Format(_T("%s%s%s"), msg, strSpace, strTime);

SIZE size = {0};
while (size.cx < maxWidth)
{
	::GetTextExtentPoint32(dc.m_hDC, strTmp, strTmp.GetLength(), &size);
	strTmp.Format(_T("%s%s%s"), msg, strSpace, strTime);
	strSpace += _T(" ");
}
strMsg += strTmp + _T("\r\n");
dc.SelectObject(pOldFont);

一定要记得选入字体,不然计算的长度会不精确

上一篇:2019.5.12 七层模型及三次握手


下一篇:Ocelot中文文档-转换Headers