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

📄 studentmanage.cpp

📁 学生考勤统计软件
💻 CPP
字号:
// StudentManage.cpp : implementation file
//

#include "stdafx.h"
#include "StudentCheck.h"
#include "StudentManage.h"
#include "Afxtempl.h"


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

#define COLNUM 12 // 11是记录的属性数目


/////////////////////////////////////////////////////////////////////////////
// CStudentManage dialog


CStudentManage::CStudentManage(CWnd* pParent /*=NULL*/)
	: CDialog(CStudentManage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStudentManage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_DepInfo.LoadDep();
	m_CurAdo.InitialDB();
	m_AllStuInfos.GetAllDBInfo();
}

CStudentManage::~CStudentManage()
{
	m_CurAdo.ClosedDB();
}


void CStudentManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStudentManage)
	DDX_Control(pDX, IDC_MEMBER_LIST, m_StuInfoList);
	DDX_Control(pDX, IDC_CLASS_TREE, m_DepInfoTree);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStudentManage, CDialog)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CStudentManage)
	ON_NOTIFY(TVN_SELCHANGED, IDC_CLASS_TREE, OnTreeSelectChanged)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_MEMBER_LIST, OnItemchangedMemberList)
	ON_NOTIFY(LVN_KEYDOWN, IDC_MEMBER_LIST, OnKeydownMemberList)
	ON_NOTIFY(NM_DBLCLK, IDC_MEMBER_LIST, OnDblclkMemberList)
	ON_COMMAND(ID_EDIT_INSERT, OnEditInsert)
	ON_COMMAND(ID_EDIT_MODIFY, OnEditModify)
	ON_COMMAND(ID_EDIT_DEL, OnEditDel)
	ON_COMMAND(IDR_STU_MANAGE, OnStuManage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CStudentManage::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString strCurName;
	if(!(m_DepImageInfo.Create(IDB_BITMAP1,16,1,RGB(0,255,0))))
		AfxMessageBox("图像列表创建出错!");
	m_DepInfoTree.SetImageList(&m_DepImageInfo,TVSIL_NORMAL);
	HTREEITEM htRoot;
	CString strRoot = "环境学院";
	htRoot = m_DepInfoTree.InsertItem(strRoot);  //添加根节点
    m_DepInfoTree.SetItemData(htRoot,0);
	m_DepInfo.CreatTree(htRoot, m_DepInfoTree);
	m_DepInfoTree.Expand(htRoot, TVE_EXPAND);

	ConstructTableName();
	m_StuInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT| LVS_EX_GRIDLINES |LVS_EX_CHECKBOXES);
	return TRUE;
}




/////////////////////////////////////////////////////////////////////////////
// CStudentManage message handlers

void CStudentManage::OnTreeSelectChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	*pResult = 0;

	HTREEITEM hCurItem;
	HTREEITEM hCurChildItem;
	CString strCurClassID;
	hCurItem = m_DepInfoTree.GetSelectedItem();
	if (NULL != hCurItem)
	{
		hCurChildItem = m_DepInfoTree.GetChildItem(hCurItem);
		if (NULL == hCurChildItem) // 若某一个节点,其孩子为空,说明其为根节点,可以使用查询语句寻找具体信息
		{
			// 如果当前的所选择的叶子节点发生了变化,那么就修改当前查询语句 m_strCurQuaryInfo 
			// 并且发送消息,导致整个对话框发生刷新
			strCurClassID.Format("%d", m_DepInfoTree.GetItemData(hCurItem));
			ModityCurQueryMessage(strCurClassID);
		}
	}
}

void CStudentManage::ModityCurQueryMessage(CString _strCurClassID)
{
//传进来的是班级号 通过该号码对班级成员进行检索
	CString strStandInfo;
	CString strQuotationMark;
	strStandInfo.LoadString(IDS_STANDARD_STU_QUERY);
	strQuotationMark.LoadString(IDS_QUOTATION_MARK);
	m_strCurQuaryInfo = strStandInfo + strQuotationMark +  _strCurClassID + strQuotationMark;
//	AfxMessageBox(m_strCurQuaryInfo);
	RefreshData();
}

