📄 miniwordview.cpp
字号:
current->words+=nChar;//将输入字符加到字符串后面
m_ptOrigin.x+=tb.tmMaxCharWidth;
SetCaretPos(m_ptOrigin);
CString char1;
int i;
char1=buffer1;
char1.Insert(0,nChar);//将buffer中字符串加到nchar后面
if(char1.GetLength()>1){//当插入字符的位置在某一行中间时
clr=dc.SetTextColor(dc.GetBkColor());
for(i=0;i<=char1.GetLength()-1;i++)//先输出空格覆盖后面内容
dc.TextOut(m_ptOrigin.x+(i-1)*tb.tmMaxCharWidth,m_ptOrigin.y," ");
dc.SetTextColor(clr);
for(i=0;i<=char1.GetLength()-1;i++){//在逐个输出字符
dc.TextOut(m_ptOrigin.x-tb.tmMaxCharWidth,m_ptOrigin.y,char1.GetAt(i));
m_ptOrigin.x+=tb.tmMaxCharWidth;
}
m_ptOrigin.x-=tb.tmMaxCharWidth*char1.GetLength();
}
else//如果插入字符的位置在某行最后
dc.TextOut(m_ptOrigin.x-tb.tmMaxCharWidth,m_ptOrigin.y,char1);
}
SetCaretPos(m_ptOrigin);
CScrollView::OnChar(nChar, nRepCnt, nFlags);
}
void CMiniwordView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MouseDown=1;
int x,y,i;
word* m;
CPoint point1;//point1是用来标准化插入符位置的
point1.x=0;
point1.y=0;
m=First;
CClientDC dc(this);
TEXTMETRIC ta;
dc.GetTextMetrics(&ta);
OnPrepareDC(&dc);
x=point.x/ta.tmMaxCharWidth;//标准化鼠标点击的位置
y=point.y/ta.tmHeight;
point.x=x*ta.tmMaxCharWidth;
point.y=y*ta.tmHeight;
if(BufferSignal1==0){//如果前一次操作为鼠标点击
current->words+=buffer1;//将缓存加回字符串
buffer1.Empty();
}
//处理光标位置的问题
for(i=1;i<=y&&m!=NULL;i++){//检验行数
m=m->downlink;
}
current=m;//currrent定为当前插入符所在行
if(BufferSignal1==0&&m!=NULL&&x<=(m->words.GetLength()+buffer1.GetLength())){//若行数小于等于最后一行且该行的字符数合格则改变光标位置
SetCaretPos(point);
m_ptOrigin=point;
//要处理中途插入的问题
buffer1.Empty();
buffer1=m->words.Right(m->words.GetLength()-x);//将光标后的字符存到buffer内
m->words=m->words.Left(x);//将该行字符截取插入符之前的内容
BufferSignal1=0;//缓存标识符还是0
}
else if(BufferSignal1==1&&m!=NULL&&x<=(m->words.GetLength())){//若缓存标识符为1且行数合格且该行的字符数合格则改变光标位置
SetCaretPos(point);
m_ptOrigin=point;
//要处理中途插入的问题
buffer1.Empty();
buffer1=m->words.Right(m->words.GetLength()-x);//将光标后的字符存到buffer内
m->words=m->words.Left(x);
BufferSignal1=0;//缓存标识符改为0
}
else{//若鼠标点击位置无字符则置末尾点
word* temp3;
temp3=First;
int k,l;
for(k=0;temp3->downlink!=NULL;k++)//遍历到最后一行
temp3=temp3->downlink;
l=temp3->words.GetLength();
point1.x=l*ta.tmMaxCharWidth;
point1.y=k*ta.tmHeight;
SetCaretPos(point1);//插入符定为最后一个字符后
m_ptOrigin=point1;
current=temp3;
}
CScrollView::OnLButtonDown(nFlags, point);
}
void CMiniwordView::OnCancelMode()
{
CScrollView::OnCancelMode();
// TODO: Add your message handler code here
}
void CMiniwordView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
TEXTMETRIC tc;
dc.GetTextMetrics(&tc);
OnPrepareDC(&dc);
int i;
if(0x26==nChar){//响应向上键
if(m_ptOrigin.y>=tc.tmHeight)
m_ptOrigin.y-=tc.tmHeight;
}
if(0x28==nChar)//响应向下键
m_ptOrigin.y+=tc.tmHeight;
if(0x25==nChar)//响应向左键
m_ptOrigin.x-=tc.tmMaxCharWidth;
if(0x27==nChar)//响应向右键
m_ptOrigin.x+=tc.tmMaxCharWidth;
if(0x25==nChar||0x26==nChar||0x27==nChar||0x28==nChar){
SetCaretPos(m_ptOrigin);
if(0x26==nChar||0x28==nChar)
current->words+=buffer1;
buffer1.Empty();
word* temp13=new word;
temp13=First;
int i;
for(i=1;i<=m_ptOrigin.y/tc.tmMaxCharWidth;i++)
temp13=temp13->downlink;
current=temp13;
buffer1=temp13->words.Right(temp13->words.GetLength()-m_ptOrigin.x/tc.tmMaxCharWidth);
BufferSignal1=0;
}
if(0x2E==nChar&&!buffer1.IsEmpty()){//处理删除键
buffer1=buffer1.Mid(1);//将buffer1中第一个字符删掉
for(i=0;i<=1024;i++)//用空格覆盖后面字符
dc.TextOut(m_ptOrigin.x+i*tc.tmMaxCharWidth,m_ptOrigin.y," ");
for(i=0;i<=buffer1.GetLength()-1;i++)//输出新的buffer1
dc.TextOut(m_ptOrigin.x+i*tc.tmMaxCharWidth,m_ptOrigin.y,buffer1.GetAt(i));
}
CScrollView::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CMiniwordView::OnInitialUpdate()
{
SetScrollSizes(MM_TEXT,CSize(800,600));//创建竖直滚动条
CScrollView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
}
void CMiniwordView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::CalcWindowRect(lpClientRect, nAdjustType);
}
void CMiniwordView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
TEXTMETRIC te;
dc.GetTextMetrics(&te);
OnPrepareDC(&dc);
if(point.x/te.tmMaxCharWidth!=m_ptOrigin.x/te.tmMaxCharWidth){//当鼠标抬起的坐标与鼠标按下的坐标不同时
int i,x1,x2;
current->words+=buffer1;//将buffer1加回当前行
x1=m_ptOrigin.x/te.tmMaxCharWidth;
x2=point.x/te.tmMaxCharWidth;
buffer1=current->words.Right(current->words.GetLength()-x2);//将buffer1设为鼠标抬起位置后面的字符
current->words=current->words.Left(x2);//当前行字符设为鼠标抬起位置之前的字符
Chosen=current->words.Mid(x1);//选择的字符为两点之间的字符
dc.SetTextColor(RGB(0,0,255));//设置字符颜色
for(i=0;i<=Chosen.GetLength()-1;i++)//输出变色后的选中字符
dc.TextOut(m_ptOrigin.x+i*te.tmMaxCharWidth,m_ptOrigin.y,Chosen.GetAt(i));
BufferSignal1=0;
}
CScrollView::OnLButtonUp(nFlags, point);
}
void CMiniwordView::OnCaptureChanged(CWnd *pWnd)
{
// TODO: Add your message handler code here
Invalidate();//当滚动条移动时重绘
UpdateWindow();
CScrollView::OnCaptureChanged(pWnd);
}
void CMiniwordView::OnSearch()
{
// TODO: Add your command handler code here
CSearch dlg;
dlg.DoModal();
CClientDC dc(this);
TEXTMETRIC ta;
dc.GetTextMetrics(&ta);
OnPrepareDC(&dc);
word* temp11;
temp11=First;
int m=0,x,y=0;
while(temp11!=NULL&&m==0){//在整个存储结构中查找字符
x=temp11->words.Find(Searches,0);//x为查找到的存储位置
if(x>=0){//如果找到
m=1;
m_ptOrigin.x=x*ta.tmMaxCharWidth;//记录该字符位置并退出循环
m_ptOrigin.y=y*ta.tmHeight;
}
y++;//否则继续查找下一行
temp11=temp11->downlink;
}
CreateSolidCaret(ta.tmAveCharWidth/8,ta.tmHeight);
ShowCaret();
if(m==0&&!Searches.IsEmpty()){//如果未找到弹出提示框
AfxMessageBox("找不到该字符!");
m_ptOrigin.x=0;
m_ptOrigin.y=0;
}
SetCaretPos(m_ptOrigin);
Searches.Empty();//将查找用的字符串置为空
}
void CMiniwordView::OnFileNew() //新建
{
// TODO: Add your command handler code here
CClientDC dc(this);
TEXTMETRIC tc;
dc.GetTextMetrics(&tc);
OnPrepareDC(&dc);
word* tempx;
tempx=First;
int x=0,y=0;
while(tempx!=NULL){//用空格覆盖各行内容
for(x=0;x<=1024;x++)
dc.TextOut(x*tc.tmMaxCharWidth,y*tc.tmHeight," ");
y++;
tempx=tempx->downlink;
}
First->downlink=NULL;//将首行的下行指针置为空
First->words.Empty();
Invalidate();//更新窗口
UpdateWindow();
m_ptOrigin.x=0;
m_ptOrigin.y=0;
SetCaretPos(m_ptOrigin);//插入符置为文件开头
}
void CMiniwordView::OnReplace()
{
// TODO: Add your command handler code here
CREPLACE dxg;
dxg.DoModal();
CClientDC dc(this);
TEXTMETRIC ta;
dc.GetTextMetrics(&ta);
OnPrepareDC(&dc);
word* temp12;
temp12=First;
int m=0,x,y=0;
while(temp12!=NULL&&m==0){//在整个存储结构中查找字符
x=temp12->words.Find(Searches,0);//x为查找到的存储位置
if(x>=0){//如果找到
m=1;
m_ptOrigin.x=x*ta.tmMaxCharWidth;//记录该字符位置并退出循环
m_ptOrigin.y=y*ta.tmHeight;
temp12->words.Replace(Searches,Replacer);//替换字符串
}
y++;//否则继续查找下一行
temp12=temp12->downlink;
}
CreateSolidCaret(ta.tmAveCharWidth/8,ta.tmHeight);
ShowCaret();
if(m==0&&!Searches.IsEmpty()){//如果未找到弹出提示框
AfxMessageBox("找不到该字符!");
m_ptOrigin.x=0;
m_ptOrigin.y=0;
}
SetCaretPos(m_ptOrigin);
Searches.Empty();//将查找用的字符串置为空
Replacer.Empty();
}
void CMiniwordView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CScrollView::OnMouseMove(nFlags, point);
}
void CMiniwordView::OnEditCopy()
{
// TODO: Add your command handler code here
Copyed=Chosen;
}
void CMiniwordView::OnEditPaste()
{
// TODO: Add your command handler code here
int x,y;
CClientDC dc(this);
TEXTMETRIC ta;
dc.GetTextMetrics(&ta);
OnPrepareDC(&dc);
for(x=1;x<=1024;x++)//粘贴时先把原字符用空格覆盖
dc.TextOut(m_ptOrigin.x+x*ta.tmMaxCharWidth,m_ptOrigin.y," ");
for(x=0;x<=Copyed.GetLength()-1;x++)//输出复制的字符
dc.TextOut(m_ptOrigin.x+x*ta.tmMaxCharWidth,m_ptOrigin.y,Copyed.GetAt(x));
y=Copyed.GetLength();
for(x=0;x<=buffer1.GetLength()-1;x++)//输出原字符
dc.TextOut(m_ptOrigin.x+(x+y)*ta.tmMaxCharWidth,m_ptOrigin.y,buffer1.GetAt(x));
current->words+=Copyed;
}
void CMiniwordView::OnEditCut()
{
// TODO: Add your command handler code here
Copyed=Chosen;
CClientDC dc(this);
TEXTMETRIC tq;
dc.GetTextMetrics(&tq);
OnPrepareDC(&dc);
int x;
for(x=0;x<=1024;x++)//剪切时先把原字符用空格覆盖
dc.TextOut(m_ptOrigin.x+x*tq.tmMaxCharWidth,m_ptOrigin.y," ");
for(x=0;x<=buffer1.GetLength()-1;x++)//输出缓冲中的字符
dc.TextOut(m_ptOrigin.x+x*tq.tmMaxCharWidth,m_ptOrigin.y,buffer1.GetAt(x));
current->words=current->words.Left(current->words.GetLength()-Copyed.GetLength());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -