📄 modifyempinfodlg.cpp
字号:
// ModifyEmpInfoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PersonelManage.h"
#include "ModifyEmpInfoDlg.h"
#include "SelPhotoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CModifyEmpInfoDlg dialog
CModifyEmpInfoDlg::CModifyEmpInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModifyEmpInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModifyEmpInfoDlg)
m_strEmail = _T("");
m_iLevel = 0;
m_strName = _T("");
m_strLoginName = _T("");
m_strPassword = _T("");
m_strTelephone = _T("");
m_strTitle = _T("");
m_iBasicSalary = 0;
//}}AFX_DATA_INIT
m_pPicture = NULL;
m_nFileLen = 0;
m_pPicBuffer = NULL;
}
CModifyEmpInfoDlg::~CModifyEmpInfoDlg()
{
if(m_pPicture)
m_pPicture->Release();
if(m_pPicBuffer)
delete [] m_pPicBuffer;
}
void CModifyEmpInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModifyEmpInfoDlg)
DDX_Text(pDX, IDC_EDIT_EMAIL, m_strEmail);
DDX_Text(pDX, IDC_EDIT_LEVEL, m_iLevel);
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_JOB, m_strTitle);
DDX_Text(pDX, IDC_EDIT_BASICSALARY, m_iBasicSalary);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModifyEmpInfoDlg, CDialog)
//{{AFX_MSG_MAP(CModifyEmpInfoDlg)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_BUTTON_UPLOADPHOTO, OnButtonUploadphoto)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModifyEmpInfoDlg message handlers
void CModifyEmpInfoDlg::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 CDialog::OnPaint() for painting messages
}
BOOL CModifyEmpInfoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//注意关于图片的显示
((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_LEVEL))->SetBuddy(GetDlgItem(IDC_EDIT_LEVEL));
((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_LEVEL))->SetRange(0, 3);
// TODO: Add extra initialization here
CString strQuery = "SELECT [Name],[LoginName],CAST([Password] AS varchar(20)) AS [Password],[Email],[BasicSalary],[Title],\
[Telephone],[EmployeeLevel],[PhotoImage] FROM [BlueHill].[dbo].[tblEmployee] WHERE EmployeeID = " + m_strEmpID;
CADODatabase *pDb = new CADODatabase;
try
{
if(pDb->Open())
{
CADORecordset *pRs = new CADORecordset(pDb);
if(pRs->Open(strQuery, CADORecordset::openQuery))
{
if(pRs->GetRecordCount() != 0)
{
pRs->MoveFirst();
pRs->GetFieldValue("Name", m_strName);
pRs->GetFieldValue("LoginName", m_strLoginName);
pRs->GetFieldValue("Password", m_strPassword);
pRs->GetFieldValue("Email", m_strEmail);
pRs->GetFieldValue("BasicSalary", m_iBasicSalary);
pRs->GetFieldValue("Title", m_strTitle);
pRs->GetFieldValue("Telephone", m_strTelephone);
pRs->GetFieldValue("EmployeeLevel", m_iLevel);
//以下得到照片数据
m_nFileLen = pRs->GetRecordset()->GetFields()->GetItem("PhotoImage")->GetActualSize();
if(m_nFileLen > 0)
{
m_pPicBuffer = new BYTE[m_nFileLen];
if(m_pPicBuffer)
{
if(pRs->GetChunk("PhotoImage", (LPVOID)m_pPicBuffer))
LoadPicFromBuffer(); //加载图片
}
}
UpdateData(FALSE);
}
pRs->Close();
}
delete pRs;
pDb->Close();
}
delete pDb;
}
catch(CADOException &e)
{
AfxMessageBox(e.GetErrorMessage());
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CModifyEmpInfoDlg::OnButtonUploadphoto()
{
// TODO: Add your control notification handler code here
CSelPhotoDlg selPhoto;
if(selPhoto.DoModal() == IDOK)
{
m_strPhotoPath = selPhoto.GetPhotoPath();
LoadBufPicFromFile(m_strPhotoPath);
}
}
BOOL CModifyEmpInfoDlg::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;
}
BOOL CModifyEmpInfoDlg::LoadPicFromBuffer()
{
BOOL bResult = FALSE;
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;
}
void CModifyEmpInfoDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
CString strEmpID = _T("");
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;
}
CADODatabase *pDb = new CADODatabase;
try
{
if(pDb->Open())
{
CADORecordset *pRs = new CADORecordset(pDb);
if(pRs->Open("tblEmployee", CADORecordset::openTable))
{
//首先判断相同的登录名是否存在
flag = 0;
pRs->MoveFirst();
while(!pRs->IsEOF())
{
pRs->GetFieldValue("LoginName", strLoginName);
pRs->GetFieldValue("EmployeeID", strEmpID);
strEmpID.TrimRight();
//strLoginName.TrimRight();
if((strEmpID != m_strEmpID) && (strLoginName == m_strLoginName))
{
AfxMessageBox("登录名已存在请重新输入!");
flag = 1;
break;
}
pRs->MoveNext();
}
///////////////////////////////////////////////
if(0 == flag)
{
strEmpID = _T("");
pRs->MoveFirst();
while(!pRs->IsEOF())
{
pRs->GetFieldValue("EmployeeID", strEmpID);
strEmpID.TrimRight();
if(strEmpID == m_strEmpID)
{
pRs->Edit();
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("BasicSalary", m_iBasicSalary);
pRs->SetFieldValue("Title", m_strTitle);
pRs->SetFieldValue("Telephone", m_strTelephone);
pRs->SetFieldValue("EmployeeLevel", m_iLevel);
//以下保存图像数据
if(m_pPicBuffer)
pRs->AppendChunk("PhotoImage", (LPVOID)m_pPicBuffer, m_nFileLen);
//更新数据,勿忘记
if(pRs->Update())
AfxMessageBox("修改成功!");
break;
}
pRs->MoveNext();
}
}
/////////////////////////////////////////
}
//关闭记录集
pRs->Close();
delete pRs;
}
pDb->Close();
delete pDb;
}
catch(CADOException)
{
}
if(1 == flag)
{
m_strLoginName = _T("");
UpdateData(FALSE);
return;
}
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -