HexEdit/GridCtrl_src/GridCellBase.cpp
CSize CGridCellBase::GetTextExtent(LPCTSTR szText, CDC* pDC /*= NULL*/)
{
    CGridCtrl* pGrid = GetGrid();
    ASSERT(pGrid);

    BOOL bReleaseDC = FALSE;
    if (pDC == NULL || szText == NULL)
    {
        if (szText)
			pDC = pGrid->GetDC();
        if (pDC == NULL || szText == NULL)
        {
            CGridDefaultCell* pDefCell = (CGridDefaultCell*) GetDefaultCell();
            ASSERT(pDefCell);
            return CSize(pDefCell->GetWidth(), pDefCell->GetHeight());
        }
        bReleaseDC = TRUE;
    }

    CFont *pOldFont = NULL,
          *pFont = GetFontObject();
    if (pFont)
        pOldFont = pDC->SelectObject(pFont);

    CSize size;
    int nFormat = GetFormat();

    // If the cell is a multiline cell, then use the width of the cell
    // to get the height
    if ((nFormat & DT_WORDBREAK) && !(nFormat & DT_SINGLELINE))
    {
        CString str = szText;
        int nMaxWidth = 0;
        while (TRUE)
        {
            int nPos = str.Find(_T('\n'));
            CString TempStr = (nPos < 0)? str : str.Left(nPos);
            int nTempWidth = pDC->GetTextExtent(TempStr).cx;
            if (nTempWidth > nMaxWidth)
                nMaxWidth = nTempWidth;

            if (nPos < 0)
                break;
            str = str.Mid(nPos + 1);    // Bug fix by Thomas Steinborn
        }

        CRect rect;
        rect.SetRect(0,0, nMaxWidth+1, 0);
        pDC->DrawText(szText, -1, rect, nFormat | DT_CALCRECT);
        size = rect.Size();
    }
    else
        size = pDC->GetTextExtent(szText, _tcslen(szText));

    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    size.cx += (tm.tmOverhang);

    if (pOldFont)
        pDC->SelectObject(pOldFont);

    size += CSize(4*GetMargin(), 2*GetMargin());

    // Kludge for vertical text
    LOGFONT *pLF = GetFont();
    if (pLF->lfEscapement == 900 || pLF->lfEscapement == -900)
    {
        int nTemp = size.cx;
        size.cx = size.cy;
        size.cy = nTemp;
        size += CSize(0, 4*GetMargin());
    }

    if (bReleaseDC)
        pGrid->ReleaseDC(pDC);

    return size;
}
// By default this uses the selected font (which is a bigger font)

By default this uses the selected font (which is a bigger font)
By default this uses the selected font ( which is a bigger font )
CSize CGridCellBase::GetTextExtent(LPCTSTR szText, CDC* pDC )
{
    CGridCtrl* pGrid = GetGrid();
    ASSERT(pGrid);
    BOOL bReleaseDC = FALSE;
    if (pDC == NULL || szText == NULL)
    {
        if (szText)
			pDC = pGrid->GetDC();
        if (pDC == NULL || szText == NULL)
        {
            CGridDefaultCell* pDefCell = (CGridDefaultCell*) GetDefaultCell();
            ASSERT(pDefCell);
            return CSize(pDefCell->GetWidth(), pDefCell->GetHeight());
        }
        bReleaseDC = TRUE;
    }
    CFont *pOldFont = NULL,
          *pFont = GetFontObject();
    if (pFont)
        pOldFont = pDC->SelectObject(pFont);
    CSize size;
    int nFormat = GetFormat();
    if ((nFormat & DT_WORDBREAK) && !(nFormat & DT_SINGLELINE))
    {
        CString str = szText;
        int nMaxWidth = 0;
        while (TRUE)
        {
            int nPos = str.Find(_T('\n'));
            CString TempStr = (nPos < 0)? str : str.Left(nPos);
            int nTempWidth = pDC->GetTextExtent(TempStr).cx;
            if (nTempWidth > nMaxWidth)
                nMaxWidth = nTempWidth;
            if (nPos < 0)
                break;
            str = str.Mid(nPos + 1);    
        }
        CRect rect;
        rect.SetRect(0,0, nMaxWidth+1, 0);
        pDC->DrawText(szText, -1, rect, nFormat | DT_CALCRECT);
        size = rect.Size();
    }
    else
        size = pDC->GetTextExtent(szText, _tcslen(szText));
    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    size.cx += (tm.tmOverhang);
    if (pOldFont)
        pDC->SelectObject(pOldFont);
    size += CSize(4*GetMargin(), 2*GetMargin());
    LOGFONT *pLF = GetFont();
    if (pLF->lfEscapement == 900 || pLF->lfEscapement == -900)
    {
        int nTemp = size.cx;
        size.cx = size.cy;
        size.cy = nTemp;
        size += CSize(0, 4*GetMargin());
    }
    if (bReleaseDC)
        pGrid->ReleaseDC(pDC);
    return size;
}
CSize CGridCellBase :: GetTextExtent ( LPCTSTR szText , CDC * pDC ) { CGridCtrl * pGrid = GetGrid ( ) ; ASSERT ( pGrid ) ; BOOL bReleaseDC = FALSE ; if ( pDC == NULL || szText == NULL ) { if ( szText ) pDC = pGrid -> GetDC ( ) ; if ( pDC == NULL || szText == NULL ) { CGridDefaultCell * pDefCell = ( CGridDefaultCell * ) GetDefaultCell ( ) ; ASSERT ( pDefCell ) ; return CSize ( pDefCell -> GetWidth ( ) , pDefCell -> GetHeight ( ) ) ; } bReleaseDC = TRUE ; } CFont * pOldFont = NULL , * pFont = GetFontObject ( ) ; if ( pFont ) pOldFont = pDC -> SelectObject ( pFont ) ; CSize size ; int nFormat = GetFormat ( ) ; if ( ( nFormat & DT_WORDBREAK ) && ! ( nFormat & DT_SINGLELINE ) ) { CString str = szText ; int nMaxWidth = 0 ; while ( TRUE ) { int nPos = str . Find ( _T ( ' \n ' ) ) ; CString TempStr = ( nPos < 0 ) ? str : str . Left ( nPos ) ; int nTempWidth = pDC -> GetTextExtent ( TempStr ) . cx ; if ( nTempWidth > nMaxWidth ) nMaxWidth = nTempWidth ; if ( nPos < 0 ) break ; str = str . Mid ( nPos + 1 ) ; } CRect rect ; rect . SetRect ( 0 , 0 , nMaxWidth + 1 , 0 ) ; pDC -> DrawText ( szText , -1 , rect , nFormat | DT_CALCRECT ) ; size = rect . Size ( ) ; } else size = pDC -> GetTextExtent ( szText , _tcslen ( szText ) ) ; TEXTMETRIC tm ; pDC -> GetTextMetrics ( & tm ) ; size . cx += ( tm . tmOverhang ) ; if ( pOldFont ) pDC -> SelectObject ( pOldFont ) ; size += CSize ( 4 * GetMargin ( ) , 2 * GetMargin ( ) ) ; LOGFONT * pLF = GetFont ( ) ; if ( pLF -> lfEscapement == 900 || pLF -> lfEscapement == -900 ) { int nTemp = size . cx ; size . cx = size . cy ; size . cy = nTemp ; size += CSize ( 0 , 4 * GetMargin ( ) ) ; } if ( bReleaseDC ) pGrid -> ReleaseDC ( pDC ) ; return size ; }
By default this uses the selected font (which is a bigger font)
By default this uses the selected font ( which is a bigger font )
/*= NULL*/ // If the cell is a multiline cell, then use the width of the cell
 // to get the height
 // Bug fix by Thomas Steinborn
 // Kludge for vertical text
