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

📄 departmentset.cpp

📁 这是采用mfc编写的工资管理系统
💻 CPP
字号:
// 
//
/***************************
文件名:DepartmentSet.cpp 
文件类型:implementation file
功能描述:对出勤信息进行记录
创建人:张国
版本号:1.0
***************************/

#include "stdafx.h"
#include "salarymanagement.h"
#include "DepartmentSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDepartmentSet dialog


CDepartmentSet::CDepartmentSet(CWnd* pParent /*=NULL*/)
	: CDialog(CDepartmentSet::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDepartmentSet)
	m_depID = _T("");
	m_depName = _T("");
	m_position = _T("");
	//}}AFX_DATA_INIT

	//初始化数据库操作对象
	m_ado.OnInitADOConn();
}

CDepartmentSet::~CDepartmentSet()
{
	m_ado.ExitConnect();
}

void CDepartmentSet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDepartmentSet)
	DDX_Control(pDX, IDC_LIST1, m_list2);
	DDX_Control(pDX, IDC_LIST2, m_list1);
	DDX_Control(pDX, IDC_COMBO1, m_cboDepartment);
	DDX_Text(pDX, IDC_DEPID, m_depID);
	DDX_Text(pDX, IDC_DEPNAME, m_depName);
	DDX_Text(pDX, IDC_POSITION, m_position);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDepartmentSet, CDialog)
	//{{AFX_MSG_MAP(CDepartmentSet)
	ON_BN_CLICKED(IDC_BUTTON1, OnAddDepartment)
	ON_BN_CLICKED(IDC_BUTTON3, OnAddPosition)
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_BN_CLICKED(IDC_DEP_DEL, OnDepDel)
	ON_BN_CLICKED(IDC_POS_DEL, OnPosDel)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST2, OnItemchangedList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDepartmentSet message handlers

