📄 staff.cpp
字号:
// Staff.cpp : 实现文件
//
#include "stdafx.h"
#include "Staff.h"
#include "choosedepart.h"
extern _ConnectionPtr pMyConnect;
CString str_choose_depart = _T("");
// CStaff 对话框
IMPLEMENT_DYNAMIC(CStaff, CDialog)
CStaff::CStaff(CWnd* pParent /*=NULL*/)
: CDialog(CStaff::IDD, pParent)
{
for (int i = 0; i < DEPART_MAX_NUM; i++)
{
str_depart[i] = _T("");
}
hSubItemNum = 0;
bAdd = FALSE;
}
CStaff::~CStaff()
{
}
void CStaff::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE_STAFF, m_tree_staff);
}
BEGIN_MESSAGE_MAP(CStaff, CDialog)
ON_BN_CLICKED(IDC_BUTTON_ADDSTAFF, &CStaff::OnBnClickedButtonAddstaff)
ON_BN_CLICKED(IDC_BUTTON_CHOOSE, &CStaff::OnBnClickedButtonChoose)
ON_BN_CLICKED(IDOK, &CStaff::OnBnClickedOk)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_STAFF, &CStaff::OnTvnSelchangedTreeStaff)
ON_NOTIFY(NM_DBLCLK, IDC_TREE_STAFF, &CStaff::OnNMDblclkTreeStaff)
END_MESSAGE_MAP()
// CStaff 消息处理程序
BOOL CStaff::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
DWORD dwStyle = GetWindowLong(m_tree_staff.m_hWnd,GWL_STYLE);
dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
SetWindowLong(m_tree_staff.m_hWnd,GWL_STYLE,dwStyle);
// Insert a root item using the structure. We must
// initialize a TVINSERTSTRUCT structure and pass its
// address to the call.
/*TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("员工列表");
hRoot = m_tree_staff.InsertItem(&tvInsert);
// Insert subitems of that root. Pennsylvania is
// a state in the United States, so its item will be a child
// of the United States item. We won't set any image or states,
// so we supply only the TVIF_TEXT mask flag. This
// override provides nearly complete control over the
// insertion operation without the tedium of initializing
// a structure. If you're going to add lots of items
// to a tree, you might prefer the structure override
// as it affords you a performance win by allowing you
// to initialize some fields of the structure only once,
// outside of your insertion loop.
HTREEITEM hPA = m_tree_staff.InsertItem(TVIF_TEXT, _T("Pennsylvania"), 0, 0, 0, 0, 0, hRoot, NULL);
// Insert the "Washington" item and assure that it is
// inserted after the "Pennsylvania" item. This override is
// more appropriate for conveniently inserting items with
// images.
HTREEITEM hWA = m_tree_staff.InsertItem(_T("Washington"), 0, 0, hRoot, hPA);
// We'll add some cities under each of the states.
// The override used here is most appropriate
// for inserting text-only items.
m_tree_staff.InsertItem(_T("Pittsburgh"), hPA, TVI_SORT);
m_tree_staff.InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
m_tree_staff.InsertItem(_T("Altoona"), hPA, TVI_SORT);
m_tree_staff.InsertItem(_T("Seattle"), hWA, TVI_SORT);
m_tree_staff.InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
m_tree_staff.InsertItem(_T("Yakima"), hWA, TVI_SORT);*/
//创建节点
//父节点
//hRoot=m_tree_staff.InsertItem(_T("Dialog1"),0,1,TVI_ROOT,TVI_LAST);
//一层子节点
//HTREEITEM sub_son0=m_tree_staff.InsertItem(_T("Dialog 1-1"),0,1,hRoot,TVI_LAST);
//HTREEITEM sub_son1=m_tree_staff.InsertItem(_T("Dialog 2-1"),0,1,hRoot,TVI_LAST);
//二层孙子节点
//HTREEITEM sub_m_son0=m_tree_staff.InsertItem(_T("Dialog 2-1-1"),0,1,sub_son1,TVI_LAST);
ResetCombo();
ResetTimeCtrl();
ListTree();
return TRUE; // return TRUE unless you set the focus to a control
}
void CStaff::ListTree()
{
m_tree_staff.DeleteAllItems();
//创建节点
//父节点
hRoot = m_tree_staff.InsertItem(_T("员工列表"),0,1,TVI_ROOT,TVI_LAST);
_RecordsetPtr m_pRs;
CString m_strSql;
hSubItemNum = 0;
m_strSql.Format(_T("select DEPART from DEPART_TABLE where ( SN > 0)") );
m_pRs.CreateInstance( __uuidof(Recordset) );
m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);
try
{
_variant_t TheValue;
m_pRs->MoveFirst();
while(m_pRs->adoEOF==VARIANT_FALSE)
{
//Retrieve column's value:
TheValue = m_pRs->Fields->GetItem(_variant_t("DEPART"))->Value;
_bstr_t s1=(_bstr_t)TheValue;
CString str_temp = s1;
str_depart[hSubItemNum] = str_temp;
hSubItemNum++;
if ( hSubItemNum == DEPART_MAX_NUM )
{
break;
}
//Do something what you want to do: ......
m_pRs->MoveNext();
}
}
catch (_com_error &e)
{
//::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
}
m_pRs->Close();
for (int i = 0; i < hSubItemNum; i++ )
{
sub_son[i] = m_tree_staff.InsertItem( str_depart[i],0,1,hRoot,TVI_LAST);
m_strSql.Format( _T("select STAFF_ID,NAME from STAFF_TABLE where ( DEPART ='") + str_depart[i]+ _T("')") );
m_pRs.CreateInstance( __uuidof(Recordset) );
m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);
try
{
_variant_t TheValue;
m_pRs->MoveFirst();
while(m_pRs->adoEOF==VARIANT_FALSE)
{
//Retrieve column's value:
TheValue = m_pRs->Fields->GetItem(_variant_t("STAFF_ID"))->Value;
_bstr_t s1=(_bstr_t)TheValue;
CString str_temp1 = s1;
TheValue = m_pRs->Fields->GetItem(_variant_t("NAME"))->Value;
_bstr_t s2=(_bstr_t)TheValue;
CString str_temp2 = s2;
CString str_temp = str_temp1 + str_temp2;
HTREEITEM sub_m_son = m_tree_staff.InsertItem( str_temp,0,1,sub_son[i],TVI_LAST);
//Do something what you want to do: ......
m_pRs->MoveNext();
}
}
catch (_com_error &e)
{
//::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
}
m_pRs->Close();
}
}
void CStaff::ResetCombo()
{
CComboBox* pWnd;
pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_SEX);
pWnd->ResetContent();
pWnd->AddString( _T("男") );
pWnd->AddString( _T("女") );
pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_EDU);
pWnd->ResetContent();
pWnd->InsertString( 0,_T("博士") );
pWnd->InsertString( 1,_T("硕士") );
pWnd->InsertString( 2,_T("本科") );
pWnd->InsertString( 3, _T("大专") );
pWnd->InsertString( 4, _T("中专") );
pWnd->InsertString( 5, _T("高中") );
}
void CStaff::ResetTimeCtrl()
{
CDateTimeCtrl* pCtrl = (CDateTimeCtrl*) GetDlgItem(IDC_DATETIMEPICKER_JOINTIME);
ASSERT(pCtrl != NULL);
pCtrl->SetFormat(_T("yyyy-MM-dd"));
CTime CurrentTime = CTime::GetCurrentTime();
pCtrl->SetTime(&CurrentTime);
}
void CStaff::ClearInputEdit()
{
CWnd* pWnd;
CString str_temp = _T("");
pWnd = GetDlgItem(IDC_EDIT_STAFFID);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_NAME);
pWnd->SetWindowText(str_temp);
ResetCombo();
pWnd = GetDlgItem(IDC_EDIT_POST);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_DEPART);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_ID);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_TEL);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_MOBILE);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_EMAIL);
pWnd->SetWindowText(str_temp);
ResetTimeCtrl();
pWnd = GetDlgItem(IDC_EDIT_ADDR);
pWnd->SetWindowText(str_temp);
pWnd = GetDlgItem(IDC_EDIT_NOTE);
pWnd->SetWindowText(str_temp);
}
void CStaff::OnBnClickedButtonAddstaff()
{
// TODO: 在此添加控件通知处理程序代码
ClearInputEdit();
CWnd* pWnd;
CString str_temp = _T("");
_RecordsetPtr m_pRs;
CString m_strSql;
CChooseDepart choosedepart;
INT_PTR nResponse = choosedepart.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此处放置处理何时用“确定”来关闭
// 对话框的代码
bAdd = TRUE;
//DEPART
pWnd = GetDlgItem(IDC_EDIT_DEPART);
pWnd->SetWindowText(str_choose_depart);
m_strSql.Format(_T("select FLAG from DEPART_TABLE where ( DEPART='") +str_choose_depart+ _T("')") );
m_pRs.CreateInstance( __uuidof(Recordset) );
m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);
try
{
_variant_t TheValue;
m_pRs->MoveFirst();
while(m_pRs->adoEOF==VARIANT_FALSE)
{
//Retrieve column's value:
TheValue = m_pRs->Fields->GetItem(_variant_t("FLAG"))->Value;
_bstr_t s1=(_bstr_t)TheValue;
CString str_temp1 = s1;
str_temp = str_temp1;
//Do something what you want to do: ......
m_pRs->MoveNext();
}
}
catch (_com_error &e)
{
//::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
}
m_pRs->Close();
//执行SQL统计命令得到包含记录条数的记录集
_variant_t RecordsAffected;
m_strSql.Format(_T("select COUNT(SN) from STAFF_TABLE ") );
m_pRs = pMyConnect->Execute( _bstr_t(m_strSql) ,&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRs->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
_bstr_t s2=(_bstr_t)vCount;
CString str_temp2 = s2;
int iStaffNumber = _wtoi(str_temp2);
iStaffNumber++;
if ( iStaffNumber == STAFF_MAX_NUM )
{
AfxMessageBox(_T("员工已经达到最大人数,不能再增加!"));
return;
}
str_temp2.Format( _T("%06d"), iStaffNumber);
str_temp = str_temp2;
pWnd = GetDlgItem(IDC_EDIT_STAFFID);
pWnd->SetWindowText(str_temp);
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
// 对话框的代码
}
}
void CStaff::OnBnClickedButtonChoose()
{
// TODO: 在此添加控件通知处理程序代码
CChooseDepart choosedepart;
INT_PTR nResponse = choosedepart.DoModal();
if (nResponse == IDOK)
{
// TODO: 在此处放置处理何时用“确定”来关闭
// 对话框的代码
CWnd* pWnd;
//DEPART
pWnd = GetDlgItem(IDC_EDIT_DEPART);
pWnd->SetWindowText(str_choose_depart);
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
// 对话框的代码
}
}
void CStaff::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
_RecordsetPtr m_pRs;
CString m_strSql;
CWnd* pWnd;
CString str_staff_staffid;
pWnd = GetDlgItem(IDC_EDIT_STAFFID);
pWnd->GetWindowText(str_staff_staffid);
if ( str_staff_staffid.GetLength() == 0 )
{
AfxMessageBox(_T("员工ID不能为空!"));
return;
}
if ( bAdd )
{
bool bHad = FALSE;
m_strSql.Format(_T("select SN from STAFF_TABLE where ( STAFF_ID ='") +str_staff_staffid+ _T("')") );
m_pRs.CreateInstance( __uuidof(Recordset) );
m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -