您的位置:首页 > 其它

VC DrawText显示多行,包括设置行距。

2010-04-26 09:50 337 查看
int   DrawMultLineText(HDC   hDC   ,   int   nXStart   ,   int   nYStart   ,   int   nWidth   ,   int   nRowHeight   ,   LPCTSTR   pBuff)
{
TEXTMETRIC   tm;

LPCTSTR   pChar;

if(!GetTextMetrics(hDC   ,   &tm))
return   0;
CPoint   posStart   ,   posCurr;
int   nRowCount   =   0;
pChar   =   pBuff;

posStart.SetPoint(nXStart   ,   nYStart);

for(;   *pChar   ;   pChar++)
{
if(*pChar   ==   '\t')
{
MovePos(posStart   ,   posCurr   ,   nWidth   ,nRowHeight   ,   tm.tmAveCharWidth   ,   4);
}
else
{
if(*pChar   ==   '\r')
{
posCurr.y   +=   nRowHeight;
posCurr.x   =   posStart.x;
if(   *(pChar   +   1)   ==   '\n')
pChar++;
}
else
if(*pChar   ==   '\n')
{
posCurr.y   +=   nRowHeight;
posCurr.x   =   posStart.x;
}
else
if(   IsChineseChar(pChar))
{
TextOut(hDC   ,   posCurr.x   ,   posCurr.y   ,pChar   ,   2);
MovePos(posStart   ,   posCurr   ,   nWidth   ,nRowHeight   ,   tm.tmMaxCharWidth   ,   1)   ;
pChar   ++;
}
else
{
TextOut(hDC   ,   posCurr.x   ,   posCurr.y   ,pChar   ,   1);
MovePos(posStart   ,   posCurr   ,   nWidth   ,nRowHeight   ,   tm.tmAveCharWidth   ,   1);
}
}
}
return   (posCurr.y   +   posStart.y)   /   nRowHeight;
}

void   MovePos(const   POINT   &posStart   ,   POINT   &posCurr   ,   int   nColWidth   ,   int   nRowHeight   ,   int   nCharWidth   ,   int   nCount)
{
int   i;
if(nColWidth   <   nCharWidth)
return;
if(posStart.x   >   posCurr.x)
posCurr.x   =   posStart.x;
if(posStart.y   >   posCurr.y)
posCurr.y   =   posStart.y;

for(i   =   0   ;   i   <   nCount   ;   i++)
{
posCurr.x   +=   nCharWidth;
if(posCurr.x   >   nColWidth)
{
posCurr.x   =   posStart.x;
posCurr.y   +=   nRowHeight;
}
}
}
BOOL   IsChineseChar(LPCTSTR   str)
{
return   *str   <   0   &&   (*(str   +   1))   <   0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: