Andrew_Lvov
Andrew_Lvov

Reputation: 4668

How to get spacing between characters printed using TextOut?

I'm trying to calcuate size of each cell (containing text like "ff" or "a0"), so that 32 cells will fit into window by width. However, charWidth*2 doesn' represent the width of a cell, since it doesn't take spacing between characters in the account.

How can I obtain size of a font so that 32 cells each is two chars like "ff" fit exactly into window's client area ?

Courier is fixed-width font.

RECT rect;
::GetClientRect( hWnd, &rect );
LONG charWidth = (rect.right-rect.left)/BLOCK_SIZE/2-2;
int oldMapMode = ::SetMapMode( hdc, MM_TEXT );
HFONT font = CreateFont( charWidth*2, charWidth, 0, 0, FW_DONTCARE, FALSE,
    FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
    CLEARTYPE_QUALITY, FF_ROMAN, _T("Courier") );
HGDIOBJ oldFont = ::SelectObject( hdc, font );

for( int i = 0; i < BLOCK_SIZE; ++i )
{
    CString str;
    str.Format( _T("%.2x"), (unsigned char)*(g_memAddr+i) );
    SIZE size;
    ::TextOut( hdc, (size.cx+2)*i+1, 1, str, _tcslen((LPCTSTR)str) );
}

Upvotes: 1

Views: 1360

Answers (4)

Hải Duy
Hải Duy

Reputation: 43

you should use GetCharABCWidths. C of the previous Char + A of the next Char will be equal to the distance of two chars

Additionally, if you use SetTextCharacterExtra, store the argument value of the extra parameter and add it to C + A above.

See more

Upvotes: 0

Sandeep Datta
Sandeep Datta

Reputation: 29335

Also see GetTextMetrics

Upvotes: 1

MSalters
MSalters

Reputation: 179779

It seems that you'd want the measured difference in charwidth between a two-character and four-character string.

Upvotes: 0

Related Questions