How to get spacing between characters printed using TextOut ?

Posted by life-warrior on Stack Overflow See other posts from Stack Overflow or by life-warrior
Published on 2010-05-26T10:05:10Z Indexed on 2010/05/26 10:21 UTC
Read the original article Hit count: 247

Filed under:
|
|

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 ?

Curier 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) );
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about programming-languages