16,472
社区成员
发帖
与我相关
我的任务
分享
// IDC_STATIC1为静态文本控件,下面的代码主要是使用GetTextExtentPoint32函数来得到字符串的CSize,希望对你有帮助
CString strText(_T(""));
CWnd* pWnd = GetDlgItem(IDC_STATIC1);
pWnd->GetWindowText(strText);
ASSERT(pWnd);
CDC* pDC = pWnd->GetDC();
CSize size;
int count = strText.GetLength();
GetTextExtentPoint32(pDC->GetSafeHdc(), strText, count, &size);
pDC->DeleteDC();
int nWidth = size.cx / count;
int nIndex = (size.cx/2)/nWidth;
strText = strText.Mid(0, nIndex);
AfxMessageBox(strText);
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;