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

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

你可能感兴趣的:(text)