15,979
社区成员
发帖
与我相关
我的任务
分享
CDC * pCdc = pMainDlg->GetDC ();//获取主窗口的CDC类对象
CFont fn;
CSize cSize;
fn.CreatePointFont (100, "新宋体");//创建字体类对象
pCdc->SelectObject (&fn);//设置CDC类对象的字体
cSize = pCdc->GetTextExtent (szStatus);//计算像素尺寸
void CDrawPic::AccountStr(CDC *pdc,
int &width, // 字串宽度(像素)
int &height, // 字串高度(像素)
CString str, // 字串
int size, // 字体大小
CString font) // 字体名
{
CSize txtpot;
CFont fn;
fn.CreatePointFont(size,(LPCTSTR)font);
CFont *pOldfont=pdc->SelectObject(&fn); // 设置新字体
txtpot=pdc->GetTextExtent(str); // 字串的长度像素
width=txtpot.cx;
height=txtpot.cy;
pdc->SelectObject(pOldfont);
}
试试这个函数:void CDrawPic::AccountStr(CDC *pdc, int &width, // 字串宽度(像素) int &height, // 字串高度(像素) CString str, // 字串 int size, // 字体大小 CString font) // 字体名 { CSize txtpot; CFont fn; fn.CreatePointFont(size,(LPCTSTR)font); CFont *pOldfont=pdc->SelectObject(&fn); // 设置新字体 txtpot=pdc->GetTextExtent(str); // 字串的长度像素 width=txtpot.cx; height=txtpot.cy; pdc->SelectObject(pOldfont); }
CRect rc1;
CSize rc2;
char * pszText = "I";
pMainDlg->GetDC()->DrawText(pszText, rc1, DT_CALCRECT);
rc2 = pMainDlg->GetDC()->GetTextExtent(pszText);
在对话框的OnPaint或视图的OnDraw中加入如下代码:// 取文本的尺寸 CRect rc; pDC->DrawText(L"一二三", rc, DT_CALCRECT);
// 取文本的尺寸
CRect rc;
pDC->DrawText(L"一二三", rc, DT_CALCRECT);