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

本文介绍了一个用于绘制多行文本的函数intDrawMultLineText,它处理了文本的换行、缩进和中文字符宽度计算。通过实例代码演示了如何根据字体测量调整行高,适合理解Windows API中的文本渲染技巧。
摘要由CSDN通过智能技术生成


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

https://www.cnblogs.com/aoyihuashao/archive/2010/04/26/1720942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值