📄 pagedeptmanage.cpp
字号:
// PageDeptManage.cpp : implementation file
//
#include "stdafx.h"
#include "PersonelManage.h"
#include "PageDeptManage.h"
#include "PageNewDept.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPageDeptManage property page
IMPLEMENT_DYNCREATE(CPageDeptManage, CResizablePage)
CPageDeptManage::CPageDeptManage() : CResizablePage(CPageDeptManage::IDD)
{
//{{AFX_DATA_INIT(CPageDeptManage)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_psp.dwFlags &= (~PSP_HASHELP);
m_psp.dwFlags |= ( PSP_USEHICON );
HICON hIconTab = AfxGetApp()->LoadIcon(IDI_PERSON);
m_psp.hIcon = hIconTab;
}
CPageDeptManage::~CPageDeptManage()
{
}
void CPageDeptManage::DoDataExchange(CDataExchange* pDX)
{
CResizablePage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPageDeptManage)
DDX_Control(pDX, IDC_TREE_DEPT, m_ctrlDept);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPageDeptManage, CResizablePage)
//{{AFX_MSG_MAP(CPageDeptManage)
ON_BN_CLICKED(IDC_BUTTON_ADDDEPT, OnButtonAdddept)
ON_BN_CLICKED(IDC_BUTTON_DELDEPT, OnButtonDeldept)
ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPageDeptManage message handlers
BOOL CPageDeptManage::OnInitDialog()
{
CResizablePage::OnInitDialog();
// TODO: Add extra initialization here
AddAnchor(IDC_TREE_DEPT, TOP_LEFT, BOTTOM_RIGHT);
AddAnchor(IDC_BUTTON_ADDDEPT, BOTTOM_RIGHT);
AddAnchor(IDC_BUTTON_DELDEPT, BOTTOM_RIGHT);
AddAnchor(IDC_BUTTON_REFRESH, BOTTOM_RIGHT);
/////////////////////////////////////////////////////////////
tree_imageList.Create(IDB_TREE_IMAGELIST,18,7, RGB( 255, 0 ,255) );
m_ctrlDept.SetImageList(&tree_imageList, TVSIL_NORMAL);
//填充数据
FillTree();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPageDeptManage::OnButtonAdddept()
{
// TODO: Add your control notification handler code here
CPageNewDept newDept;
if(newDept.DoModal() == IDOK)
FillTree();
}
void CPageDeptManage::OnButtonDeldept()
{
// TODO: Add your control notification handler code here
HTREEITEM hItem = m_ctrlDept.GetSelectedItem();
long deptID;
if(hItem == NULL || m_ctrlDept.GetParentItem(hItem) != NULL)
{
AfxMessageBox("请选择一个部门!");
return;
}
else if(m_ctrlDept.ItemHasChildren(hItem))
{
AfxMessageBox("请先转移该部门下所有员工!");
return;
}
else
{
deptID = m_ctrlDept.GetItemData(hItem);
CString cmdText;
cmdText.Format("DELETE FROM [tblDepartment] WHERE DeptID = %d", deptID);
CADODatabase *pDb = new CADODatabase;
if(pDb->Open())
{
CADOCommand pCmd(pDb, cmdText, CADOCommand::typeCmdText);
if(pCmd.Execute(CADOCommand::typeCmdText))
{
m_ctrlDept.DeleteItem(hItem);
AfxMessageBox("操作成功!");
}
else
AfxMessageBox("删除操作成功!");
pDb->Close();
}
delete pDb;
}
}
void CPageDeptManage::FillTree()
{
long deptID, empID;
CString deptName, empName;
HTREEITEM group;
CString strQueryDept = "SELECT [DeptID],[DeptName]FROM [BlueHill].[dbo].[tblDepartment]";
CString strQueryEmp;
CADODatabase *pDb = new CADODatabase;
try
{
if(pDb->Open())
{
CADORecordset *pDeptRs = new CADORecordset(pDb);
CADORecordset *pEmpRs = new CADORecordset(pDb);
if(pDeptRs->Open(strQueryDept, CADORecordset::openQuery))
{
//先删除所有数据
m_ctrlDept.DeleteAllItems();
while(!pDeptRs->IsEOF())
{
pDeptRs->GetFieldValue("DeptName", deptName);
pDeptRs->GetFieldValue("DeptID", deptID);
group = m_ctrlDept.AddGroup(deptName, deptID);
strQueryEmp.Format("SELECT [EmployeeID],[Name] FROM [tblEmployee] WHERE [DeptID] = %d", deptID);
if(pEmpRs->Open(strQueryEmp, CADORecordset::openQuery))
{
while(!pEmpRs->IsEOF())
{
pEmpRs->GetFieldValue("EmployeeID", empID);
pEmpRs->GetFieldValue("Name", empName);
m_ctrlDept.AddChild(empName, group, empID);
pEmpRs->MoveNext();
}
pEmpRs->Close();
}
pDeptRs->MoveNext();
}
pDeptRs->Close();
}
delete pDeptRs;
delete pEmpRs;
pDb->Close();
}
delete pDb;
}
catch(CADOException)
{
}
}
void CPageDeptManage::OnButtonRefresh()
{
// TODO: Add your control notification handler code here
FillTree();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -