您的位置:首页 > 其它

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

2014-01-21 12:35 381 查看
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;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