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

📄 ex_studentview.cpp

📁 郑阿奇主编< Visual C++教程> 配套的实验书的全部代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Ex_StudentView.cpp : implementation of the CEx_StudentView class
//

#include "stdafx.h"
#include "Ex_Student.h"

#include "Ex_StudentDoc.h"
#include "Ex_StudentView.h"

#include "StudentSet.h"
#include "ScoreSet.h"
#include "CourseSet.h"

#include "StuInfoDlg.h"
#include "ScoreDlg.h"
#include "CourseDlg.h"

#include "SeekDlg.h"
#include "StatDlg.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView

IMPLEMENT_DYNCREATE(CEx_StudentView, CListView)

BEGIN_MESSAGE_MAP(CEx_StudentView, CListView)
	//{{AFX_MSG_MAP(CEx_StudentView)
	ON_COMMAND(ID_OP_CHANGE, OnOpChange)
	ON_COMMAND(ID_OP_DEL, OnOpDel)
	ON_COMMAND(ID_SEL_SEEK, OnSelSeek)
	ON_COMMAND(ID_SEL_STAT, OnSelStat)
	ON_UPDATE_COMMAND_UI(ID_SEL_STAT, OnUpdateSelStat)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView construction/destruction

CEx_StudentView::CEx_StudentView()
{
	// TODO: add construction code here
	memset(&m_pageInfo, 0, sizeof(m_pageInfo));		// 所有成员置为0

	double fontScale = 254.0/72.0;	// 一个点相当于多少0.1mm
	// 页眉字体
	m_pageInfo.lfHead.lfHeight = - (int)(9 * fontScale + 0.5);		// 9号字
	m_pageInfo.lfHead.lfWeight = FW_BOLD;
	m_pageInfo.lfHead.lfCharSet = GB2312_CHARSET;
	strcpy((LPSTR)&(m_pageInfo.lfHead.lfFaceName), "黑体");

	// 页脚字体
	m_pageInfo.lfFoot.lfHeight = - (int)(9 * fontScale + 0.5);		// 9号字
	m_pageInfo.lfFoot.lfWeight = FW_NORMAL;
	m_pageInfo.lfFoot.lfCharSet = GB2312_CHARSET;
	strcpy((LPSTR)&(m_pageInfo.lfFoot.lfFaceName), "楷体_GB2312");

	// 正文字体
	m_pageInfo.lfText.lfHeight = - (int)(9 * fontScale + 0.5);		// 9号字
	m_pageInfo.lfText.lfWeight = FW_NORMAL;
	m_pageInfo.lfText.lfCharSet = GB2312_CHARSET;
	strcpy((LPSTR)&(m_pageInfo.lfText.lfFaceName), "宋体");
}

CEx_StudentView::~CEx_StudentView()
{
}

BOOL CEx_StudentView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |= LVS_REPORT;

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView drawing

void CEx_StudentView::OnDraw(CDC* pDC)
{
	CEx_StudentDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CEx_StudentView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView printing

BOOL CEx_StudentView::OnPreparePrinting(CPrintInfo* pInfo)
{
	ReadListViewData();
	int nSize =m_strContents.GetSize();
	if (nSize<1) pInfo->SetMaxPage(1);
	return DoPreparePrinting(pInfo);
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView diagnostics

#ifdef _DEBUG
void CEx_StudentView::AssertValid() const
{
	CListView::AssertValid();
}

void CEx_StudentView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx_StudentView message handlers

void CEx_StudentView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	CEx_StudentDoc* pDoc = GetDocument();
	CString strHint, str;
	if (pHint)
	{
		strHint = *((CString *)pHint);
		delete (CString *)pHint;
	}
	if (pDoc->m_nInfoType == 1) { // 学生信息
		switch ((int)lHint)
		{
			case 1:	
			case 2:
			case 3:
					str.Format("studentno LIKE '%s%%'", strHint.Left(6));
					DispStudentInfo( str, "studentno");
					break;
			case 11:	
					str.Format("studentno LIKE '%s%%'", strHint.Left(6));
					DispStudentInfo( str, "studentno");
					break;
			case 12:
					str.Format("special = '%s'", strHint);
					DispStudentInfo( str, "studentno");
					break;
			case 13:
					str = "";
					DispStudentInfo( str, "studentno");
					break;
		}
	} else
	if (pDoc->m_nInfoType == 2) {	// 课程信息
		switch ((int)lHint)
		{
			case 1:	
			case 2:
			case 3:
					str.Format("courseno = '%s'", strHint);
					DispCourseInfo( str, "");
					break;
			case 11:	
					str.Format("special = '%s'", strHint);
					DispCourseInfo( str, "courseno");
					break;
			case 12:
					str.Format("special = '%s'", strHint);
					DispCourseInfo( str, "courseno");
					break;
			case 13:
					str = "";
					DispCourseInfo( str, "courseno");
					break;
		}
	} else
	if (pDoc->m_nInfoType == 3) {	// 学生成绩
		switch ((int)lHint)
		{
			case 1:	
			case 2:
			case 3:
					str.Format("studentno = '%s'", strHint);
					DispScoreAndCourseInfo( str);
					break;
			case 11:	
					str.Format("studentno LIKE '%s%%'", strHint.Left(6));
					DispScoreAndCourseInfo( str);
					break;
			case 12:
					str.Format("special = '%s'", strHint);
					DispScoreAndCourseInfo2( str);
					break;
			case 13:
					str = "";
					DispScoreAndCourseInfo( str);
					break;
		}
	}
	DispStatusRecord();
}

void CEx_StudentView::DispStudentInfo(CString strFilter, CString strSort)
{
	DeleteAllColumn();	// 删除表头
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strHeader[]={"学号", "姓名","性别","出生日期",
		"专业"};
	int nColWidth[] = {100,100,50,100,200};
	for (int nCol=0; nCol<sizeof(strHeader)/sizeof(CString); nCol++)
		m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,nColWidth[nCol]);

	m_ListCtrl.DeleteAllItems();		// 删除所有的列表项

	CStudentSet sSet;
	sSet.m_strFilter = strFilter;
	sSet.m_strSort = strSort;
	sSet.Open();
	int nItem = 0;
	CString str;
	while (!sSet.IsEOF())
	{
		m_ListCtrl.InsertItem( nItem, sSet.m_studentno);		// 插入学号
		m_ListCtrl.SetItemText( nItem, 1, sSet.m_studentname);	// 设置姓名
		// 通过Get获取相关字段记录内容
		if (sSet.m_xb)
			m_ListCtrl.SetItemText( nItem, 2, "男");	// 设置性别
		else
			m_ListCtrl.SetItemText( nItem, 2, "女");	// 设置性别


		for (UINT i=3; i<sSet.m_nFields; i++)
		{
			sSet.GetFieldValue(i, str);			// 获取指定字段值
			m_ListCtrl.SetItemText( nItem, i, str);	
		}
		sSet.MoveNext();
		nItem++;
	}
	if (sSet.IsOpen()) sSet.Close();
}

void CEx_StudentView::DeleteAllColumn()
{
	CListCtrl& m_ListCtrl = GetListCtrl();
	int nCount = 0;
	CHeaderCtrl* pHeaderCtrl = m_ListCtrl.GetHeaderCtrl();
	if ( pHeaderCtrl!= NULL)
		nCount = pHeaderCtrl->GetItemCount();

	for (int i=0;i < nCount;i++)
		m_ListCtrl.DeleteColumn(0);
}


void CEx_StudentView::DispCourseInfo(CString strFilter, CString strSort)
{
	DeleteAllColumn();	// 删除表头
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strHeader[]={"课程号","所属专业", "课程名称","课程类别","开课学期","课时数","学分"};
	int nLong[] = {80, 180, 180, 80, 80, 80, 80};
	for (int nCol=0; nCol<sizeof(strHeader)/sizeof(CString); nCol++)
		m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,nLong[nCol]);
	m_ListCtrl.DeleteAllItems();		// 删除所有的列表项

	CCourseSet sSet;
	sSet.m_strFilter = strFilter;
	sSet.m_strSort = strSort;
	sSet.Open();
	int nItem = 0;
	CString str;
	while (!sSet.IsEOF())
	{
		m_ListCtrl.InsertItem( nItem, sSet.m_courseno);		// 插入课程号
		// 通过Get获取相关字段记录内容
		for (UINT i=1; i<sSet.m_nFields; i++)
		{
			sSet.GetFieldValue(i, str);			// 获取指定字段值
			m_ListCtrl.SetItemText( nItem, i, str);	
		}
		sSet.MoveNext();
		nItem++;
	}
	if (sSet.IsOpen()) sSet.Close();
}

void CEx_StudentView::DispScoreAndCourseInfo(CString strFilter, CString strFilter2)
{
	DeleteAllColumn();	// 删除表头
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strHeader[]={"学号", "课程号","课程所属专业", "课程名称","课程类别","开课学期","课时数","学分","成绩"};
	int nLong[] = {80, 80, 180, 180, 80, 80, 80, 80, 80};
	for (int nCol=0; nCol<sizeof(strHeader)/sizeof(CString); nCol++)
		m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,nLong[nCol]);
	m_ListCtrl.DeleteAllItems();		// 删除所有的列表项

	CScoreSet sSet;
	sSet.m_strFilter = strFilter;
	sSet.Open();
	int nItem = 0;
	CString str;
	while (!sSet.IsEOF())
	{
		CCourseSet cSet;
		cSet.m_strFilter.Format("courseno='%s' %s", sSet.m_course, strFilter2);
		cSet.Open();
		UINT i;
		// 通过Get获取相关字段记录内容
		if (!cSet.IsEOF()) {
			m_ListCtrl.InsertItem( nItem, sSet.m_studentno);		// 插入学号
			m_ListCtrl.SetItemText( nItem, 1, sSet.m_course);
			for (i=1; i<cSet.m_nFields; i++)
			{
				cSet.GetFieldValue(i, str);			// 获取指定字段值
				m_ListCtrl.SetItemText( nItem, i+1, str);	
			}
			str.Format("%0.1f", sSet.m_score);
			m_ListCtrl.SetItemText( nItem, i+1, str);	
			nItem++;
		}
		cSet.Close();
		sSet.MoveNext();
	}
	if (sSet.IsOpen()) sSet.Close();
}

void CEx_StudentView::DispScoreAndCourseInfo2(CString strSpecFilter, CString strFilter2)
{
	DeleteAllColumn();	// 删除表头
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strHeader[]={"学号", "课程号", "课程所属专业", "课程名称","课程类别","开课学期","课时数","学分","成绩"};
	int nLong[] = {80, 80, 180, 180, 80, 80, 80, 80, 80};
	for (int nCol=0; nCol<sizeof(strHeader)/sizeof(CString); nCol++)
		m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,nLong[nCol]);
	m_ListCtrl.DeleteAllItems();		// 删除所有的列表项

	CStudentSet sSet;
	sSet.m_strFilter = strSpecFilter;
	sSet.m_strSort = "studentno";
	sSet.Open();
	int nItem = 0;
	CString str;
	while (!sSet.IsEOF())
	{
		CScoreSet oSet;
		oSet.m_strFilter.Format("studentno='%s'", sSet.m_studentno);
		oSet.Open();
		while (!oSet.IsEOF())
		{

			CCourseSet cSet;
			cSet.m_strFilter.Format("courseno='%s' %s", oSet.m_course, strFilter2);
			cSet.Open();
			UINT i;
			// 通过Get获取相关字段记录内容
			if (!cSet.IsEOF()) {
				m_ListCtrl.InsertItem( nItem, oSet.m_studentno);		// 插入学号
				for (i=0; i<cSet.m_nFields; i++)
				{
					cSet.GetFieldValue(i, str);			// 获取指定字段值
					m_ListCtrl.SetItemText( nItem, i+1, str);	
				}
				str.Format("%0.1f", oSet.m_score);
				m_ListCtrl.SetItemText( nItem, i+1, str);	
				nItem++;
			}
			cSet.Close();
			oSet.MoveNext();
		}

		oSet.Close();
		sSet.MoveNext();
	}
	if (sSet.IsOpen()) sSet.Close();
}

