📄 painterview.cpp
字号:
else
pCmdUI->SetCheck(FALSE);
pCmdUI->Enable(!m_bModify);
}
void CPainterView::OnFileSave()
{
// TODO: Add your command handler code here
if(bFileSaved)
{
// 已经有文件名,修改图形文件
Save();
}
else
OnFileSaveAs();
}
void CPainterView::OnFileSaveAs()
{
// TODO: Add your command handler code here
CFileDialog file(0, // 1-文件打开, 0-文件另存为
".hpy|*.*",
NULL,
OFN_OVERWRITEPROMPT | OFN_NOREADONLYRETURN ,
"绘图文件 (*.hpy)|*.hpy|All Files (*.*)|*.*||",
NULL);
if(file.DoModal()==IDOK)
{
path = file.GetPathName();
fileName = file.GetFileTitle();
Save();
if(!AddIcon())
MessageBox("添加记录失败!","错误");
}
}
//处理文件打开命令
void CPainterView::OnFileOpen()
{
// TODO: Add your command handler code here
CPainterDoc *pDoc=GetDocument();
CFileDialog file(1, // 1-文件打开, 0-文件另存为
".hpy|*.*",
NULL,
OFN_OVERWRITEPROMPT ,
"绘图文件 (*.hpy)|*.hpy|All Files (*.*)|*.*||",
NULL);
if(file.DoModal()==IDOK)
{
path = file.GetPathName();
fileName=file.GetFileTitle();
CFile file;
if(!file.Open(path,0x0000,NULL)) return;
if(!pDoc->RecordList.IsEmpty())//如果文档数据内容非空则清空之
pDoc->RecordList.RemoveAll();
DWORD CurFilePointer=file.GetPosition();//得到文件指针当前位置
DWORD FileEndPointer=file.GetLength();//得到文件长度
while(CurFilePointer<FileEndPointer)//如果文件指针到达文件尾则停止
{
BYTE* style;//图元类型
style=new BYTE;
file.Read(style,sizeof(BYTE));//读出图元类型
pDoc->RecordList.AddTail(style);//将图元类型加入链表
switch(*style)
{
//如果图元是直线
case LINE:
LINENODE* line;
line=new LINENODE;
file.Read(line,sizeof(LINENODE));
pDoc->RecordList.AddTail(line);
break;
//如果图元是矩形
case RECTANGLE:
RECTANGLENODE* rectangle;
rectangle=new RECTANGLENODE;
file.Read(rectangle,sizeof(RECTANGLENODE));;
pDoc->RecordList.AddTail(rectangle);
break;
//如果是填充
case FILL:
FILLNODE* fill;
fill=new FILLNODE;
file.Read(fill,sizeof(FILLNODE));
pDoc->RecordList.AddTail(fill);
break;
//如果图元是文本
case TEXT:
TEXTNODE* text;
text=new TEXTNODE;
file.Read(text,sizeof(TEXTNODE));
pDoc->RecordList.AddTail(text);
break;
//如果是椭圆
case ELLIPSE:
ELLIPSENODE* ellipse;
ellipse=new ELLIPSENODE;
file.Read(ellipse,sizeof(ELLIPSENODE));
pDoc->RecordList.AddTail(ellipse);
break;
//如果是三角形
case TRIANGLE:
TRIANGLENODE* triangle;
triangle=new TRIANGLENODE;
file.Read(triangle,sizeof(TRIANGLENODE));
pDoc->RecordList.AddTail(triangle);
break;
}
CurFilePointer=file.GetPosition();//获得当前文件指针位置
}
file.Close();//关闭文件
}
bFileSaved=TRUE;
pDoc->UpdateAllViews(NULL,0,NULL);//重绘窗口
GetParent()->SetWindowText(fileName);
}
//处理文件保存
void CPainterView::Save()
{
CFile save;
//////////////////////////////////////////
/* //
modeRead = 0x0000, //
modeWrite = 0x0001, //
modeReadWrite = 0x0002, //
modeCreate = 0x1000, //
*/ //
//////////////////////////////////////////
save.Open(path,0x0001|0x1000,NULL);
CPainterDoc *pDoc=GetDocument();
POSITION pos=pDoc->RecordList.GetHeadPosition();
while(pos!=NULL)
{
BYTE *style;
style=(BYTE*)pDoc->RecordList.GetNext(pos);
save.Write(style,sizeof(BYTE));
switch(*style)
{
case LINE:
save.Write((LINENODE*)pDoc->RecordList.GetAt(pos),sizeof(LINENODE));
break;
case RECTANGLE:
save.Write((RECTANGLENODE*)pDoc->RecordList.GetNext(pos),sizeof(RECTANGLENODE));
break;
case FILL:
save.Write((FILLNODE*)pDoc->RecordList.GetNext(pos),sizeof(FILLNODE));
break;
case TEXT:
save.Write((TEXTNODE*)pDoc->RecordList.GetNext(pos),sizeof(TEXTNODE));
break;
case TRIANGLE:
save.Write((TRIANGLENODE*)pDoc->RecordList.GetNext(pos),sizeof(TRIANGLENODE));
break;
case ELLIPSE:
save.Write((ELLIPSENODE*)pDoc->RecordList.GetNext(pos),sizeof(ELLIPSENODE));
break;
}
}
save.Close();
pDoc->SetModifiedFlag(FALSE);
bFileSaved=TRUE;
GetParent()->SetWindowText(fileName);
}
void CPainterView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_SHIFT)////如果按下Shift键
bShiftDown=TRUE;
CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CPainterView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_SHIFT)//如果没有按下Shift键
bShiftDown=FALSE;
CScrollView::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CPainterView::UndoMove(MOVENODE *move)
{
CPainterDoc *pDoc=GetDocument();
POSITION pos=pDoc->RecordList.GetHeadPosition();//得到链表头节点的指针
int x_off=move->x_off;
int y_off=move->y_off;
int i;
BOOL Do=FALSE;//判断图元是否属于移动集
while(pos!=NULL)
{
BYTE *style;
style=(BYTE *)pDoc->RecordList.GetNext(pos);//得到图元类型指针
switch(*style)
{
case LINE:
LINENODE *line;
line=(LINENODE *)pDoc->RecordList.GetNext(pos);//从链表中取出图元数据
//移动集中是否存在直线图元的索引
for(i=0;i<move->iCompNum;i++)
if(line->Index==move->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)//如果是撤消
{
line->End.x-=x_off;
line->End.y-=y_off;
line->Start.x-=x_off;
line->Start.y-=y_off;
line->bDo=7;
}
else//如果是恢复
{
line->End.x+=x_off;
line->End.y+=y_off;
line->Start.x+=x_off;
line->Start.y+=y_off;
line->bDo=5;
}
}
break;
case RECTANGLE:
RECTANGLENODE *rectangle;
rectangle=(RECTANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<move->iCompNum;i++)
if(rectangle->Index==move->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
{
rectangle->rT.bottom-=y_off;
rectangle->rT.top-=y_off;
rectangle->rT.right-=x_off;
rectangle->rT.left-=x_off;
rectangle->bDo=7;
}
else
{
rectangle->rT.bottom+=y_off;
rectangle->rT.top+=y_off;
rectangle->rT.right+=x_off;
rectangle->rT.left+=x_off;
rectangle->bDo=5;
}
}
break;
case ELLIPSE:
ELLIPSENODE *ellipse;
ellipse=(ELLIPSENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<move->iCompNum;i++)
if(ellipse->Index==move->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
{
ellipse->rT.bottom-=y_off;
ellipse->rT.top-=y_off;
ellipse->rT.right-=x_off;
ellipse->rT.left-=x_off;
ellipse->bDo=7;
}
else
{
ellipse->rT.bottom+=y_off;
ellipse->rT.top+=y_off;
ellipse->rT.right+=x_off;
ellipse->rT.left+=x_off;
ellipse->bDo=5;
}
}
break;
case TRIANGLE:
TRIANGLENODE *triangle;
triangle=(TRIANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<move->iCompNum;i++)
if(triangle->Index==move->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
{
triangle->rT.bottom-=y_off;
triangle->rT.top-=y_off;
triangle->rT.right-=x_off;
triangle->rT.left-=x_off;
triangle->bDo=7;
}
else
{
triangle->rT.bottom+=y_off;
triangle->rT.top+=y_off;
triangle->rT.right+=x_off;
triangle->rT.left+=x_off;
triangle->bDo=5;
}
}
break;
case TEXT:
TEXTNODE *text;
text=(TEXTNODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<move->iCompNum;i++)
if(text->Index==move->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
{
text->point.x-=x_off;
text->point.y-=y_off;
text->bDo=7;
}
else
{
text->point.x+=x_off;
text->point.y+=y_off;
text->bDo=5;
}
}
break;
}
Do=FALSE;
}
}
void CPainterView::UndoPaste(PASTENODE *paste)
{
CPainterDoc *pDoc=GetDocument();
POSITION pos=pDoc->RecordList.GetHeadPosition();
int i;
BOOL Do=FALSE;
while(pos!=NULL)
{
BYTE *style;
style=(BYTE *)pDoc->RecordList.GetNext(pos);
switch(*style)
{
case LINE:
LINENODE *line;
line=(LINENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<paste->iCompNum;i++)
if(line->Index==paste->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
line->bDo=-3;
else
line->bDo=3;
}
break;
case RECTANGLE:
RECTANGLENODE *rectangle;
rectangle=(RECTANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<paste->iCompNum;i++)
if(rectangle->Index==paste->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
rectangle->bDo=-3;
else
rectangle->bDo=3;
}
break;
case ELLIPSE:
ELLIPSENODE *ellipse;
ellipse=(ELLIPSENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<paste->iCompNum;i++)
if(ellipse->Index==paste->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
ellipse->bDo=-3;
else
ellipse->bDo=3;
}
break;
case TRIANGLE:
TRIANGLENODE *triangle;
triangle=(TRIANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<paste->iCompNum;i++)
if(triangle->Index==paste->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
triangle->bDo=-3;
else
triangle->bDo=3;
}
break;
case TEXT:
TEXTNODE *text;
text=(TEXTNODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<paste->iCompNum;i++)
if(text->Index==paste->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
text->bDo=-3;
else
text->bDo=3;
}
break;
}
Do=FALSE;
}
}
void CPainterView::UndoCut(CUTNODE *cut)
{
CPainterDoc *pDoc=GetDocument();
POSITION pos=pDoc->RecordList.GetHeadPosition();
int i;
BOOL Do=FALSE;
while(pos!=NULL)
{
BYTE *style;
style=(BYTE *)pDoc->RecordList.GetNext(pos);
switch(*style)
{
case LINE:
LINENODE *line;
line=(LINENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<cut->iCompNum;i++)
if(line->Index==cut->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
line->bDo=6;
else
line->bDo=-6;
}
break;
case RECTANGLE:
RECTANGLENODE *rectangle;
rectangle=(RECTANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<cut->iCompNum;i++)
if(rectangle->Index==cut->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType=UNDO)
rectangle->bDo=6;
else
rectangle->bDo=-6;
}
break;
case ELLIPSE:
ELLIPSENODE *ellipse;
ellipse=(ELLIPSENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<cut->iCompNum;i++)
if(ellipse->Index==cut->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
ellipse->bDo=6;
else
ellipse->bDo=-6;
}
break;
case TRIANGLE:
TRIANGLENODE *triangle;
triangle=(TRIANGLENODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<cut->iCompNum;i++)
if(triangle->Index==cut->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
triangle->bDo=6;
else
triangle->bDo=-6;
}
break;
case TEXT:
TEXTNODE *text;
text=(TEXTNODE *)pDoc->RecordList.GetNext(pos);
for(i=0;i<cut->iCompNum;i++)
if(text->Index==cut->Index[i])
Do=TRUE;
if(Do)
{
if(*DrawType==UNDO)
text->bDo=6;
else
text->bDo=-6;
}
break;
}
Do=FALSE;
}
}
void CPainterView::OnDrawNotilt()
{
// TO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -