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

📄 studentdlg.cpp

📁 学生成绩管理系统 使用C++语言加access数据库的一个小型软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// StudentDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Student.h"
#include "StudentDlg.h"
#include "DlgLoggin.h"
#include "UserManagement.h"
#include<time.h>

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


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog

CStudentDlg::CStudentDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStudentDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStudentDlg)
	m_ID = _T("");
	m_Name = _T("");
	m_Exercise = 0.0f;
	m_Report = 0.0f;
	m_Midterm = 0.0f;
	m_Terminal = 0.0f;
	m_Mark = 0.0f;
	m_Average = 0.0f;
	m_StdDev = 0.0f;
	m_CountA = 0;
	m_CountE = 0;
	m_Time = _T("");
	m_Userid = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStudentDlg)
	DDX_Control(pDX, IDC_BUTTON_LOCATE, m_Locate);
	DDX_Control(pDX, IDC_BUTTON_DELETE, m_Delete);
	DDX_Control(pDX, IDC_BUTTON_UPDATE, m_Update);
	DDX_Control(pDX, IDC_BUTTON_INSERT, m_Insert);
	DDX_Control(pDX, IDC_BUTTON_SORT, m_Sort);
	DDX_Control(pDX, IDC_COMBO_CLASS, m_Class);
	DDX_Control(pDX, IDC_COMBO_COURSE, m_Course);
	DDX_Control(pDX, IDC_COMBO_PERIOD, m_Period);
	DDX_Text(pDX, IDC_EDIT_ID, m_ID);
	DDX_Text(pDX, IDC_EDIT_NAME, m_Name);
	DDX_Control(pDX, IDC_MSFLEXGRID, m_Grid);
	DDX_Text(pDX, IDC_EDIT_EXERCISE, m_Exercise);
	DDX_Text(pDX, IDC_EDIT_REPORT, m_Report);
	DDX_Text(pDX, IDC_EDIT_MIDTERM, m_Midterm);
	DDX_Text(pDX, IDC_EDIT_TERMINAL, m_Terminal);
	DDX_Text(pDX, IDC_EDIT_MARK, m_Mark);
	DDX_Text(pDX, IDC_EDIT_AVERAGE, m_Average);
	DDX_Text(pDX, IDC_EDIT_STDDEV, m_StdDev);
	DDX_Text(pDX, IDC_EDIT_COUNTA, m_CountA);
	DDX_Text(pDX, IDC_EDIT_COUNTE, m_CountE);
	DDX_Text(pDX, IDC_STATIC_TIME, m_Time);
	DDX_Text(pDX, IDC_STATIC_USERID, m_Userid);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
	//{{AFX_MSG_MAP(CStudentDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_CLASS, OnSelchangeComboClass)
	ON_CBN_SELCHANGE(IDC_COMBO_PERIOD, OnSelchangeComboPeriod)
	ON_BN_CLICKED(ID_BUTTON_EXIT, OnButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_SORT, OnButtonSort)
	ON_CBN_SELCHANGE(IDC_COMBO_COURSE, OnSelchangeComboCourse)
	ON_BN_CLICKED(IDC_BUTTON_CLAC, OnButtonClac)
	ON_BN_CLICKED(IDC_BUTTON_INSERT, OnButtonInsert)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_EDIT_EXERCISE, OnChangeEditExercise)
	ON_EN_CHANGE(IDC_EDIT_MIDTERM, OnChangeEditMidterm)
	ON_EN_CHANGE(IDC_EDIT_REPORT, OnChangeEditReport)
	ON_EN_CHANGE(IDC_EDIT_TERMINAL, OnChangeEditTerminal)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
	ON_BN_CLICKED(IDC_BUTTON_USERMAN, OnButtonUserman)
	ON_BN_CLICKED(IDC_BUTTON_LOCATE, OnButtonLocate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers

BOOL CStudentDlg::OnInitDialog()
{
	CDlgLoggin DlgLog;
	DlgLog.DoModal();

	CDialog::OnInitDialog();

	
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Grid.SetCols(8);							// 设置表格列数
	m_Grid.SetRows(50);							// 设置表格行数
	// 设置各列宽度
	m_Grid.SetColWidth(0, 600);
	m_Grid.SetColWidth(1, 1340);
	m_Grid.SetColWidth(2, 1340);
	m_Grid.SetColWidth(3, 1320);
	m_Grid.SetColWidth(4, 1320);
	m_Grid.SetColWidth(5, 1320);
	m_Grid.SetColWidth(6, 1320);
	m_Grid.SetColWidth(7, 1320);
	//设置标题行各列的对齐方式(居中)
	for (int j = 0; j <= 7; j ++)
	{
		m_Grid.SetFixedAlignment(j, 3);
	}
	// 设置标题行标题文本
	m_Grid.SetRow(0);							// 定位在标题行(行号为0)
	m_Grid.SetCol(0);							// 定位到第一列(列号为0)
	m_Grid.SetText("序号");						// 设置标题行第一列的文本
	m_Grid.SetCol(1);							// 定位到第二列(列号为1)
	m_Grid.SetText("学号");						// 设置标题行第二列的文本
	m_Grid.SetCol(2);							// 定位到第二列(列号为1)
	m_Grid.SetText("姓名");						// 设置标题行第二列的文本
	m_Grid.SetCol(3);							// 定位到第四列(列号为3)
	m_Grid.SetText("平时");						// 设置标题行第四列的文本
	m_Grid.SetCol(4);							// 定位到第五列(列号为4)
	m_Grid.SetText("实验");						// 设置标题行第五列的文本
	m_Grid.SetCol(5);							// 定位到第六列(列号为5)
	m_Grid.SetText("期中");						// 设置标题行第六列的文本
	m_Grid.SetCol(6);							// 定位到第七列(列号为6)
	m_Grid.SetText("期末");						// 设置标题行第七列的文本
	m_Grid.SetCol(7);							// 定位到第八列(列号为7)
	m_Grid.SetText("总评");						// 设置标题行第八列的文本
	
	SetTimer(1,100,NULL);
	CString strSQL, str;
	strSQL = "SELECT 班级名称 FROM 班级;";		// 创建查询班级的SQL语句
	_variant_t nTemp;
	m_pRecordset_Std.CreateInstance(__uuidof(ADODB::Recordset)); 

// 连接数据源
	HRESULT hr; 				
	try 
	{ 
		hr = m_pConnection_Std.CreateInstance("ADODB.Connection");//创建Connection对象 
		if(SUCCEEDED(hr)) 
		{ 
			hr = m_pConnection_Std->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb","","",ADODB::adModeUnknown);///连接数据库
		}
	}
	catch(_com_error e)///捕捉异常 
	{ 
		CString errormessage; 
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); 
		AfxMessageBox(errormessage);///显示错误信息 
	}
	//打开数据库
	try
	{

		m_pRecordset_Std->Open(_bstr_t(strSQL), m_pConnection_Std.GetInterfacePtr(),ADODB::adOpenDynamic,ADODB::adLockOptimistic,ADODB::adCmdText);
	}
	catch (_com_error e)
	{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。错误信息:%s",e.ErrorMessage());
	}
	// 扫描记录集,取出班级数据载入列表框
	m_pRecordset_Std->MoveFirst();
	while (!m_pRecordset_Std->adoEOF)
	{
		nTemp=m_pRecordset_Std->GetCollect("班级名称");
		str=(LPCSTR)_bstr_t(nTemp);
		m_Class.AddString(str);
		m_pRecordset_Std->MoveNext();
	}
	m_pRecordset_Std->Close();						// 关闭记录集
	strSQL = "SELECT 课程名称 FROM 课程;";		// 创建查询课程的SQL语句
	try
	{

		m_pRecordset_Std->Open(_bstr_t(strSQL), m_pConnection_Std.GetInterfacePtr(),ADODB::adOpenDynamic,ADODB::adLockOptimistic,ADODB::adCmdText);
	}
	catch (_com_error e)
	{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。错误信息:%s",e.ErrorMessage());
	}
	// 扫描记录集,取出课程数据载入列表框
	m_pRecordset_Std->MoveFirst();
	while (!m_pRecordset_Std->adoEOF)
	{
		nTemp=m_pRecordset_Std->GetCollect("课程名称");
		str=(LPCSTR)_bstr_t(nTemp);
		m_Course.AddString(str);
		m_pRecordset_Std->MoveNext();
	}
	m_pRecordset_Std->Close();						// 关闭记录集
	UpdateData(TRUE); 
	str=DlgLog.username;
	m_Userid=DlgLog.userid;
	GetDlgItem(IDC_STATIC_NAME)->SetWindowText(str);

	m_Insert.EnableWindow(FALSE);
	m_Update.EnableWindow(FALSE);
	m_Sort.EnableWindow(FALSE);					// 使“排序”按钮变灰
	m_Period.EnableWindow(FALSE);				// 使“阶段”组合框变灰
	m_Locate.EnableWindow(false);
	m_Delete.EnableWindow(false);
	CEdit* pEdit[6];

	pEdit[0]=(CEdit*)GetDlgItem(IDC_EDIT_ID);
	pEdit[1]=(CEdit*)GetDlgItem(IDC_EDIT_NAME);

	pEdit[3]=(CEdit*)GetDlgItem(IDC_EDIT_MIDTERM);
	pEdit[4]=(CEdit*)GetDlgItem(IDC_EDIT_TERMINAL); 
	pEdit[5]=(CEdit*)GetDlgItem(IDC_EDIT_REPORT);
	pEdit[6]=(CEdit*)GetDlgItem(IDC_EDIT_EXERCISE);
    pEdit[0]->SetReadOnly();
	pEdit[1]->SetReadOnly();

	pEdit[3]->SetReadOnly();
	pEdit[4]->SetReadOnly();
	pEdit[5]->SetReadOnly();
	pEdit[6]->SetReadOnly();

	UpdateData(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CStudentDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CStudentDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CStudentDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CStudentDlg::OnSelchangeComboClass() 
{
	// TODO: Add your control notification handler code here
	_variant_t nTemp;
	CString strSQL;	                                    // 声明MFC之字符串类的对象,存放用于数据库查询的SQL语句
	CString id, name;
	float exercise, report, midterm, terminal;
	CString str;
	int i, n;
	CEdit* pEdit[6];
	pEdit[0]=(CEdit*)GetDlgItem(IDC_EDIT_ID);
	pEdit[1]=(CEdit*)GetDlgItem(IDC_EDIT_NAME);

	pEdit[3]=(CEdit*)GetDlgItem(IDC_EDIT_MIDTERM);
	pEdit[4]=(CEdit*)GetDlgItem(IDC_EDIT_TERMINAL); 
	pEdit[5]=(CEdit*)GetDlgItem(IDC_EDIT_REPORT);
	pEdit[6]=(CEdit*)GetDlgItem(IDC_EDIT_EXERCISE);
	if (m_Course.GetCurSel() < 0)
	{	
		
    pEdit[0]->SetReadOnly();
	pEdit[1]->SetReadOnly();

	pEdit[3]->SetReadOnly();
	pEdit[4]->SetReadOnly();
	pEdit[5]->SetReadOnly();
	pEdit[6]->SetReadOnly();
	return;
	}
	else
	{
    pEdit[0]->SetReadOnly(false);
	pEdit[1]->SetReadOnly(false);

	pEdit[3]->SetReadOnly(false);
	pEdit[4]->SetReadOnly(false);
	pEdit[5]->SetReadOnly(false);
	pEdit[6]->SetReadOnly(false);

	}
	
	Student2* pStudent = new Student2[64];				// 声明并创建64个元素的学生对象数组
	CString ClassName, CourseName;
	m_Class.GetLBText(m_Class.GetCurSel(), ClassName);	// 得到所选班级名称文本
	m_Course.GetLBText(m_Course.GetCurSel(),CourseName);// 得到所选课程名称文本

	// 创建查询所需数据的SQL语句
	strSQL = "SELECT 学生.学号, 学生.姓名, 成绩.平时作业, 成绩.实验报告, 成绩.期中考试, 成绩.期末考试 ";
	strSQL += "FROM (班级 INNER JOIN 学生 ON 班级.班级ID = 学生.班级ID) INNER JOIN (课程 INNER JOIN 成绩 ON 课程.课程ID = 成绩.课程ID) ON 学生.学生ID = 成绩.学生ID ";
	strSQL += "WHERE 班级.班级名称='";
	strSQL += ClassName;
	strSQL += "' AND 课程.课程名称='";
	strSQL += CourseName;
	strSQL += "'";
	try
	{

		m_pRecordset_Std->Open(_bstr_t(strSQL), m_pConnection_Std.GetInterfacePtr(),ADODB::adOpenDynamic,ADODB::adLockOptimistic,ADODB::adCmdText);
	}
	catch (_com_error e)
	{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。错误信息:%s",e.ErrorMessage());
	}
	m_pRecordset_Std->MoveFirst();
	// 对记录集进行扫描以确定记录数
	i = 0;
	while (!m_pRecordset_Std->adoEOF)
	{
		m_pRecordset_Std->MoveNext();

⌨️ 快捷键说明

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