⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 direction2doc.cpp

📁 数据结构求最短路径vc++ 实现手动作图
💻 CPP
字号:
// direction2Doc.cpp : implementation of the CDirection2Doc class
//

#include "StdAfx.h"
#include "direction2.h"
#include "afxtempl.h"
#include "direction2Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDirection2Doc

IMPLEMENT_DYNCREATE(CDirection2Doc, CDocument)

BEGIN_MESSAGE_MAP(CDirection2Doc, CDocument)
	//{{AFX_MSG_MAP(CDirection2Doc)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_COMMAND(ID_EDIT_CLEARALL, OnEditClearall)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CLEARALL, OnUpdateEditClearall)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()





/////////////////////////////////////////////////////////////////////////////
// CDirection2Doc construction/destruction

CDirection2Doc::CDirection2Doc()
{
	// TODO: add one-time construction code here

}

CDirection2Doc::~CDirection2Doc()
{
}










BOOL CDirection2Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CDirection2Doc serialization

void CDirection2Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here

		m_CArray.Serialize(ar);

	}
	else
	{

		// TODO: add loading code here
		m_CArray.Serialize(ar);
	}
}





///////////CBasepic基类实现
IMPLEMENT_SERIAL(CBasepic, CObject , 1)


void CBasepic::Serialize(CArchive& ar)
{

	if(ar.IsStoring())
    {	ar.Write(&m_text,sizeof(m_text));  //把字体式样写入文件
		ar<<m_x1<<m_y1<<m_name<<m_color;
	}
	else
	{ar.Read(&m_text,sizeof(m_text));  //把字体式样写入文件
		ar>>m_x1>>m_y1>>m_name>>m_color;
	}
}



///////////CText类的实现代码

IMPLEMENT_SERIAL(CText, CBasepic , 1)


CText::CText(int x1,int y1,CString name,LOGFONT text,COLORREF color)
{
m_text= text;   //字体式样字体大小
m_name=name;    //标题
m_x1=x1;
m_y1=y1;  //图坐标范围
m_color=color;   //作图的默认颜色
selete=false;
}



void CText::Serialize(CArchive& ar)
{
	CBasepic::Serialize(ar);

}


	


void CText::Draw(CDC *pDC)
{
CFont Font , *pOldFont;
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);


//创建并选入字体

//判断字体是否被选中
if(selete)  //如果被选中则有矩形框包围
{
//离字 的距离3个像素,
int x1 = m_x1-5 ;
int y1 = m_y1 - 5 ;
int x2 = m_x1 + m_text.lfWidth*sizeof(m_name)+5;
int y2 = m_y1 + m_text.lfHeight+5;

pDC->SetBkColor(TRANSPARENT);  //透明背景
pDC->SelectStockObject(NULL_BRUSH);

CPen Pen ,*pOldPen;

Pen.CreatePen(PS_INSIDEFRAME,1,RGB(255,0,0));
pOldPen=pDC->SelectObject(&Pen); 
//绘制矩形
pDC->Rectangle(x1,y1,x2,y2);

//恢复原来的画笔和画刷
pDC->SelectObject(pOldPen);
}



Font.CreateFontIndirect(&m_text);
pOldFont=pDC->SelectObject(&Font);
pDC->SetTextColor(m_color);
pDC->SetBkMode(TRANSPARENT); ///设计透明背景的字体
pDC->TextOut(m_x1,m_y1,m_name);
pDC->SelectObject(pOldFont);  //恢复原来的字体

}




CRect CText::GetDimRect()
{

int x1 = m_x1 - 5;
int y1 = m_y1 -5;
int x2 = m_y1 + m_text.lfWidth*sizeof(m_name)+5;
int y2 = m_y1 + m_text.lfHeight+5;

return new CRect(x1,y1,x2,y2);
}






/////////////CLine 类实现的代码

IMPLEMENT_SERIAL(CLine,CBasepic,1)

//构造函数,初始化数据成员

CLine::CLine(int x1,int y1,CString name,LOGFONT  text,COLORREF color,int x2,int y2,int index1,int index2,int width,COLORREF linecolor,int linestyle)
{
lindex1=index1;
lindex2=index2;
linewidth=width;
m_linestyle=linestyle;
m_x2=x2;
m_y2=y2;
m_linecolor=linecolor;
m_text= text;   //字体式样字体大小
m_name=name;    //标题
m_x1=x1;
m_y1=y1;  //图坐标范围
m_color=color;   //作图的默认颜色
selete=false;

m_zhi=0;

}

void CLine::Serialize(CArchive& ar)
{
	CBasepic::Serialize(ar);

	if(ar.IsStoring())
	{
  
		ar<<m_x2<<m_y2<<lindex1<<lindex2<<linewidth<<m_linecolor<<m_linestyle<<m_zhi;
	
	}

	else
	{
	
	ar>>m_x2>>m_y2>>lindex1>>lindex2>>linewidth>>m_linecolor>>m_linestyle>>m_zhi;
	}

}

void CLine::Draw(CDC * pDC)
{
CPen Pen ,*pOldPen;
Pen.CreatePen(m_linestyle,linewidth,m_linecolor);  //创建画笔
pOldPen=pDC->SelectObject(&Pen);           //选入新的画笔,并保存原来的画笔

///绘制直线

pDC->MoveTo(m_x1,m_y1);
pDC->LineTo(m_x2,m_y2);

//恢复原来的画笔
pDC->SelectObject(pOldPen);

if(selete) //如果被选中了,则这样显示
{
//Pen.CreatePen(PS_SOLID,linewidth,RGB(255,0,0));  //创建红色画笔
//pOldPen=pDC->SelectObject(&Pen);  

CBrush Brush,*pOldBrush;

CPen Pen ,*pOldPen;

///创建并选入画笔 

Pen.CreatePen(PS_INSIDEFRAME,1,RGB(255,0,0));
pOldPen=pDC->SelectObject(&Pen); 

//创建并选入画刷
Brush.CreateSolidBrush(RGB(255,0,0));
pOldBrush=pDC->SelectObject(&Brush); 

//绘制填充矩形
pDC->Rectangle(m_x1-3,m_y1-3,m_x1+3,m_y1+3);
pDC->Rectangle(m_x2-3,m_y2-3,m_x2+3,m_y2+3);
//恢复原来的画笔和画刷
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}




CFont Font , *pOldFont;

//创建并选入字体

Font.CreateFontIndirect(&m_text);
pOldFont=pDC->SelectObject(&Font);

pDC->SetTextColor(m_color);
pDC->SetBkMode(TRANSPARENT); ///设计透明背景的字体
pDC->TextOut((m_x1+m_x2)/2-5,(m_y1+m_y2)/2-15,m_name);
pDC->SelectObject(pOldFont);  //恢复原来的字体
}


void CLine::Setpoint(int x,int y,int index)
{
if(index==lindex1)
{
m_x1=m_x1+x;
m_y1=m_y1+y;
}
else if(index==lindex2)
{
m_x2=m_x2+x;
m_y2=m_y2+y;
}
}

CRect CLine::GetDimRect()
{
	//(m_x1+m_x2)/2-5,(m_y1+m_y2)/2-15
return  CRect((m_x1+m_x2)/2-15,(m_y1+m_y2)/2-15,(m_x1+m_x2)/2+15,(m_y1+m_y2)/2+15);

}




//CRect类的实现代码


IMPLEMENT_SERIAL(CRectFill,CObject,1)

CRectFill::CRectFill(int x1,int y1,CString name,LOGFONT  text,COLORREF color,int width,int height,COLORREF rectcolor,COLORREF rectfillcolor)
{
m_text= text;   //字体式样字体大小
m_name=name;    //标题
m_x1=x1;
m_y1=y1;  //图坐标范围
m_color=color;   //作图的默认颜色
m_rectcolor=rectcolor;
m_rectfillcolor=rectfillcolor;
m_width=width;
m_height=height;
selete=false;
Linenum=0;
for(int i=0;i<200;i++)   //以便知道存放直线的数目
line[i]=-1;
m_zhi=0;
}


void CRectFill::Serialize(CArchive& ar)
{

	CBasepic::Serialize(ar);
	if(ar.IsStoring())
	{
    
		ar.Write(line,sizeof(int)*200);  //相关矩形索引写入文件
		ar<<Linenum<<m_height<<m_width<<m_rectcolor<<m_rectfillcolor<<m_zhi;

	
	}

	else
	{
	
		ar.Read(line,sizeof(int)*200);  //相关矩形索引写入文件
		ar>>Linenum>>m_height>>m_width>>m_rectcolor>>m_rectfillcolor>>m_zhi;
	}

}


void  CRectFill::Draw(CDC * pDC)
{

CBrush Brush,*pOldBrush;

CPen Pen ,*pOldPen;
COLORREF color=m_rectcolor;
///创建并选入画笔 
if(selete)
{
color=RGB(255,0,0);
}
Pen.CreatePen(PS_INSIDEFRAME,2,color);
pOldPen=pDC->SelectObject(&Pen); 


//创建并选入画刷
Brush.CreateSolidBrush(m_rectfillcolor);
pOldBrush=pDC->SelectObject(&Brush); 

//绘制填充矩形
pDC->Rectangle(m_x1-m_width,m_y1+m_height,m_x1+m_width,m_y1-m_height);

//恢复原来的画笔和画刷
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);





CFont Font , *pOldFont;

//创建并选入字体

Font.CreateFontIndirect(&m_text);
pOldFont=pDC->SelectObject(&Font);

pDC->SetTextColor(m_color);
pDC->SetBkMode(TRANSPARENT); ///设计透明背景的字体
pDC->TextOut(m_x1-m_width,m_y1-m_height-20,m_name);
pDC->SelectObject(pOldFont);  //恢复原来的字体
}




CRect CRectFill::GetDimRect()
{

	
int x1=m_x1-(int)(m_width/2);
int x2= m_x1+(int)(m_width/2); 
int y1=m_y1+(int)(m_height/2);
int y2=m_y1-(int)(m_height/2)-m_text.lfWidth;

return CRect(min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2));
}
		 
void CRectFill::Setpoint(int x,int y)
{
m_x1=m_x1+x;
m_y1=m_y1+y;
}

BOOL CRectFill::AddLine(int index)               //增加直线
 {
	 if(Linenum <= 200)
	 {
		 line[Linenum] = index;
		 Linenum = Linenum + 1;
		 return true;
	 }
     else
	return false;
     
 }   
 
CPoint CRectFill::Getmid()
{
	CPoint point;
    point.x = m_x1;
    point.y = m_y1;
	return point;
}

BOOL CRectFill::DeleteLine(int index)             //删除直线
 {
	 int  num = Linenum;
	 for(int i=0;i<Linenum+1;i++)
	 {
		 if(line[i] == index)
		 {
			 for(int j=i;j<Linenum;j++)
			 line[i] =line[i+1];
			 line[Linenum] =-1;
             Linenum=Linenum-1;
             break;
		}
	 
	 }

 if(num == Linenum)   //证明删除对象不成功
return false;
 else
return true;
 }



int CRectFill::Getline(int num)
{ 

return line[num];

}




CPoint CRectFill::GetHW()
{
CPoint point;
point.x  =m_height;
point.y = m_width; 

return point;
}
/////////////////////////////////////////////////////////////////////////////
// CDirection2Doc diagnostics

#ifdef _DEBUG
void CDirection2Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDirection2Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDirection2Doc commands


void CDirection2Doc::AddDraw(CBasepic *pDraw)  //增加直线对象无返回
{
	m_CArray.Add(pDraw);

	SetModifiedFlag();
}



CBasepic *CDirection2Doc::GetDraw(int index) //返回直线对象
{
	if(index < 0 || index > m_CArray.GetUpperBound())
		return 0;
	return (CBasepic *) m_CArray.GetAt(index);

}

int CDirection2Doc::GetDrawnum()//返回各对象的数目
{
	return m_CArray.GetSize();

}






void CDirection2Doc::DeleteContents() 
{
	// TODO: Add your specialized code here and/or call the base class
	int Index1 = m_CArray.GetSize();

	while(Index1--)
	delete m_CArray.GetAt(Index1);
	
	m_CArray.RemoveAll();


	CDocument::DeleteContents();
}














//DEL void CDirection2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
//DEL {
//DEL 	// TODO: Add your specialized code here and/or call the base class
//DEL 	if(pHint !=0)
//DEL 	{
//DEL 
//DEL 
//DEL 		CRect  InvalidRect = ((CLine *)pHint)-> GetDimRect();
//DEL 
//DEL 
//DEL 
//DEL 		CClientDC ClientDC(this);
//DEL 		OnPrepareDC(&ClientDC);
//DEL 		ClientDC.LPtoDP(&InvalidRect);
//DEL 		InvalidateRect(&InvalidRect);
//DEL }
//DEL 	else
//DEL 		CScrollView::OnUpdate(pSender,lHint,pHint);
//DEL 	
//DEL }


//DEL void CDirection2Doc::OnUpdateEditUndo(CCmdUI* pCmdUI) 
//DEL {
//DEL 	// TODO: Add your command update UI handler code here
//DEL int panduan;
//DEL 
//DEL if(pic[2]==-1 && pic[1] == -1 && pic[0] == -1)
//DEL {panduan=1;}
//DEL else
//DEL panduan = 0;
//DEL pCmdUI->Enable(panduan);
//DEL 	
//DEL }

//DEL void CDirection2Doc::OnEditClearall() 
//DEL {
//DEL 	// TODO: Add your command handler code here
//DEL DeleteContents();
//DEL   pic[2] = -1;
//DEL   pic[1] = -1; 
//DEL   pic[0] =- 1 ; 
//DEL   UpdateAllViews(NULL);
//DEL   SetModifiedFlag();	
//DEL }

void CDirection2Doc::OnEditUndo() 
{
	// TODO: Add your command handler code here
CPoint point;
CLine *line;
CRectFill *rectfill1;
CRectFill *rectfill2;
int Index = m_CArray.GetUpperBound();
if(Index >-1)
{
if(m_CArray.GetAt(Index)->Gettype() == 1) //为直线时
{
  line=(CLine *)m_CArray.GetAt(Index);
    point = line->Getindex(); //得到与直线相关的两矩形索引
rectfill1=(CRectFill *)GetDraw(point.x);
rectfill1->DeleteLine(Index);
rectfill2=(CRectFill *)GetDraw(point.y);
rectfill2->DeleteLine(Index);
}

	delete m_CArray.GetAt(Index);
	m_CArray.RemoveAt(Index);
}
UpdateAllViews(NULL);
SetModifiedFlag();	
}

void CDirection2Doc::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_CArray.GetSize());	
}

void CDirection2Doc::OnEditClearall() 
{
	// TODO: Add your command handler code here
DeleteContents();
UpdateAllViews(NULL);
SetModifiedFlag();	
}

void CDirection2Doc::OnUpdateEditClearall(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_CArray.GetSize());		
}

void CLine::SetZhi(long zhi)
{
m_zhi=zhi;
}

long CLine::GetZhi()
{
return m_zhi;
}

void CRectFill::SetZhi(long zhi)
{
m_zhi=zhi;
}

long CRectFill::GetZhi()
{
return m_zhi;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -