CRichedit在结尾添加一行,同时设置字体,大小及颜色,并在添加后自动滚屏到末行,


int CJWChatServerDlg::GetNumVisibleLines(CRichEditCtrl* pCtrl)
{
	CRect rect;
	long nFirstChar, nLastChar;
	long nFirstLine, nLastLine;

	// Get client rect of rich edit control
	pCtrl->GetClientRect(rect);

	// Get character index close to upper left corner
	nFirstChar = pCtrl->CharFromPos(CPoint(0, 0));

	// Get character index close to lower right corner
	nLastChar = pCtrl->CharFromPos(CPoint(rect.right, rect.bottom));
	if (nLastChar < 0)
	{
		nLastChar = pCtrl->GetTextLength();
	}

	// Convert to lines
	nFirstLine = pCtrl->LineFromChar(nFirstChar);
	nLastLine  = pCtrl->LineFromChar(nLastChar);

	return (nLastLine - nFirstLine);
}
int CJWChatServerDlg::AppendToLogAndScroll(const CString& csMsg, COLORREF color, int iFontSize)
{
	long nVisible = 0;
	long nInsertionPoint = 0;
	CHARFORMAT cf;
	m_ChatRoom.GetSelectionCharFormat(cf);
	// Initialize character format structure
	cf.cbSize = sizeof(CHARFORMAT);
	static int iHeight = 100;
	cf.dwMask|=CFM_BOLD;
	cf.dwEffects |=CFE_BOLD;//设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
	cf.dwEffects &= ~CFE_AUTOCOLOR;
	cf.dwMask |= CFM_COLOR;
	cf.crTextColor = color;
	cf.dwMask|=CFM_SIZE;
	cf.yHeight = iHeight;//设置高度
	iHeight += 50;
	cf.dwMask |=CFM_FACE;
	_tcscpy(cf.szFaceName ,_T("微软雅黑"));//设置字体
	// Set insertion point to end of text
	nInsertionPoint = m_ChatRoom.GetWindowTextLength();
	m_ChatRoom.SetSel(nInsertionPoint, -1);

	// Set the character format
	m_ChatRoom.SetSelectionCharFormat(cf);

	// Replace selection. Because we have nothing
	// selected, this will simply insert
	// the string at the current caret position.
	m_ChatRoom.ReplaceSel(csMsg);

	// Get number of currently visible lines or maximum number of visible lines
	// (We must call GetNumVisibleLines() before the first call to LineScroll()!)
	nVisible   = GetNumVisibleLines(&m_ChatRoom);

	// Now this is the fix of CRichEditCtrl's abnormal behaviour when used
	// in an application not based on dialogs. Checking the focus prevents
	// us from scrolling when the CRichEditCtrl does so automatically,
	// even though ES_AUTOxSCROLL style is NOT set.
	if (&m_ChatRoom != m_ChatRoom.GetFocus())
	{
		m_ChatRoom.LineScroll(INT_MAX);
		m_ChatRoom.LineScroll(1 - nVisible);
	}

	return 0;
}

void CJWChatServerDlg::OnBnClickedButtonUpdate()
{
	static int i = 0;
	COLORREF clrs[4] = {RGB(200, 25, 25), RGB(240, 155, 155), RGB(80, 205, 25), RGB(10, 25, 255)};
	AppendToLogAndScroll(_T("多说一下,自从 Lamport 在 1998 年发表 Paxos 算法后,无论何种改进,其重点依然是在消息延迟与性能、吞吐量之间作出各种权衡。\n"), clrs[i], 20);
	i++;
	i %= 4;
}





  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值