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

📄 pageaddemployee.cpp

📁 人事管理系统
💻 CPP
字号:
// PageAddEmployee.cpp : implementation file
//

#include "stdafx.h"
#include "PersonelManage.h"
#include "PageAddEmployee.h"

#include "SelPhotoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPageAddEmployee property page

IMPLEMENT_DYNCREATE(CPageAddEmployee, CResizablePage)

CPageAddEmployee::CPageAddEmployee() : CResizablePage(CPageAddEmployee::IDD)
{
	//{{AFX_DATA_INIT(CPageAddEmployee)
	m_strName = _T("");
	m_strLoginName = _T("");
	m_strPassword = _T("");
	m_strTelephone = _T("");
	m_iBasicSalary = 0;
	m_tBirthday = COleDateTime::GetCurrentTime();
	m_iLevel = 0;
	m_strEmail = _T("");
	m_strDeptName = _T("");
	m_strTitle = _T("");
	//}}AFX_DATA_INIT

	m_psp.dwFlags &= (~PSP_HASHELP);
	m_psp.dwFlags |= ( PSP_USEHICON );
	HICON hIconTab = AfxGetApp()->LoadIcon(IDI_PERSON);
	m_psp.hIcon = hIconTab;

	m_pPicture = NULL;
	m_nFileLen = 0;
	m_pPicBuffer = NULL;
}

CPageAddEmployee::~CPageAddEmployee()
{
	if(m_pPicture)
		m_pPicture->Release();
	if(m_pPicBuffer)
		delete [] m_pPicBuffer;
	m_pPicture = NULL;
	m_nFileLen = 0;
	m_pPicBuffer = NULL;
}

void CPageAddEmployee::DoDataExchange(CDataExchange* pDX)
{
	CResizablePage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageAddEmployee)
	DDX_Control(pDX, IDC_COMBO_DEPT, m_cmbDept);
	DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
	DDX_Text(pDX, IDC_EDIT_LOGINNAME, m_strLoginName);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPassword);
	DDX_Text(pDX, IDC_EDIT_TELEPHONE, m_strTelephone);
	DDX_Text(pDX, IDC_EDIT_BASICSALARY, m_iBasicSalary);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_BIRTHDAY, m_tBirthday);
	DDX_Text(pDX, IDC_EDIT_LEVEL, m_iLevel);
	DDX_Text(pDX, IDC_EDIT_EMAIL, m_strEmail);
	DDX_CBString(pDX, IDC_COMBO_DEPT, m_strDeptName);
	DDX_Text(pDX, IDC_EDIT_JOB, m_strTitle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageAddEmployee, CResizablePage)
	//{{AFX_MSG_MAP(CPageAddEmployee)
	ON_BN_CLICKED(IDC_BUTTON_ADDEMP, OnButtonAddemp)
	ON_BN_CLICKED(ID_BUTTTON_CANCEL, OnButttonCancel)
	ON_BN_CLICKED(IDC_BUTTON_UPLOADPHOTO, OnButtonUploadphoto)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageAddEmployee message handlers

BOOL CPageAddEmployee::OnInitDialog() 
{
	CResizablePage::OnInitDialog();
	
	// TODO: Add extra initialization here
	AddAnchor(IDC_STATIC_NAME, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_LOGINNAME, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_PASSWORD, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_BIRTHDAY, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_EMAIL, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_DEPT, TOP_LEFT, TOP_RIGHT);
	AddAnchor(IDC_STATIC_BASICSALARY, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDC_STATIC_LEVEL, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDC_CHECK_PRINT, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDC_STATIC_GRP, TOP_RIGHT);
	AddAnchor(IDC_STATIC_PHOTO, TOP_RIGHT);
	AddAnchor(IDC_BUTTON_UPLOADPHOTO, TOP_RIGHT);
	AddAnchor(IDC_STATIC_JOB, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDC_STATIC_TELEPHONE, BOTTOM_LEFT, BOTTOM_RIGHT);
	AddAnchor(IDC_BUTTON_ADDEMP, BOTTOM_RIGHT);
	AddAnchor(IDC_BUTTON_CANCEL, BOTTOM_RIGHT);

	AddAnchor(IDC_EDIT_NAME, TOP_LEFT);
	AddAnchor(IDC_EDIT_LOGINNAME, TOP_LEFT);
	AddAnchor(IDC_EDIT_PASSWORD, TOP_LEFT);
	AddAnchor(IDC_DATETIMEPICKER_BIRTHDAY, TOP_LEFT);
	AddAnchor(IDC_EDIT_EMAIL, TOP_LEFT);
	AddAnchor(IDC_COMBO_DEPT, TOP_LEFT);
	AddAnchor(IDC_EDIT_BASICSALARY, BOTTOM_LEFT);
	AddAnchor(IDC_EDIT_LEVEL, BOTTOM_LEFT);
	AddAnchor(IDC_SPIN_LEVEL, BOTTOM_LEFT);
	AddAnchor(IDC_EDIT_JOB, BOTTOM_LEFT);
	AddAnchor(IDC_EDIT_TELEPHONE, BOTTOM_LEFT);

	((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_LEVEL))->SetBuddy(GetDlgItem(IDC_EDIT_LEVEL));
	((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_LEVEL))->SetRange(0, 3);

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

void CPageAddEmployee::OnButtonAddemp() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	BYTE password[20];
	memset(password, 0, 20);
	CString strLoginName = _T("");
	int flag = 0;
	//以下做验证
	if(m_strName.GetLength() == 0)
	{
		AfxMessageBox("请输入员工姓名!");
		return;
	}
	if(m_strLoginName.GetLength() == 0)
	{
		AfxMessageBox("请输入员工登录名!");
		return;
	}
	if(m_strPassword.GetLength() > 20)
	{
		AfxMessageBox("密码过长,请重新输入!");
		return;
	}
	if(m_strEmail.GetLength() == 0)
	{
		AfxMessageBox("请输入电子邮件!");
		return;
	}
	long nDeptID = -1;
	m_strDeptName.TrimRight();
	CString strQuery = "SELECT [DeptID] FROM [BlueHill].[dbo].[tblDepartment] WHERE DeptName = '" + m_strDeptName + "'";
	
	CADODatabase *pDb = new CADODatabase;
	try
	{
		if(pDb->Open())
		{
			CADORecordset *pRs = new CADORecordset(pDb);
			//首先由部门名得到部门ID
			if(pRs->Open(strQuery, CADORecordset::openQuery))
			{
				if(pRs->GetRecordCount() > 0)
					pRs->GetFieldValue("DeptID", nDeptID);
				pRs->Close();
			}
			if(pRs->Open("tblEmployee", CADORecordset::openTable))
			{
				//首先判断相同的登录名是否存在
				flag = 0;
				while(!pRs->IsEOF())
				{
					pRs->GetFieldValue("LoginName", strLoginName);
					//strLoginName.TrimRight();
					if(strLoginName == m_strLoginName)
					{
						AfxMessageBox("登录名已存在请重新输入!");
						flag = 1;
						break;
					}
					pRs->MoveNext();
				}
				if(0 == flag)
				{
					pRs->AddNew();
					pRs->SetFieldValue("Name", m_strName);
					pRs->SetFieldValue("LoginName", m_strLoginName);
					if(m_strPassword.GetLength() > 0)
					{
						for(int i = 0, j = m_strPassword.GetLength(); i < j; i++)
							password[i] = m_strPassword[i];
						//pRs->AppendChunk("Password", (LPVOID)(BYTE *)password, 20);
						//修改密码
						_variant_t varBLOB;
						SAFEARRAY *psa;
						SAFEARRAYBOUND rgsabound[1];
						rgsabound[0].lLbound = 0;
						rgsabound[0].cElements = 20;
						psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
						BYTE *pByte;
						if(SafeArrayAccessData(psa, (void **)&pByte) == NOERROR)
							memcpy((LPVOID)pByte, (LPVOID)password, 20);
						SafeArrayUnaccessData(psa);

						varBLOB.vt = VT_ARRAY | VT_UI1;
						varBLOB.parray = psa;

						pRs->SetFieldValue("Password", varBLOB);
					}
					pRs->SetFieldValue("Email", m_strEmail);
					pRs->SetFieldValue("DeptID", nDeptID);
					pRs->SetFieldValue("BasicSalary", m_iBasicSalary);
					pRs->SetFieldValue("Title", m_strTitle);
					pRs->SetFieldValue("Telephone", m_strTelephone);
					pRs->SetFieldValue("OnboardDate", m_tBirthday);
					pRs->SetFieldValue("EmployeeLevel", m_iLevel);
					//以下保存图像数据
					if(m_pPicBuffer)
						pRs->AppendChunk("PhotoImage", (LPVOID)m_pPicBuffer, m_nFileLen);
					//更新数据,勿忘记
					if(pRs->Update())
						AfxMessageBox("添加成功!");
				}
				//关闭记录集
				pRs->Close();
			}
			delete pRs;
			pDb->Close();
		}
		delete pDb;
	}
	catch(CADOException)
	{
	}
	//清除数据
	if(0 == flag)
		OnButttonCancel();
	else
	{
		m_strLoginName = _T("");
		UpdateData(FALSE);
	}
}

void CPageAddEmployee::OnButttonCancel() 
{
	// TODO: Add your control notification handler code here
	if(m_pPicture)
		m_pPicture->Release();
	if(m_pPicBuffer)
		delete [] m_pPicBuffer;
	m_pPicture = NULL;
	m_nFileLen = 0;
	m_pPicBuffer = NULL;

	m_strName = _T("");
	m_strLoginName = _T("");
	m_strPassword = _T("");
	m_strTelephone = _T("");
	m_iBasicSalary = 0;
	m_tBirthday = COleDateTime::GetCurrentTime();
	m_iLevel = 0;
	m_strEmail = _T("");
	m_strDeptName = _T("");
	m_strTitle = _T("");

	UpdateData(FALSE);
	Invalidate();
}

void CPageAddEmployee::OnButtonUploadphoto() 
{
	// TODO: Add your control notification handler code here
	CSelPhotoDlg selPhoto;
	if(selPhoto.DoModal() == IDOK)
	{
		m_strPhotoPath = selPhoto.GetPhotoPath();
		LoadBufPicFromFile(m_strPhotoPath);
	}
}

void CPageAddEmployee::FillCMBDept()
{
	CString strQuery = _T("SELECT [DeptName] FROM [BlueHill].[dbo].[tblDepartment]");
	CString deptValue;

	CADODatabase *pDb = new CADODatabase;
	try
	{
		if(pDb->Open())
		{
			CADORecordset *pRs = new CADORecordset(pDb);
			if(pRs->Open(strQuery, CADORecordset::openQuery))
			{
				while(!pRs->IsEOF())
				{
					pRs->GetFieldValue("DeptName", deptValue);
					m_cmbDept.AddString(deptValue);
					pRs->MoveNext();
				}
				pRs->Close();
			}
			delete pRs;
			pDb->Close();
		}
		delete pDb;
	}
	catch(CADOException)
	{
	}
}

void CPageAddEmployee::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	if(m_pPicture)
	{
		CWnd *pWnd = GetDlgItem(IDC_STATIC_PHOTO);
		CRect rect;
		pWnd->GetClientRect(&rect);
		CDC *pDC = pWnd->GetDC();

		OLE_XSIZE_HIMETRIC hmWidth;
		OLE_YSIZE_HIMETRIC hmHeight;
		m_pPicture->get_Width(&hmWidth);
		m_pPicture->get_Height(&hmHeight);
		m_pPicture->Render(*pDC, 0, 0, rect.Width(), rect.Height(), 0, hmHeight, hmWidth, -hmHeight, NULL);
	}

	// Do not call CResizablePage::OnPaint() for painting messages
}

//从文件加载缓冲区与图片
BOOL CPageAddEmployee::LoadBufPicFromFile(CString strFilePath)
{
	BOOL bResult = FALSE;
	if(strFilePath.GetLength() > 0)
	{
		//清除原有数据,否则多次修改会占用大量内存
		if(m_pPicBuffer)
		{
			delete [] m_pPicBuffer;
			m_pPicBuffer = NULL;
		}
		if(m_pPicture)
		{
			m_pPicture->Release();
			m_pPicture = NULL;
		}
		m_nFileLen = 0;

		CFile cFile;
		if(cFile.Open(strFilePath, CFile::modeRead | CFile::typeBinary))
		{
			m_nFileLen = cFile.GetLength();
			m_pPicBuffer = new BYTE[m_nFileLen];
			if(cFile.ReadHuge(m_pPicBuffer, m_nFileLen) > 0)
			{
				HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, m_nFileLen);
				LPVOID pData = GlobalLock(hGlobal);
				memcpy(pData, m_pPicBuffer, m_nFileLen);
				GlobalUnlock(hGlobal);
				IStream * pStream = NULL;
				if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
				{
					//此处注意参数形式
					if(OleLoadPicture(pStream, m_nFileLen, FALSE, IID_IPicture, (LPVOID *)&m_pPicture) == S_OK)
						bResult = TRUE;
					pStream->Release();
				}
			}
		}
	}
	return bResult;
}

⌨️ 快捷键说明

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