CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: GetTextExtent

  1. #1
    Join Date
    Aug 1999
    Location
    UK
    Posts
    180

    GetTextExtent

    In the following code, all works well. But the width of the text obtained by GetTextExtent seems to be more that the actual text width.

    Any reason for this?


    BOOL CWarnListDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    CClientDC dc(this);
    int n, nTextWidth, nMaxTextWidth = 0;

    for(n = 0; n < m_ErrorList.GetSize(); n++)
    {
    m_WarnList.AddString(m_ErrorList[n]);
    nTextWidth = dc.GetTextExtent(m_ErrorList[n]).cx;

    if(nTextWidth > nMaxTextWidth)
    nMaxTextWidth = nTextWidth;
    }

    // set horizontal bar extent
    m_WarnList.SetHorizontalExtent(nMaxTextWidth);

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }




    Hope you can help.


  2. #2
    Join Date
    Aug 1999
    Location
    Chicago, IL
    Posts
    43

    Re: GetTextExtent

    dwExtent = GetTextExtent(hDC, lpString, nCount);

    GetTextMetrics(hDC, &tm);
    xExtent = LOWORD(dwExtent) - tm.tmOverhang;



  3. #3
    Join Date
    Aug 1999
    Location
    UK
    Posts
    180

    Re: GetTextExtent

    Hi

    Thanks for your suggestion.

    I tried using the CDC member functions on the CClientDC dc(this) device context - as in my function - but the result was exactly the same.

    Does it make a difference using a handle to a DC instead as you do in your sample?

    If so, how do you get the hDC?



  4. #4
    Join Date
    Aug 1999
    Location
    UK
    Posts
    180

    Re: GetTextExtent

    I found out on the MSDN that the functions GetTextExtents do not take into account "kerning". My font is Arial - this is why the size reported by GetTextExtents is larger that when it is actually drawn.

    Don't know how to get around that!


  5. #5
    Guest

    Re: GetTextExtent

    The SDK way of getting the DC is shown below. Event though I am an MFC programmer, I am always leary about using MFC handles. I have had problems in the past with functions like WaitforMultipleObjects (using MFC handles), and a few other problems.

    HDC hdc = ::GetDC(HandleOfWindow)

    It sounds as though you might not be getting the metrics from the proper font. Below is a function I wrote for a scroll view. I hope this helps.

    void CViewMetrics::ComputeScreenMetrics()
    {
    // get a CDC* for the screen
    CDC* pDC = CDC::FromHandle(::GetDC(NULL));
    // select the specified map mode
    pDC->SetMapMode(m_nMapMode);
    // select the specified font
    CFont* pFont = pDC->SelectObject(m_pFont);
    ASSERT(pFont);

    // First metric: get size of logical inch in pixels
    m_sizeLogInch.cx = pDC->GetDeviceCaps(LOGPIXELSX);
    m_sizeLogInch.cy = pDC->GetDeviceCaps(LOGPIXELSY);

    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);

    // Store some useful text metrics
    VERIFY(m_sizeCharScn.cy = tm.tmHeight + tm.tmExternalLeading);
    m_sizeCharScn.cx = tm.tmAveCharWidth;
    m_cyExtLeading = tm.tmExternalLeading;

    m_sizeLine = CSize(0,0);

    // loop through the document and find the longest line
    for(int i = 0; i < m_pDoc->GetNumberOfLines(); i++)
    {
    CString str = m_pDoc->m_saErrorMessages.GetAt(i);
    CSize size = pDC->GetTextExtent(str);
    m_sizeLine.cx = max(size.cx, m_sizeLine.cx);
    }

    // line height is currently equal to character height
    m_sizeLine.cy = m_sizeCharScn.cy;

    // clean up
    pDC->SelectObject(pFont);
    ::ReleaseDC(NULL,pDC->GetSafeHdc());
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured