UsersListView.cpp
上传用户:yuxuan88
上传日期:2022-05-09
资源大小:2290k
文件大小:6k
- // UsersListView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Inhabitants.h"
- #include "UsersListView.h"
- #include "InhabitantsDoc.h"
- #include "MainFrm.h"
- #include "House.h"
- #include "UserinfoView.h"
- #include "LeftTreeView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CUsersListView
- IMPLEMENT_DYNCREATE(CUsersListView, CListView)
- CUsersListView::CUsersListView()
- {
- }
- CUsersListView::~CUsersListView()
- {
- }
- BEGIN_MESSAGE_MAP(CUsersListView, CListView)
- //{{AFX_MSG_MAP(CUsersListView)
- ON_WM_LBUTTONDBLCLK()
- ON_COMMAND(ID_OPERATE_DELETE, OnOperateDelete)
- ON_WM_RBUTTONDOWN()
- ON_UPDATE_COMMAND_UI(ID_OPERATE_DELETE, OnUpdateOperateDelete)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CUsersListView drawing
- void CUsersListView::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- // TODO: add draw code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CUsersListView diagnostics
- #ifdef _DEBUG
- void CUsersListView::AssertValid() const
- {
- CListView::AssertValid();
- }
- void CUsersListView::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CUsersListView message handlers
- void CUsersListView::SetListCtrlStyle()
- {
- DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
- dwStyle &= ~(LVS_TYPEMASK);
- dwStyle &= ~(LVS_EDITLABELS);
-
- // Make sure we have report view and send edit label messages.
- SetWindowLong( m_hWnd, GWL_STYLE,
- dwStyle | LVS_REPORT|LVS_NOLABELWRAP|LVS_SHOWSELALWAYS);
- // Enable the full row selection and the drag drop of headers.
- DWORD styles = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES;
- // Use macro since this is new and not in MFC.
- ListView_SetExtendedListViewStyleEx(m_hWnd, styles, styles );
- }
- void CUsersListView::SetTitle()
- {
- TCHAR rgtsz[5][10] = {_T("户主"),_T("所属小区"),_T("所属楼号"),
- _T("所属单元"),_T("房间号")};
-
- LV_COLUMN lvcolumn;
- CRect rect;
- this->GetWindowRect(&rect);
- for(int i=0;i<5;i++)
- {
- lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT
- | LVCF_WIDTH | LVCF_ORDER;
- lvcolumn.fmt = LVCFMT_LEFT;
- lvcolumn.pszText = rgtsz[i];
- lvcolumn.iSubItem = i;
- lvcolumn.iOrder = i;
- lvcolumn.cx = rect.Height()/3;
- GetListCtrl().InsertColumn(i, &lvcolumn);
- }
- }
- void CUsersListView::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
-
- SetListCtrlStyle();
- SetTitle();
- }
- void CUsersListView::ShowUsers(CString strSql)
- {
- CListCtrl* p = &GetListCtrl();
- //清空列表
- p->DeleteAllItems();
- if(strSql == "")
- return;
- CInhabitantsDoc* pDoc = (CInhabitantsDoc*)GetDocument();
- CCommand<CAccessor<CHouseAccessor> > dbHouse;
- long* pCount = new long;
- //按照strSql查询符合条件的住户
- if(dbHouse.Open(pDoc->m_dbHouse.m_session,strSql,NULL,pCount) != S_OK)
- {
- AfxMessageBox("error");
- delete pCount;
- return;
- }
- int iItem = 0;
- LV_ITEM lvitem;
- int iActualItem = 0;
- //将查询得到的住户都逐个加入列表中
- if(dbHouse.MoveFirst() == S_OK)
- {
- do
- {
- for(int iSubItem=0;iSubItem<5;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? iItem : iActualItem;
- lvitem.iSubItem = iSubItem;
- lvitem.iImage = (iItem%2)?0:2;
- CString str = "";
- switch(iSubItem)
- {
- case 0://户主
- str = dbHouse.m_housemaster;
- lvitem.pszText =(LPTSTR)(LPCTSTR)str;
- break;
- case 1://小区名
- {
- str = dbHouse.m_sectionname;
- lvitem.pszText = (LPTSTR)(LPCTSTR) str;
- break;
- }
- case 2://楼号
- {
- str.Format("%d",dbHouse.m_buildingnum);
- lvitem.pszText = (LPTSTR)(LPCTSTR) str;
- break;
- }
- case 3://单元号
- {
- str.Format("%d",dbHouse.m_cellnum);
- lvitem.pszText = (LPTSTR)(LPCTSTR)str;
- break;
- }
- case 4://房间号
- {
- str.Format("%d",dbHouse.m_roomnum);
- lvitem.pszText = (LPTSTR)(LPCTSTR)str;
- }
- }
- if (iSubItem == 0)
- iActualItem = GetListCtrl().InsertItem(&lvitem);
- else
- GetListCtrl().SetItem(&lvitem);
- }
- iItem ++;
- }
- while( dbHouse.MoveNext() == S_OK );
- }
- dbHouse.Close();
- }
- void CUsersListView::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- CListView::OnLButtonDblClk(nFlags, point);
- //获得双击的项
- CListCtrl* pCtrl = &GetListCtrl();
- int nHitItem = pCtrl->HitTest(point,NULL);
- if( nHitItem < 0 )
- return;
- CString strSectionName = pCtrl->GetItemText(nHitItem,1);
- CString strBuildingNum = pCtrl->GetItemText(nHitItem,2);
- CString strCellNum = pCtrl->GetItemText(nHitItem,3);
- CString strRoomNum = pCtrl->GetItemText(nHitItem,4);
- CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- CInhabitantsDoc* pDoc = (CInhabitantsDoc*)GetDocument();
- pFrame->SwitchToView(USERINFOVIEW);
- pFrame->m_pUserinfoView->UpdateUserInfo(strSectionName,atoi(strBuildingNum.GetBuffer(0)),
- atoi(strCellNum.GetBuffer(0)),atoi(strRoomNum.GetBuffer(0)));
- }
- void CUsersListView::OnOperateDelete()
- {
- CListCtrl* pCtrl = &GetListCtrl();
- POSITION pos = pCtrl->GetFirstSelectedItemPosition();
- if( pos == NULL )
- {
- AfxMessageBox("请选择要删除的住户");
- return;
- }
- if(this->MessageBox("你真的要删除选择的所有住户吗?",
- "小区居民管理系统",MB_YESNO)==IDNO)
- return;
- //删除选择的所有住户
- while (pos)
- {
- int nHitItem = pCtrl->GetNextSelectedItem(pos);
- CString strSectionName = pCtrl->GetItemText(nHitItem,1);
- CString strBuildingNum = pCtrl->GetItemText(nHitItem,2);
- CString strCellNum = pCtrl->GetItemText(nHitItem,3);
- CString strRoomNum = pCtrl->GetItemText(nHitItem,4);
- CString strSql;
- strSql.Format("delete * from house where sectionname = '%s' and buildingnum = %s and cellnum = %s and roomnum = %s",
- strSectionName,strBuildingNum,strCellNum,strRoomNum);
- CInhabitantsDoc* pDoc = (CInhabitantsDoc*)GetDocument();
- pDoc->DeleteUser(strSql);
- }
- CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- pFrame->m_pLeftView->AddUsersToTree();
- }
- void CUsersListView::OnUpdateOperateDelete(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(GetListCtrl().GetFirstSelectedItemPosition() != NULL);
- }