参考《VC技术内幕》,简单的实现程序如下:
void CEx05cwView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CRect aRect;
GetClientRect(aRect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(aRect.Width(),aRect.Height());
pDC->SetViewportExt(aRect.Width(),aRect.Height());
CView::OnPrepareDC(pDC, pInfo);
}
void CEx05cwView::OnDraw(CDC* pDC)
{
CEx05cwDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int nLen;
CFont aFont;
CRect aRect;
CString strText;
strText.Format("This is a test for CFont !");
nLen = strText.GetLength();
GetClientRect(aRect);
aFont.CreateFont(aRect.Height(),aRect.Width()/nLen,0,0,0,FALSE,FALSE,0,ANSI_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
CFont* pOldFont = pDC->SelectObject(&aFont);
pDC->TextOut(0,0,strText);
pDC->SelectObject(pOldFont);
}
关键点有两个:
一、使用MM_ANISOTROPIC映射模式。
二、指定字体的宽和高。
一点体会大家共享。