📄 coubuild.cpp
字号:
// CouBuild.cpp : implementation file
//
#include "stdafx.h"
#include "VirtualTeacher.h"
#include "CouBuild.h"
#include "Courses.h"
#include "CouEditDlg.h"
//#include "Users.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCouBuild dialog
CCouBuild::CCouBuild(CWnd* pParent /*=NULL*/)
: CDialog(CCouBuild::IDD, pParent)
{
//{{AFX_DATA_INIT(CCouBuild)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CCouBuild::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCouBuild)
DDX_Control(pDX, IDC_TREE1, m_tree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCouBuild, CDialog)
//{{AFX_MSG_MAP(CCouBuild)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_BN_CLICKED(IDCLOSE, OnCloseButton)
ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
ON_BN_CLICKED(IDC_MODIFY_BUTTON, OnModiButton)
ON_BN_CLICKED(IDC_DEL_BUTTON, OnDelButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCouBuild message handlers
BOOL CCouBuild::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//装入课程信息
cou.Load_course();
//添加根节点
m_treeImageList.Create(16,16,FALSE,1,0); //创建CImageList控件
//装入ICON资源
HICON hIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_ICON1));
m_treeImageList.Add(hIcon); //将ICON资源添加到CImageList控件中
//将CImageList控件与Tree控件相关联
m_tree.SetImageList(&m_treeImageList, LVSIL_NORMAL);
m_root = m_tree.InsertItem("课程建设"); //添加根节点
//调用AddtoTree() 函数,将所有课程信息添加到Tree控件中
AddtoTree(m_root, 0);
m_tree.Expand(m_root, TVE_EXPAND); //展开根节点
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CCouBuild::AddtoTree(HTREEITEM m_node, int TypeId)
{
//使用递归方法将课程添加到TreeView控件中
int i;
HTREEITEM m_child;
//依次访问cou对象中的部门数组,查找满足条件的记录添加到Tree控件中
for(i=0; i<cou.a_TypeID.GetSize(); i++)
{
//@@@@ 打开a_TypeId数组中等于TypeId的元素
//@@@@ 表明此元素为当前根节点的子节点,因此将其添加到Tree控件中,新节点为m_child
//@@@@ 使用SetItemData()函数将部门编号值也保存在新节点中
//@@@@ 以新节点为根节点,再次递归调用AddtoTree()函数,将其子节点添加到Tree控件中
if (TypeId == atoi(cou.a_TypeID.GetAt(i)))
{
m_child = m_tree.InsertItem(cou.a_CourseName.GetAt(i), m_node);
m_tree.SetItemData(m_child, atol(cou.a_CourseID.GetAt(i)));
AddtoTree(m_child, atoi(cou.a_CourseID.GetAt(i)));
}
}
}
void CCouBuild::OnAddButton()
{
// TODO: Add your control notification handler code here
HTREEITEM node; //用于保存当前选择的节点
node = m_tree.GetSelectedItem(); //取得当前选择的节点
CCouEditDlg dlg;
//设置编辑对话框中变量的值
dlg.m_Type = m_tree.GetItemText(node);
dlg.TypeId = m_tree.GetItemData(node);
dlg.CouId = 0; //表示插入记录
//如果在编辑对话框中单击“确定”按钮,则在TreeView中添加新节点
if(dlg.DoModal() == IDOK)
{
HTREEITEM child; //表示插入的新节点,它是node的子节点
child = m_tree.InsertItem(dlg.m_CouName, node); //插入节点
m_tree.SetItemData(child, dlg.CouId); //设置新节点的值
m_tree.Expand(node, TVE_EXPAND); //展开node节点
}
}
void CCouBuild::OnCloseButton()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CCouBuild::OnModiButton()
{
// TODO: Add your control notification handler code here
HTREEITEM node; //用于保存当前节点
node = m_tree.GetSelectedItem(); //取得当前节点值
if (m_tree.GetItemData(node) == 0) //不能修改根节点
{
MessageBox("不能修改此项");
return;
}
HTREEITEM parent; //读取父节点
parent = m_tree.GetParentItem(node);
CCouEditDlg dlg;
//设置编辑对话框中变量的值
dlg.m_Type = m_tree.GetItemText(parent);
dlg.TypeId = m_tree.GetItemData(parent);
dlg.CouId = m_tree.GetItemData(node);
dlg.CouName = m_tree.GetItemText(node);
dlg.m_CouName = m_tree.GetItemText(node);
//如果在编辑对话框中单击“确定”按钮,则在TreeView中修改节点信息
if(dlg.DoModal() == IDOK)
{
m_tree.SetItemText(node, dlg.m_CouName); //设置当前节点的显示标题
m_tree.Expand(node, TVE_EXPAND); //展开当前节点
}
}
void CCouBuild::OnDelButton()
{
// TODO: Add your control notification handler code here
HTREEITEM node; //用于保存当前节点
node = m_tree.GetSelectedItem(); //取得当前节点
if (m_tree.GetItemData(node) == 0) //不能删除根节点
{
MessageBox("不能删除此项");
return;
}
//取得当前节点的值,并转换为CString类型
CString cCouId;
cCouId.Format("%d", m_tree.GetItemData(node));
if (cou.HaveSon(cCouId) == 1)
{
MessageBox("此类别包含子类,不能删除!");
return;
}
if (cou.HaveFile(cCouId) == 1)
{
MessageBox("此课程包含文件,不能删除!");
return;
}
//弹出对话框,要求用户确认是否删除。如果用户单击“是”按钮,则删除
if (MessageBox("是否删除当前课程?", "请确认", MB_YESNO) == IDYES)
{
cou.sql_delete(cCouId); //删除表Departments中的指定记录
m_tree.DeleteItem(node); //删除Tree控件中的当前节点
}
}
void CCouBuild::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -