编程语言
首页 > 编程语言> > 面向对象程序设计实验-使用MFC进行可视化编程

面向对象程序设计实验-使用MFC进行可视化编程

作者:互联网

一、实验目的及要求

  1. 了解在集成开发环境下程序的编辑、编译、连接、运行与调试;

二、实验设备与平台

  1. 实验设备:计算机;
  2. 平台:Windows 2000操作系统,Visual C++ 6.0或Microsoft Visual Studio 2005/2008/2010/2012/2013。

三、实验内容与步骤
1.用MFC的向导创建编辑框,在编辑框中实现算术“加”、“减”、“乘”及“除”的运算,界面如图13-1所示。
在这里插入图片描述
部分代码如下:

void CMFCApplication2Dlg::OnClickedButtonride()
{
	UpdateData(true);
	mC = mA * mB;
	UpdateData(false);
}
void CMFCApplication2Dlg::OnClickedButtonadd()
{
	UpdateData(true);
	mC = mA + mB;
	UpdateData(false);
}
void CMFCApplication2Dlg::OnClickedButtondivide()
{
	UpdateData(true);
	mC = mA / mB;
	UpdateData(false);
}
void CMFCApplication2Dlg::OnClickedButtonsub()
{
	UpdateData(true);
	mC = mA - mB;
	UpdateData(false);
}
void CMFCApplication2Dlg::OnClickedButtonExit()
{
	ExitProcess(0);
}

2.利用文档/视图结构创建一个绘图板,使其能绘画各种直线,具有新建空白窗口,重画窗口时可以恢复直线等功能。]

void CMFCApplication2View::void OnDraw(CDC* pDC)
{
int index;
index=pDoc->GetTotalLine();
while(index--)
pDoc->GetLine(index)->DrawLine(pDC);
}
void CMFCApplication2View::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_pOld = point;
	m_pOrigin = point;
	SetCapture();
	m_Draw = 1;
	RECT rect;
	GetClientRect(&rect);
	ClientToScreen(&rect);
	ClipCursor(&rect);
	CView::OnLButtonDown(nFlags, point);
}
void CMFCApplication2View::OnMouseMove(UINT nFlags, CPoint point)
{
	SetCursor(m_Hcursor);
	if (m_Draw)
	{
		CClientDC dc(this);
		dc.SetROP2(R2_NOT);
		dc.MoveTo(m_pOrigin);
		dc.LineTo(m_pOld);
		dc.MoveTo(m_pOrigin);
		dc.LineTo(point);
		m_pOld = point;
	}
	CView::OnMouseMove(nFlags, point);
}
void CMFCApplication2View::OnLButtonUp(UINT nFlags, CPoint point)
{
	if (m_Draw)
	{
		m_Draw = 0;
		ReleaseCapture();
		ClipCursor(NULL);
	}
	CView::OnLButtonUp(nFlags, point);
	CMFCApplication2Doc* pDoc = GetDocument();
	pDoc->AddLine(m_pOrigin.x, m_pOrigin.y, point.x, point.y);
}
MFCApplication2Doc.h
class CMyLine : public CObject
{
protected:
	int m_StartX;
	int m_StartY;
	int m_EndX;
	int m_EndY;
public:
	CMyLine(int StartX, int StartY, int EndX, int EndY);
	void DrawLine(CDC* pDC);
}
protected:
CObArray m_ObArray;
public:
void AddLine(int StartX, int StartY,int EndX,int EndY);
CMyLine *GetLine(int index);
int GetTotalLine();

class CMFCApplication2Doc : public CDocument
{
	virtual void DeleteContents();
protected:
	CObArray m_ObArray;
public:
	void AddLine(int StartX, int StartY, int EndX, int EndY);
	CMyLine* GetLine(int index);
	int GetTotalLine();
};

MFCApplication2Doc.cpp 
CMyLine::CMyLine(int StartX, int StartY,int EndX, int EndY)
{
  m_StartX=StartX;
  m_StartY=StartY;
  m_EndX=EndX;
  m_EndY=EndY;
};
void CMyLine::DrawLine(CDC*pDC)
{
 pDC->MoveTo(m_StartX,m_StartY);
 pDC->LineTo(m_EndX,m_EndY);
};
void CMyExamDoc::AddLine(int StartX, int StartY,int EndX,int EndY)
{
CMyLine *pMyLine = new CMyLine(StartX, StartY,EndX, EndY);
m_ObArray. Add(pMyLine);
};
CMyLine*CMyExamDoc::GetLine(int index)
{
if(index<0||index>m_ObArray.GetUpperBound())
return 0;
return(CMyLine*)m_ObArray.GetAt(index);
};
int CMyExamDoc::GetTotalLine()
{
return m_ObArray.GetSize();
};
void CMyExamDoc::DeleteContents()
{
int index;
index=m_ObArray.GetSize();
while(index--)
delete m_ObArray.GetAt(index);
m_ObArray.RemoveAll();
}

标签:index,MFC,point,int,void,EndY,面向对象,可视化,CMyLine
来源: https://blog.csdn.net/weixin_47178179/article/details/117488707