LeftTreeView.cpp
上传用户:yuxuan88
上传日期:2022-05-09
资源大小:2290k
文件大小:12k
- // LeftTreeView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Inhabitants.h"
- #include "InhabitantsDoc.h"
- #include "LeftTreeView.h"
- #include "MainFrm.h"
- #include "UsersListView.h"
- #include "UserinfoView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CLeftTreeView
- IMPLEMENT_DYNCREATE(CLeftTreeView, CTreeView)
- CLeftTreeView::CLeftTreeView()
- {
-
- }
- CLeftTreeView::~CLeftTreeView()
- {
-
- }
- BEGIN_MESSAGE_MAP(CLeftTreeView, CTreeView)
- //{{AFX_MSG_MAP(CLeftTreeView)
- ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
- ON_COMMAND(ID_OPERATE_DELETE, OnOperateDelete)
- ON_UPDATE_COMMAND_UI(ID_OPERATE_DELETE, OnUpdateOperateDelete)
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLeftTreeView drawing
- void CLeftTreeView::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- // TODO: add draw code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLeftTreeView diagnostics
- #ifdef _DEBUG
- void CLeftTreeView::AssertValid() const
- {
- CTreeView::AssertValid();
- }
- void CLeftTreeView::Dump(CDumpContext& dc) const
- {
- CTreeView::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CLeftTreeView message handlers
- //将数据库中的所有住户到加到树中
- void CLeftTreeView::AddUsersToTree()
- {
- CInhabitantsDoc* pDoc = (CInhabitantsDoc*)GetDocument();
- if(pDoc==NULL)
- return;
- //清空树
- GetTreeCtrl().DeleteAllItems();
- //取数据库中得所有记录
- CString strSql = "select * from house";
- CCommand<CAccessor<CHouseAccessor> > dbHouse;
- long* pCount = new long;
- if(dbHouse.Open(pDoc->m_dbHouse.m_session,strSql,NULL,pCount) != S_OK)
- {
- AfxMessageBox("error");
- delete pCount;
- return;
- }
- delete pCount;
-
- if(dbHouse.MoveFirst() == S_OK)
- {
- do
- {
- AddUserToTree(dbHouse.m_sectionname,dbHouse.m_buildingnum,
- dbHouse.m_cellnum,dbHouse.m_roomnum);
- }
- while( dbHouse.MoveNext() == S_OK );
- }
- dbHouse.Close();
- }
- void CLeftTreeView::OnInitialUpdate()
- {
- CTreeView::OnInitialUpdate();
- //设置树风格
- ::SetWindowLong(m_hWnd,GWL_STYLE,WS_VISIBLE | WS_TABSTOP
- | WS_CHILD | WS_BORDER| TVS_HASBUTTONS
- | TVS_LINESATROOT | TVS_HASLINES
- | TVS_DISABLEDRAGDROP|TVS_SHOWSELALWAYS);
- //为树视图创建图标连表
- CInhabitantsApp* pApp = (CInhabitantsApp*)AfxGetApp();
- CTreeCtrl* pTreeCtrl = &GetTreeCtrl();
- m_ImageList.Create(16,16,ILC_COLOR16,4,4);
- m_ImageList.Add(pApp->LoadIcon(IDI_ICON_SECTION));
- m_ImageList.Add(pApp->LoadIcon(IDI_ICON_BUILDING));
- m_ImageList.Add(pApp->LoadIcon(IDI_ICON_CELL));
- m_ImageList.Add(pApp->LoadIcon(IDI_ICON_USER));
- pTreeCtrl->SetImageList(&m_ImageList,TVSIL_NORMAL);
- //将住户加入树中
- AddUsersToTree();
- pTreeCtrl->SortChildren(TVI_ROOT);
- }
- void CLeftTreeView::AddUserToTree(CString strSectionName,int nBuildingNum,int nCellNum,int nRoomNum)
- {
- HTREEITEM hSectionItem = AddSectionToTree(strSectionName);
- HTREEITEM hBuildingItem = AddBuildingToTree(hSectionItem,nBuildingNum);
- HTREEITEM hCellItem = AddCellToTree(hBuildingItem,nCellNum);
- if(hCellItem == NULL)
- return;
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- TV_INSERTSTRUCT TCItem;//插入数据项数据结构
- TCItem.hParent = hCellItem;
- TCItem.hInsertAfter = TVI_LAST;
- TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
- TCItem.item.lParam=0;//序号
- TCItem.item.iImage=3;//正常图标
- TCItem.item.iSelectedImage=3;//选中时图标
- CString str;
- str.Format("%d",nRoomNum);
- TCItem.item.pszText = (LPTSTR)(LPCTSTR)str;
- pCtrl->InsertItem(&TCItem);
- pCtrl->SortChildren(hCellItem);
- }
- //将小区加到树视图中
- HTREEITEM CLeftTreeView::AddSectionToTree(CString strSectionName)
- {
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- HTREEITEM hRootItem = pCtrl->GetRootItem();
- if(hRootItem)
- {
- while(hRootItem)
- {
- CString strItemText = pCtrl->GetItemText(hRootItem);
- if(strItemText == strSectionName)
- return hRootItem;
- hRootItem = pCtrl->GetNextSiblingItem(hRootItem);
- }
- }
- TV_INSERTSTRUCT TCItem;//插入数据项数据结构
- TCItem.hParent = TVI_ROOT;
- TCItem.hInsertAfter = TVI_LAST;
- TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
- TCItem.item.lParam=0;//序号
- TCItem.item.iImage=0;//正常图标
- TCItem.item.iSelectedImage=0;//选中时图标
- TCItem.item.pszText = (LPTSTR)(LPCTSTR)strSectionName;
- HTREEITEM hSectionItem = pCtrl->InsertItem(&TCItem);
- pCtrl->SortChildren(TVI_ROOT);
- return hSectionItem;
- }
- HTREEITEM CLeftTreeView::AddBuildingToTree(HTREEITEM hSectionItem, int nBuildingnum)
- {
- if(hSectionItem == NULL)
- return NULL;
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- CString strBuildingNum;
- strBuildingNum.Format("%d号楼",nBuildingnum);
- if(pCtrl->ItemHasChildren(hSectionItem))
- {
- HTREEITEM hItem = pCtrl->GetChildItem(hSectionItem);
- while(hItem)
- {
- CString strItemText = pCtrl->GetItemText(hItem);
- if(strItemText == strBuildingNum)
- return hItem;
- hItem = pCtrl->GetNextSiblingItem(hItem);
- }
- }
- TV_INSERTSTRUCT TCItem;//插入数据项数据结构
- TCItem.hParent = hSectionItem;
- TCItem.hInsertAfter = TVI_LAST;
- TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
- TCItem.item.lParam=0;//序号
- TCItem.item.iImage=1;//正常图标
- TCItem.item.iSelectedImage=1;//选中时图标
- TCItem.item.pszText = (LPTSTR)(LPCTSTR)strBuildingNum;
- HTREEITEM hBuildingItem = pCtrl->InsertItem(&TCItem);
- pCtrl->SortChildren(hSectionItem);
- return hBuildingItem;
- }
- HTREEITEM CLeftTreeView::AddCellToTree(HTREEITEM hBuildingItem, int nCellnum)
- {
- if(hBuildingItem == NULL)
- return NULL;
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- CString strCellNum;
- strCellNum.Format("%d单元",nCellnum);
- if(pCtrl->ItemHasChildren(hBuildingItem))
- {
- HTREEITEM hItem = pCtrl->GetChildItem(hBuildingItem);
- while(hItem)
- {
- CString strItemText = pCtrl->GetItemText(hItem);
- if(strItemText == strCellNum)
- return hItem;
- hItem = pCtrl->GetNextSiblingItem(hItem);
- }
- }
- TV_INSERTSTRUCT TCItem;//插入数据项数据结构
- TCItem.hParent = hBuildingItem;
- TCItem.hInsertAfter = TVI_LAST;
- TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
- TCItem.item.lParam=0;//序号
- TCItem.item.iImage=2;//正常图标
- TCItem.item.iSelectedImage=2;//选中时图标
- TCItem.item.pszText = (LPTSTR)(LPCTSTR)strCellNum;
- HTREEITEM hCellItem = pCtrl->InsertItem(&TCItem);
- pCtrl->SortChildren(hBuildingItem);
- return hCellItem;
- }
- void CLeftTreeView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
-
- *pResult = 0;
- //获得被选择项
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- HTREEITEM hSelItem = pCtrl->GetSelectedItem();
- m_hHitItem = hSelItem;
- //如果没有项被选择,则将右边的列表视图清空
- CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- if(hSelItem == NULL)
- {
- pFrame->SwitchToView(USERSVIEW);
- pFrame->m_pUsersView->ShowUsers("");
- return;
- }
- //判断选择项在树中的位置
- HTREEITEM hParentItem = pCtrl->GetParentItem(hSelItem);
- CString strSql,strSectionName,strBuildingNum,strCellNum,strRoomNum;
- if(hParentItem == NULL)
- { //小区
- CString strSectionName = pCtrl->GetItemText(hSelItem);
- strSql.Format("select * from house where sectionname = '%s'",strSectionName);
- pFrame->SwitchToView(USERSVIEW);
- pFrame->m_pUsersView->ShowUsers(strSql);
- return;
- }
- hParentItem = pCtrl->GetParentItem(hParentItem);
- if(hParentItem == NULL)
- { //楼
- strSectionName = pCtrl->GetItemText(pCtrl->GetParentItem(hSelItem));
- CString str = pCtrl->GetItemText(hSelItem);
- int n = str.Find("号楼");
- strBuildingNum = str.Left(n);
- strSql.Format("select * from house where sectionname = '%s' and buildingnum = %s",
- strSectionName,strBuildingNum);
- pFrame->SwitchToView(USERSVIEW);
- pFrame->m_pUsersView->ShowUsers(strSql);
- return;
- }
- hParentItem = pCtrl->GetParentItem(hParentItem);
- if(hParentItem == NULL)
- { //单元
- strSectionName = pCtrl->GetItemText(pCtrl->GetParentItem
- (pCtrl->GetParentItem(hSelItem)));
- CString str = pCtrl->GetItemText(pCtrl->GetParentItem(hSelItem));
- int n = str.Find("号楼");
- strBuildingNum = str.Left(n);
- str = pCtrl->GetItemText(hSelItem);
- n = str.Find("单元");
- strCellNum = str.Left( n );
- strSql.Format("select * from house where sectionname = '%s' and buildingnum = %s and cellnum = %s",
- strSectionName,strBuildingNum,strCellNum);
- pFrame->SwitchToView(USERSVIEW);
- pFrame->m_pUsersView->ShowUsers(strSql);
- return;
- }
- //选择项是房间号,则右边视图显示该住户的详细信息
- strSectionName = pCtrl->GetItemText(pCtrl->GetParentItem
- (pCtrl->GetParentItem(pCtrl->GetParentItem(hSelItem))));
- CString str = pCtrl->GetItemText(pCtrl->GetParentItem
- (pCtrl->GetParentItem(hSelItem)));
- int n = str.Find("号楼");
- strBuildingNum = str.Left(n);
- str = pCtrl->GetItemText(pCtrl->GetParentItem(hSelItem));
- n = str.Find("单元");
- strCellNum = str.Left( n );
- strRoomNum = pCtrl->GetItemText(hSelItem);
- pFrame->SwitchToView(USERINFOVIEW);
- pFrame->m_pUserinfoView->UpdateUserInfo(strSectionName,
- atoi(strBuildingNum.GetBuffer(0)),
- atoi(strCellNum.GetBuffer(0)),
- atoi(strRoomNum.GetBuffer(0)));
- }
- void CLeftTreeView::OnOperateDelete()
- {
- if( m_hHitItem == NULL )
- return;
- CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- CInhabitantsDoc* pDoc = (CInhabitantsDoc*)GetDocument();
- CTreeCtrl* pCtrl = &GetTreeCtrl();
- HTREEITEM hParentItem = pCtrl->GetParentItem( m_hHitItem );
- CString strSql;
- if( hParentItem == NULL && m_hHitItem != NULL )
- { //小区
- CString strSectionName = pCtrl->GetItemText(m_hHitItem);
- strSql.Format("delete * from house where sectionname = '%s'",strSectionName);
- if(this->MessageBox("你真的要删除该小区中的所有住户吗?",
- "小区居民管理系统",MB_YESNO)==IDNO)
- return;
- }
- else if( hParentItem != NULL && pCtrl->GetParentItem(hParentItem) == NULL )
- { //楼
- CString strSectionName = pCtrl->GetItemText(pCtrl->GetParentItem(m_hHitItem));
- CString str = pCtrl->GetItemText(m_hHitItem);
- int n = str.Find("号楼");
- CString strBuildingNum = str.Left(n);
- strSql.Format("delete * from house where sectionname = '%s' and buildingnum = %s",
- strSectionName,strBuildingNum);
- if(this->MessageBox("你真的要删除该楼中的所有住户吗?",
- "小区居民管理系统",MB_YESNO)==IDNO)
- return;
- }
- else if( pCtrl->GetParentItem(hParentItem) != NULL
- && pCtrl->GetParentItem(pCtrl->GetParentItem(hParentItem)) == NULL )
- { //单元
- CString strSectionName = pCtrl->GetItemText(
- pCtrl->GetParentItem(pCtrl->GetParentItem(m_hHitItem)));
- CString str = pCtrl->GetItemText(pCtrl->GetParentItem(m_hHitItem));
- int n = str.Find("号楼");
- CString strBuildingNum = str.Left(n);
- str = pCtrl->GetItemText(m_hHitItem);
- n = str.Find("单元");
- CString strCellNum = str.Left( n );
- strSql.Format("delete * from house where sectionname = '%s' and buildingnum = %s and cellnum = %s",
- strSectionName,strBuildingNum,strCellNum);
- if(this->MessageBox("你真的要删除该单元中的所有住户吗?",
- "小区居民管理系统",MB_YESNO)==IDNO)
- return;
- }
- else
- { //用户
- CString strSectionName = pCtrl->GetItemText(pCtrl->GetParentItem(
- pCtrl->GetParentItem(pCtrl->GetParentItem(m_hHitItem))));
- CString str = pCtrl->GetItemText(pCtrl->GetParentItem(
- pCtrl->GetParentItem(m_hHitItem)));
- int n = str.Find("号楼");
- CString strBuildingNum = str.Left(n);
- str = pCtrl->GetItemText(pCtrl->GetParentItem(m_hHitItem));
- n = str.Find("单元");
- CString strCellNum = str.Left( n );
- CString strRoomNum = pCtrl->GetItemText(m_hHitItem);
- strSql.Format("delete * from house where sectionname = '%s' and buildingnum = %s and cellnum = %s and roomnum = %s",
- strSectionName,strBuildingNum,strCellNum,strRoomNum);
- if(this->MessageBox("你真的要删除该住户吗?","小区居民管理系统",MB_YESNO)==IDNO)
- return;
- }
- pDoc->DeleteUser(strSql);
- AddUsersToTree();
- }
- void CLeftTreeView::OnUpdateOperateDelete(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_hHitItem != NULL);
- }