1、获取字符的宽度(单位:像素)
int pixWidth( CString &str )
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CTestBarCodeDlg *ph = new CTestBarCodeDlg;
ph->Create(CTestBarCodeDlg::IDD);
CDC* pDC = ph->GetDC();
CSize size = pDC->GetTextExtent(str);
delete ph;
return size.cx;
}
2、CDC
CDC* pDC=GetDC();
CClientDC dc1(this);
CBrush brush(RGB(255,255,255));
CRect rect1(240,400, 600,640); //指定区域大小
float n = 1.0*(600-240)/480;
dc1.FillRect(&rect1,&brush); //在指定区域绘制矩形
pDC->SetTextColor(RGB(255,0,0)); //设置字体颜色
pDC->SetTextAlign(TA_RIGHT);
CFont font; //创建新的字体
font.CreateFont(字体高度,0,0,0,FW_NORMAL,FALSE,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,
字体名称);
CFont * OldFont = dc1.SelectObject(&font);
CStringArray cstringArr; int iNum = 0;
CStringLine(cstringArr,iNum);
for (int i=0; i<iNum; i++)
{
dc1.TextOut(x,y,"str"); /
}
dc1.SelectObject(OldFont);
font.DeleteObject();