void CLaserView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
CScrollView::OnPrepareDC(pDC, pInfo);
CRect rect;
GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportExt(100,100);
CSize cs=pDC->GetViewportExt();
pDC->SetWindowExt(100, -100);
CPoint ptOrg;
CSize cs1 = GetDocument()->GetSize();
ptOrg.x = GetDocument()->GetSize().cx / 2;
ptOrg.y = GetDocument()->GetSize().cy / 2;
// ptOrg is in logical coordinates
pDC->OffsetWindowOrg(-ptOrg.x,ptOrg.y);
}
void CLaserView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
static int nFirstFlag=0;
CString str;
CClientDC dc(this);
CSize sz ;
TEXTMETRIC tm;
dc.GetTextMetrics(&tm); str.Format("%c",nChar);
         sz = dc.GetTextExtent(str);
CScrollView::OnChar(nChar, nRepCnt, nFlags);
}在计算字符h和s的过程中sz是相等的,tm也是相等的,不过我发现明显h和s的字符高度是不一样的,而且抓取到的tm感觉有些怪怪的:
tm.tmExternalLeading=0;
tm.tmDescent=3;
跟<<window程序设计>>中说的有差距,各位高手帮忙分析呀!我这种做法是否真确,如果不真确麻烦给予指导,谢谢了,解决比给分。

解决方案 »

  1.   

    可以使用GraphicsPath(路径)类,创建一个路径对象,在路径对象上画文字,最后将路径对象的边界输出,自然就是文字的宽高啦。
    如:
    /// 创建路径对象
    GraphicsPath pathObj;
    /// 添加文字
    TCHAR* tchStr = _TEXT("中国 I love you 1314");
    pathObj.AddString(tchStr);
    /// 输出路径边界值
    RectF rcBound;
    pathObj.GetBounds(&rcBound);
    /// 获取文字的宽高
    float fTextWidth = rcBound.Width;
    float fTextHeight = rcBound.Height; 
      

  2.   

    SIZE sizeone;
    BOOL ret = GetTextExtentPoint(m_hMemDC, _T("1"), wcslen(_T("1")), &sizeone);
      

  3.   

    测试代码如下:
    using namespace Gdiplus;
    Graphics graphics(dc.GetSafeHdc()); FontFamily fontFamily(L"Arial");
    StringFormat strformat; wchar_t aa[] = L"s";
    GraphicsPath path;
    path.AddString(aa, wcslen(aa), &fontFamily, 
    FontStyleRegular, 48, Gdiplus::Point(10,10), &strformat );
    RectF rect1;
    path.GetBounds(&rect1);
    float fTextWidth = rect1.Width;
    float fTextHeight = rect1.Height;
    未遂呀,以前没有接触过GDI+,头疼
      

  4.   

    1、把字写到一个DC里。
    2、逐行、逐列分析DC的像素值。宽高就有了。
      

  5.   

    DrawText函数可以计算出字符串占用的RECT,如果指定RECt的宽度还可以自动换行,计算出该宽度下的高度
      

  6.   

    好说:
        这与计算一个黑点的位置没有两样.只是一个延伸问题.
        关键要计算字符在DDB的像素位置,注意在DIB没用.要求你先搞清这两个基本概念.然后就容易了/.
      

  7.   

    DrawText(const CString& str, LPRECT lpRect, UNIT nFormat);nFormat设置为DT_CALCRECT
    这样就会只计算宽高,不真正绘制文本计算出来的宽高会返回到lpRect参数里