开发者

How to get spacing between characters printed using TextOut?

开发者 https://www.devze.com 2023-01-01 15:28 出处:网络
I\'m trying to calcuate size of each cell (containing text like \"ff\" or \"a0\"), so that 32 cells will开发者_JAVA技巧 fit into window by width.

I'm trying to calcuate size of each cell (containing text like "ff" or "a0"), so that 32 cells will开发者_JAVA技巧 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) );
}


GetTextExtentPoint32

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


Also see GetTextMetrics


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

0

精彩评论

暂无评论...
验证码 换一张
取 消