void CStudentManage::RefreshData()
{
	//刷新,这里用了一个很笨的方法:全部重新查询
	//比较好的办法是有针对性的查询
	//或者是有针对性的修改当前显示
	//时间关系,采用最简单的方式和方法进行
	
	UpdateData(TRUE);
	m_StuInfoList.DeleteAllItems();
	CArray <CString, LPCTSTR> StudentInfoAry;
	CString strCurColInfo;
	
	StudentInfoAry.SetSize(COLNUM);  
	CString * pCurInfo = StudentInfoAry.GetData();
	int nRecordNum = 0;

	// 如果记录集不为空
	_RecordsetPtr CurRecordset;
	_variant_t nIndex = (long)0; 
	_variant_t var;
	CString strCollege="环境学院"; //把03给替换掉
	CurRecordset = m_CurAdo.GetRecordSet(LPCTSTR(m_strCurQuaryInfo));
	while (0 == CurRecordset->adoEOF)
	{
		//取一条记录付值给数组StudentInfoAry
	
		for (int i = 0; i < COLNUM; i++)
		{
			nIndex = (long) i;
			var = CurRecordset->GetCollect(nIndex);
			pCurInfo[i] = (LPCTSTR)(_bstr_t )var;
 		}
		m_StuInfoList.InsertItem(nRecordNum, "");
		for (int j = 0; j < COLNUM; j++)
		{
//			strCurColInfo = StudentInfoAry[i];
			pCurInfo[j] = m_AllStuInfos.GetNum(pCurInfo[j]);
			if(7==j)
				pCurInfo[j]=strCollege;
			m_StuInfoList.SetItemText(nRecordNum, j, pCurInfo[j]);
		}
		nRecordNum ++;
		//记录转下一条
		CurRecordset->MoveNext();
	}

}


void CStudentManage::DelRecord()
{
	//删除记录主要分成三个部分:
	// 1 选择要删除的记录
	// 2 删除之
	// 3 对整体进行刷新

	int nCurItem = 0;
	int i = 0;
	int nColCounts = 0;

	LVCOLUMN colCurColumn;
	CString strCurColName;
	CString strStuID;
	CString strDelRecord;
	CString strCurDelCommand;
	CString strQuotationMark;
	CString strTableNameStuID;
	char str[256];
	
	strQuotationMark.LoadString(IDS_QUOTATION_MARK);
	strDelRecord.LoadString(IDS_ORDERS_DEL_STU);
	strTableNameStuID.LoadString(IDS_COLNAME_STUID);
	colCurColumn.mask = LVCF_TEXT;
	colCurColumn.pszText = str;
	colCurColumn.cchTextMax = 256;  

	if (m_nCurItem >= 0)
	{
		// 如果当前选到了记录
		// 获得记录的属性数目

		nColCounts = m_StuInfoList.GetHeaderCtrl()->GetItemCount();

		// MD 老子讨厌写个破11在这里
		for (i = 0; i < nColCounts; i++)
		{
			m_StuInfoList.GetColumn(i, &colCurColumn);
			strCurColName = colCurColumn.pszText;
			if (strCurColName == strTableNameStuID)
			{
				strStuID = m_StuInfoList.GetItemText(m_nCurItem, i);
				strCurDelCommand = strDelRecord + strQuotationMark + strStuID + strQuotationMark;
				RunSQLCommand(strCurDelCommand);
				RefreshData();
			}
		}
	}

}


void CStudentManage::InsertRecord()
{
	CString strCurSQL;
	CStudentInfoDlg CurStuInfoDlg;
	CurStuInfoDlg.SetStuInfoPointer(&m_AllStuInfos);
	if (TRUE == CurStuInfoDlg.DoModal())
	{
		strCurSQL = CurStuInfoDlg.GetModifySQL();
		RunSQLCommand(strCurSQL);
		RefreshData();
	}
}

void CStudentManage::RunSQLCommand(CString _strSQLCommand)
{
//	AfxMessageBox(_strSQLCommand);
	m_CurAdo.ExecuteSQL(LPCTSTR(_strSQLCommand));
}


void CStudentManage::OnItemchangedMemberList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	*pResult = 0;

	// 获得当前 item
	int nCurItem = 0;
	nCurItem = pNMListView->iItem;
	if (-1 != nCurItem)
	{
		m_nCurItem = nCurItem;
	}

}

void CStudentManage::ConstructTableName()
{
	CArray <CString, LPCTSTR> TableNameAry;
	CString * pCurTableName = NULL;
	UINT nCurUINT = IDS_COL_NAME_ID; 

	TableNameAry.SetSize(COLNUM);  		
	pCurTableName = TableNameAry.GetData();

	for (int i = 0; i < COLNUM; i++)
	{
		pCurTableName[i].LoadString(nCurUINT + i);
	}

	int j;
	for (j = 0; j < COLNUM; j++)
	{
		m_StuInfoList.InsertColumn(j, pCurTableName[j], LVCFMT_LEFT, 100);
	}
	
	for (j = 0; j < COLNUM; j++)
	{
		m_StuInfoList.SetColumnWidth(j, 90);
	}


}

void CStudentManage::OnKeydownMemberList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	*pResult = 0;
//	DelRecord();

}

void CStudentManage::OnDblclkMemberList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	*pResult = 0;
	GetCurRecord();
}

void CStudentManage::GetCurRecord()
{
	CStudentInfoDlg CurStuInfoDlg;
	CurStuInfoDlg.SetStuInfoPointer(&m_AllStuInfos);
	CArray <CString, LPCTSTR> StudentInfoAry;
	StudentInfoAry.SetSize(COLNUM - 1); // 减一个是减掉了ID序列号
	CString * pCurString = StudentInfoAry.GetData();
	CString strCurSQL;
	
	int nCurArySize = 0;
	nCurArySize = StudentInfoAry.GetSize();

	int i = 0;
	int j = 0;
	// 获得当前的记录的所有信息  m_nCurItem 是当前记录
	if (m_nCurItem >= 0)
	{
		for (i = 0; i < nCurArySize; i++)
		{
			pCurString[i] = m_StuInfoList.GetItemText(m_nCurItem, i+1);
		}
		CurStuInfoDlg.SetItmeInfos(&StudentInfoAry, nCurArySize);
		CurStuInfoDlg.SetNewRecordSign(FALSE);
		CurStuInfoDlg.SetStuIDReadOnly();
		if (TRUE == CurStuInfoDlg.DoModal())
		{
			//判断是否经过修改,然后做数据库的操作
			if (CurStuInfoDlg.IsCurDateChanged())
			{
				strCurSQL = CurStuInfoDlg.GetModifySQL();
				RunSQLCommand(strCurSQL);
				RefreshData();
			}
		}
	}

}
#include "resource.h"

void CStudentManage::OnContextMenu(CWnd*, CPoint point)
{
	// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1){			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);			point = rect.TopLeft();			point.Offset(5, 5);		}		CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_STUDENT_MANAGE));		CMenu* pPopup = menu.GetSubMenu(0);		ASSERT(pPopup != NULL);		CWnd* pWndPopupOwner = this;		while (pWndPopupOwner->GetStyle() & WS_CHILD)			pWndPopupOwner = pWndPopupOwner->GetParent();		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,			pWndPopupOwner);	}
}

void CStudentManage::OnEditInsert() 
{
	// TODO: Add your command handler code here
	InsertRecord();
}

void CStudentManage::OnEditModify() 
{
	// TODO: Add your command handler code here
	GetCurRecord();
}

void CStudentManage::OnEditDel() 
{
	// TODO: Add your command handler code here
	CString strSur2Del;
	strSur2Del.LoadString(IDS_SURE_TO_DEL);
	if (IDOK == AfxMessageBox(strSur2Del, MB_OKCANCEL))
	{
		DelRecord();
	}
}

void CStudentManage::OnStuManage() 
{
	// TODO: Add your command handler code here
	
}

⌨️ 快捷键说明

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