BOOL CDepartmentSet::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_list1.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);

	m_list1.InsertColumn(0,"部门ID",LVCFMT_CENTER,90);
	m_list1.InsertColumn(1,"部门名称",LVCFMT_CENTER,105);

	//初始化下拉列表
	m_recordset=m_ado.GetRecordSet("Select 部门名称 From DepartmentSet");

	ComboUpdate();

	OnSelchangeCombo1();

	LoadData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/*********************
函数类型:按钮响应函数
功能描述:添加部门记录
*********************/
void CDepartmentSet::OnAddDepartment() 
{
	UpdateData();
	
	if(m_depID==""||m_depName=="")
	{
		MessageBox("请填写完整信息");
		return;
	}

	try{
		m_sql.Format("Select * From DepartmentSet Where 部门ID='%s' OR 部门名称='%s'",
			m_depID,m_depName);

		//执行SQL语句并获取结果
		m_recordset=m_ado.GetRecordSet(m_sql);
		if(!m_recordset->adoEOF)
		{
			AfxMessageBox("该记录已存在!!!");
			return;
		}

		//添加新的部门记录
		m_recordset->AddNew();
		m_recordset->PutCollect("部门ID",(_variant_t)m_depID);
		m_recordset->PutCollect("部门名称",(_variant_t)m_depName);
		m_recordset->PutCollect("职位","000000");
		m_recordset->Update();
		
		//更新右边的组合框
		ComboUpdate();

		//更新列表控件的数据
		LoadData();
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}

/*********************
函数类型:按钮响应函数
功能描述:添加职位记录
*********************/
void CDepartmentSet::OnAddPosition() 
{
	UpdateData();

	if(""==m_position)
	{
		MessageBox("请输入要添加的职位名称");
		return;
	}

	CString depName;
	m_cboDepartment.GetLBText(m_cboDepartment.GetCurSel(),depName);
	m_sql.Format("Select * From DepartmentSet Where 部门名称='%s'",depName);
	try{
		m_recordset=m_ado.GetRecordSet(m_sql);
		
		//部门信息中无职位信息
		_variant_t position=m_recordset->GetCollect("职位");
		/*	if(position.vt==VT_NULL)
		m_recordset->PutCollect("职位",(_variant_t)m_position);*/
		
		if(position==_variant_t("000000"))
			m_recordset->PutCollect("职位",(_variant_t)m_position);
		else if(position==_variant_t(m_position))
		{
			MessageBox("该职位已经存在!");
			return;
		}
		else
		{
			_variant_t depID=m_recordset->GetCollect("部门ID");
			
			m_recordset->AddNew();
			
			m_recordset->PutCollect("部门ID",depID);
			m_recordset->PutCollect("部门名称",(_variant_t)depName);
			m_recordset->PutCollect("职位",(_variant_t)m_position);
		}
		m_recordset->Update();
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}

	//更新列表框内容
	m_list2.AddString(m_position);
}

/*****************************************
函数类型:消息响应函数
功能描述:当部门组合框中的选择发生变化时,
		  用来显示部门中所有职位的组合框中
		  的内容也随之发生变化
*****************************************/
void CDepartmentSet::OnSelchangeCombo1() 
{
	UpdateData();
	CString depName;
	int index=m_cboDepartment.GetCurSel();
	if(index==-1)
	{
		return;
	}
	m_cboDepartment.GetLBText(index,depName);
	m_sql.Format("Select 职位 From DepartmentSet Where 部门名称='%s'",depName);
	
	try{
		m_recordset=m_ado.GetRecordSet(m_sql);
		m_list2.ResetContent();
		while(!m_recordset->adoEOF)
		{
			//判断结果是否为空
			_variant_t position=m_recordset->GetCollect("职位");
			if(position!=_variant_t("000000"))
				m_list2.AddString((_bstr_t)m_recordset->GetCollect("职位"));
			m_recordset->MoveNext();
		}
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}

/***********************************************
函数类型:自定义函数
功能描述:载入数据库中的部门记录信息到列表控件中
***********************************************/
void CDepartmentSet::LoadData()
{
	try
	{
		m_recordset=m_ado.GetRecordSet("Select 部门ID,部门名称 From DepartmentSet order by 部门ID");
		int nItem=0;
		m_list1.DeleteAllItems();
		CString str="";
		while(!m_recordset->adoEOF)
		{
			CString mID=(LPCTSTR)_bstr_t(m_recordset->GetCollect("部门ID"));

			if(str!=mID)
			{
				m_list1.InsertItem(nItem,mID);
				m_list1.SetItem(nItem,1,1,(_bstr_t)m_recordset->GetCollect("部门名称"),NULL,0,0,0);
				
				nItem++;
				str=mID;
			}
			m_recordset->MoveNext();
		}
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}

/*********************
函数类型:消息响应函数
功能描述:退出该窗口
*********************/
void CDepartmentSet::OnDestroy() 
{
	m_ado.ExitConnect();	
	
	CDialog::OnDestroy();
}

/*************************************************
函数类型:消息响应函数
功能描述:当点击“删除”按钮时,删除响应的部门记录
*************************************************/
void CDepartmentSet::OnDepDel() 
{
	POSITION pos = m_list1.GetFirstSelectedItemPosition();
	if (pos == NULL)
		TRACE0("No items were selected!\n");
	else
	{
		try{
			while (pos)
			{
				int nItem = m_list1.GetNextSelectedItem(pos);
				
				CString nID=m_list1.GetItemText(nItem,0);
				CString sql;
				
				sql.Format("Delete From DepartmentSet Where 部门ID='%s'",nID);
				
				m_ado.ExecuteSQL(sql);//在数据库中删除该部门信息
				
				m_list1.DeleteItem(nItem);//在列表控件中删除该部门
				
				ComboUpdate();
			}
		}
		catch(_com_error e)
		{
			AfxMessageBox(e.Description());
		}
	}
	
}

/*************************************************
函数类型:消息响应函数
功能描述:当点击“删除”按钮时,删除响应的职位记录
*************************************************/
void CDepartmentSet::OnPosDel() 
{	
	UpdateData();
	int count=m_list2.GetSelCount();	//获取候选所选关键字的个数
	int *nIndex=new int[count];			//定义一个用来存放所选关键字索引的数组
	int sum=m_list2.GetSelItems(count,nIndex);	//获取所选关键字的索引
	CString temp;
	try
	{
		for(int i=sum-1;i>=0;i--)
		{
			m_list2.GetText(nIndex[i],temp);		//获取对应索引的关键字
			
			//在数据库中删除该职位
			CString sql;
			sql.Format("Delete From DepartmentSet Where 职位='%s'",temp);
			m_ado.ExecuteSQL(sql);
			
			m_list2.DeleteString(nIndex[i]);
		}
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}

/********************************************************
函数类型:自定工函数
功能描述:根据数据库中的部门信息,更新部门组合框中的数据
********************************************************/
void CDepartmentSet::ComboUpdate()
{
	try{
		m_recordset=m_ado.GetRecordSet("Select distinct 部门名称 From DepartmentSet");
		m_cboDepartment.ResetContent();
		
		CString str="",temp;
		while(!m_recordset->adoEOF)
		{
			temp=(LPCTSTR)_bstr_t(m_recordset->GetCollect("部门名称"));
			if(str!=temp)
			{
				m_cboDepartment.AddString(temp);
				str=temp;
			}
			m_recordset->MoveNext();
		}
		m_cboDepartment.SetCurSel(0);
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
	OnSelchangeCombo1();
}

void CDepartmentSet::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	
	int nItem=pNMListView->iItem;
	CString strDepName=m_list1.GetItemText(nItem,1);

	int index=m_cboDepartment.FindString(0,strDepName);
	m_cboDepartment.SetCurSel(index);

	OnSelchangeCombo1();
	*pResult = 0;
}

⌨️ 快捷键说明

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