📄 chatricheditctrl.cpp
字号:
/*
Copyright (c) 2005 william.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License".
*/
#include "stdafx.h"
// CChatRichEditCtrl
IMPLEMENT_DYNAMIC(CChatRichEditCtrl, CRichEditCtrl)
CChatRichEditCtrl::CChatRichEditCtrl()
{
memset(&cf,0,sizeof(cf));
cf.dwMask = CFM_COLOR|CFM_BOLD|CFM_SIZE|CFM_FACE;
wcscpy(cf.szFaceName,L"宋体");
cf.yHeight = 173;
//I don't knwon how to get the edit wndow height in programme
//the number 59 is directly get from .rc file.
//and, I don't sure if the length-units matching
//if you known,please awake me.
UINT windHeightPixel = 59;
HDC DC =::GetWindowDC(0);
float yPixelsPerInch =(float)::GetDeviceCaps(DC,LOGPIXELSY);
::ReleaseDC(0,DC);
//1440 twips per inch
float fontHeightPixel = yPixelsPerInch*172/1440;
WinChatLine = (UINT)(windHeightPixel/fontHeightPixel);
//actually the line of chat-text-window is 1 more line then
//WinChatLine, but it's can't work well when the text is full line.
//++WinChatLine;
}
CChatRichEditCtrl::~CChatRichEditCtrl()
{
}
BEGIN_MESSAGE_MAP(CChatRichEditCtrl, CRichEditCtrl)
ON_WM_VSCROLL()
ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()
void CChatRichEditCtrl::ShowText(int BeginLine,int EndLine)
{
if(BeginLine<0)BeginLine=0;
if(EndLine<0)EndLine=0;
else if(EndLine>ChatArray.GetCount())EndLine = (int)ChatArray.GetCount();
if(BeginLine>EndLine)BeginLine=EndLine;
DUMP2(L"display:",BeginLine,EndLine-1);
CString t;
SetDefaultCharFormat(cf);
for(int i = BeginLine;i<EndLine;i++)
{
t.Append(ChatArray.GetAt(i));
}
SetWindowText(t);
//keep the format for each line;
long from=0;
for(int i = BeginLine;i<EndLine;i++)
{
long to = ChatArray.GetAt(i).GetLength();
SetSel(from,to+from);
DUMP2(L"effect from to",from,to+from);
cf.dwEffects = EffectArray.GetAt(i).Effects;
cf.crTextColor = EffectArray.GetAt(i).TextColor;
SetSelectionCharFormat(cf);
from+=to;
}
SetSel(0,0);
UpdateWindow();
//do update now
Sleep(0);
}
void CChatRichEditCtrl::AppendText(const CString& str,COLORREF cr,bool bold)
{
ChatArray.Add(str);
EffectArray.Add(CPair(bold,cr));
UINT OldBeginLine = GetScrollPos(SB_VERT);
int max = (int)ChatArray.GetCount();
ASSERT(max>0);
#ifdef _DEBUG
if(max>20)MowArray(),--max;
#else
if(max>200)MowArray(),--max;
#endif
max = max - WinChatLine;
max = max<0?0:max;
DUMP1(L"range:",max);
SetScrollRange(SB_VERT,0,max);
//if scrollbar on the very bottom,auto scroll it;
//otherwise keep it,so reader can keep reading the history and
//not be disrupted by auto scroll
if((int)OldBeginLine == max-1)
{
//Auto scroll
SetScrollPos(SB_VERT,OldBeginLine+1);
ShowText(OldBeginLine+1);
#ifdef _DEBUG
UINT tNowPos = GetScrollPos(SB_VERT);
UINT tEndLine = GetEndLine(tNowPos);
ASSERT((UINT)max == tEndLine-WinChatLine);
#endif
}
else
{
SetScrollPos(SB_VERT,OldBeginLine);
ShowText(OldBeginLine);
}
}
// CChatRichEditCtrl 消息处理程序
void CChatRichEditCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* )
{
UINT pos = GetScrollPos(SB_VERT);
int newPos=0;
switch(nSBCode)
{
case SB_THUMBTRACK:
case SB_THUMBPOSITION : newPos=nPos;break;
case SB_LINEDOWN :newPos=pos+1;break;
case SB_LINEUP :newPos=pos-1;break;
case SB_PAGEDOWN :newPos=pos+5;break;
case SB_PAGEUP :newPos=pos-5;break;
default:newPos=pos;break;
}
if(IsOutOfRange(newPos+WinChatLine))return;
SetScrollPos(SB_VERT,newPos);
DUMP1(L"pos move to:",newPos);
CPoint cur(GetCaretPos());
ShowText(newPos);
SetCaretPos(cur);
}
BOOL CChatRichEditCtrl::OnMouseWheel(UINT , short zDelta, CPoint)
{
int pos = GetScrollPos(SB_VERT);
pos = pos-(zDelta/WHEEL_DELTA);
if(pos<0)return true;
OnVScroll(SB_THUMBTRACK,pos,0);
return true;
//return CRichEditCtrl::OnMouseWheel(nFlags, zDelta, pt);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -