views:

40

answers:

3

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) );
}
A: 

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

MSalters
I want to know the spacing width between the chars <b>before</b> I specify width for a font.
Andrew_Lvov
+1  A: 

GetTextExtentPoint32

http://msdn.microsoft.com/en-us/library/dd144938(v=VS.85).aspx

Pavel Radzivilovsky
+1  A: 

Also see GetTextMetrics

SDX2000