void CEx_StudentView::OnOpChange() 
{
	// TODO: Add your command handler code here
	// 判断是否有选择的列表项
	CListCtrl& m_ListCtrl = GetListCtrl();
	POSITION pos;
	pos = m_ListCtrl.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		MessageBox("你还没有选中列表项!");
		return;
	}
	int nItem = m_ListCtrl.GetNextSelectedItem( pos );

	CEx_StudentDoc* pDoc = GetDocument();

	if (pDoc->m_nInfoType == 1)
		ChangeStudentInfo(nItem);
	else if (pDoc->m_nInfoType == 2)
		ChangeCourseInfo(nItem);
	else if (pDoc->m_nInfoType == 3)
		ChangeScoreInfo(nItem);
	
}

void CEx_StudentView::OnOpDel() 
{
	// TODO: Add your command handler code here
	CListCtrl& m_ListCtrl = GetListCtrl();
	POSITION pos;
	pos = m_ListCtrl.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		MessageBox("你还没有选中列表项!");
		return;
	}
	int nItem = m_ListCtrl.GetNextSelectedItem( pos );
	CString strItem;
	strItem = m_ListCtrl.GetItemText( nItem, 0 );
	CString str;
	str.Format("你确实要删除 %s 列表项(记录)吗?", strItem );
	if ( IDOK != MessageBox(str, "删除确认", MB_ICONQUESTION | MB_OKCANCEL ))
		return;


	CEx_StudentDoc* pDoc = GetDocument();

	if (pDoc->m_nInfoType == 1)
		DelStudentInfo(nItem);
	else if (pDoc->m_nInfoType == 2)
		DelCourseInfo(nItem);
	else if (pDoc->m_nInfoType == 3)
		DelScoreInfo(nItem);
	DispStatusRecord();
}

void CEx_StudentView::ChangeStudentInfo(int nItem)
{
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strStuNO = m_ListCtrl.GetItemText( nItem, 0 );
	strStuNO.TrimLeft();
	CStudentSet sSet;
	sSet.m_strFilter.Format("studentno = '%s'", strStuNO);
	sSet.Open();

	CStuInfoDlg dlg;
	dlg.m_strPhotoFilePath.Format("c://%s.bmp", strStuNO);
	dlg.m_strNO		= strStuNO;
	dlg.m_strName	= sSet.m_studentname;
	dlg.m_bSex		= sSet.m_xb;
	dlg.m_tBirth	= sSet.m_birthday;
	dlg.m_strSpecial= sSet.m_special;
	dlg.m_strOKText	= "修改";
	if (IDOK != dlg.DoModal())
	{
		if (sSet.IsOpen()) sSet.Close();
		return;
	}
	sSet.Edit();

	sSet.m_studentname	= dlg.m_strName;
//	sSet.m_studentno	= dlg.m_strNO;		// 禁止修改学号
	sSet.m_xb			= dlg.m_bSex;
	sSet.m_birthday		= dlg.m_tBirth;
	sSet.m_special		= dlg.m_strSpecial;

	sSet.Update();
	sSet.Requery();

	if (sSet.IsOpen()) sSet.Close();
	// 在这里复制照片文件
	if (dlg.m_bPhotoChange) {
		CString strFileTO;
		strFileTO.Format("c://%s.bmp", dlg.m_strNO);
		GetDocument()->DoCopyFile(strFileTO, dlg.m_strPhotoFilePath);
	}

	// 更新视图
	MessageBox("稍等几秒钟后,单击[确定]按钮!",
		"特别提示",MB_OK|MB_ICONINFORMATION);
	GetDocument()->UpdateAllViews( NULL, 2, (CObject *)new CString(dlg.m_strNO) );
}

void CEx_StudentView::ChangeCourseInfo(int nItem)

⌨️ 快捷键说明

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