ex07aview.cpp

来自「MFC实现的学生成绩管理系统」· C++ 代码 · 共 292 行

CPP
292
字号
// Ex07aView.cpp : implementation of the CEx07aView class
//

#include "stdafx.h"
#include "Ex07a.h"

#include "Ex07aDoc.h"
#include "Ex07aView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx07aView

IMPLEMENT_DYNCREATE(CEx07aView, CFormView)

BEGIN_MESSAGE_MAP(CEx07aView, CFormView)
	//{{AFX_MSG_MAP(CEx07aView)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_SUM, OnSum)
	ON_BN_CLICKED(IDC_AVERAGE, OnAverage)
	ON_BN_CLICKED(IDC_FIRST, OnFirst)
	ON_BN_CLICKED(IDC_LAST, OnLast)
	ON_BN_CLICKED(IDC_PREV, OnPrev)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx07aView construction/destruction

CEx07aView::CEx07aView()
	: CFormView(CEx07aView::IDD)
{
	//{{AFX_DATA_INIT(CEx07aView)
	m_nCode = 0;
	m_sName = _T("");
	m_nAge = 0;
	m_lScore = 0.0;
	m_lScore2 = 0.0;
	m_lScore3 = 0.0;
	m_dAver = 0.0;
	m_dSum = 0.0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CEx07aView::~CEx07aView()
{
}

void CEx07aView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx07aView)
	DDX_Text(pDX, IDC_CODE, m_nCode);
	DDX_Text(pDX, IDC_NAME, m_sName);
	DDX_Text(pDX, IDC_AGE, m_nAge);
	DDX_Text(pDX, IDC_SCORE, m_lScore);
	DDX_Text(pDX, IDC_EDIT1, m_lScore2);
	DDX_Text(pDX, IDC_EDIT2, m_lScore3);
	DDX_Text(pDX, IDC_EDIT4, m_dAver);
	DDX_Text(pDX, IDC_EDIT3, m_dSum);
	//}}AFX_DATA_MAP
}

BOOL CEx07aView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CEx07aView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CEx07aView printing

BOOL CEx07aView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEx07aView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEx07aView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CEx07aView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CEx07aView diagnostics

#ifdef _DEBUG
void CEx07aView::AssertValid() const
{
	CFormView::AssertValid();
}

void CEx07aView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CEx07aDoc* CEx07aView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx07aDoc)));
	return (CEx07aDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEx07aView message handlers

void CEx07aView::OnAdd() 
{
    //在这里添加内容到对象中,并加入链表

	UpdateData();  //使得对话框编辑控件和相应变量之间传递数据

	CEx07aDoc* pDoc=GetDocument();  //取得指向文档的指针

	ASSERT_VALID(pDoc);  

	//首先用用户输入的变量构成一个新的CStudent对象,然后将其
	//加入到m_dataList表尾,最后将curPos指向表头
	CStudent *pStudent;
	pStudent=new CStudent(m_nCode,m_sName,m_nAge,m_lScore,m_lScore2,m_lScore3);
	pDoc->m_dataList.AddTail(pStudent);
	pDoc->curPos=pDoc->m_dataList.GetHeadPosition();

    m_nCode=0;
	m_sName="";
	m_nAge=0;
	m_lScore=0;
	m_lScore2=0;
	m_lScore3=0;

	//变量和对话框之间交换数据
	 UpdateData(false);

	 AfxMessageBox("您已经成功的添加了一个学生的信息!");
	
}

void CEx07aView::OnNext() 
{

	CEx07aDoc* pDoc=GetDocument();  //取得指向文档的指针

	ASSERT_VALID(pDoc);  

    if(pDoc->curPos!=NULL)
	{
	   //取出当前位置的CStudent对象,并将其成员赋给相应变量
       CStudent *pStudent= pDoc->m_dataList.GetAt(pDoc->curPos);
	   m_nCode=pStudent->m_nCode;
	   m_sName=pStudent->m_sName;
	   m_nAge=pStudent->m_nAge ;
	   m_lScore=pStudent->m_lScore;
	   m_lScore2=pStudent->m_lScore2;
	   m_lScore3=pStudent->m_lScore3;

	   //变量和对话框之间交换数据
	   UpdateData(false);

	   //将curPos移动到下一个位置。如果到了表尾,则重新从表头开始
	   pDoc->m_dataList.GetNext(pDoc->curPos);
	   if(pDoc->curPos==NULL)
		   pDoc->curPos=pDoc->m_dataList.GetHeadPosition();
	}
	else
		MessageBox("在记录里面没有数据!");	
}

void CEx07aView::OnSum() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_dSum = m_lScore + m_lScore2 + m_lScore3;
	UpdateData(FALSE);
}

void CEx07aView::OnAverage() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_dAver = (m_lScore + m_lScore2 + m_lScore3)/3;
	UpdateData(FALSE);
	
}

void CEx07aView::OnFirst() 
{
	// TODO: Add your control notification handler code here
    CEx07aDoc* pDoc=GetDocument(); 
	ASSERT_VALID(pDoc);
    if(pDoc->curPos!=NULL)
	{
	   //取出当前位置的CStudent对象,并将其成员赋给相应变量
	   pDoc->curPos=pDoc->m_dataList.GetHeadPosition();
       CStudent *pStudent= pDoc->m_dataList.GetAt(pDoc->curPos);
	   m_nCode=pStudent->m_nCode;
	   m_sName=pStudent->m_sName;
	   m_nAge=pStudent->m_nAge ;
	   m_lScore=pStudent->m_lScore;
	   m_lScore2=pStudent->m_lScore2;
	   m_lScore3=pStudent->m_lScore3;

	   //变量和对话框之间交换数据
	   UpdateData(false);
	}
	else
		MessageBox("在记录里面没有数据!");	
}
	


void CEx07aView::OnLast() 
{
	// TODO: Add your control notification handler code here
    CEx07aDoc *pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->curPos!=NULL)
	{
		pDoc->curPos=pDoc->m_dataList.GetTailPosition();
		CStudent *pStudent=pDoc->m_dataList.GetAt(pDoc->curPos);
	   m_nCode=pStudent->m_nCode;
	   m_sName=pStudent->m_sName;
	   m_nAge=pStudent->m_nAge ;
	   m_lScore=pStudent->m_lScore;
	   m_lScore2=pStudent->m_lScore2;
	   m_lScore3=pStudent->m_lScore3;

	   //变量和对话框之间交换数据
	   UpdateData(false);
	}
	
}

void CEx07aView::OnPrev() 
{
	// TODO: Add your control notification handler code here
	CEx07aDoc *pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->curPos!=NULL)
	{
	   CStudent *pStudent= pDoc->m_dataList.GetAt(pDoc->curPos);
	   m_nCode=pStudent->m_nCode;
	   m_sName=pStudent->m_sName;
	   m_nAge=pStudent->m_nAge ;
	   m_lScore=pStudent->m_lScore;
	   m_lScore2=pStudent->m_lScore2;
	   m_lScore3=pStudent->m_lScore3;

	   //变量和对话框之间交换数据
	   UpdateData(false);

	   //将curPos移动到下一个位置。如果到了表尾,则重新从表头开始
	   pDoc->m_dataList.GetPrev(pDoc->curPos);
	   if(pDoc->curPos==NULL)
		   pDoc->curPos=pDoc->m_dataList.GetTailPosition();
	}
	else
		MessageBox("在记录里面没有数据!");	
	
}

⌨️ 快捷键说明

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