// DLG_UserSelect.cpp : implementation file // #include "stdafx.h" #include "corpsms.h" #include "DLG_UserSelect.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // DLG_UserSelect dialog #include "MainFrm.h" DLG_UserSelect::DLG_UserSelect(CWnd* pParent /*=NULL*/) : CDialog(DLG_UserSelect::IDD, pParent) , m_strName(_T("")) { //{{AFX_DATA_INIT(DLG_UserSelect) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_lUserID = -1; m_strRoot = _T("所有人员"); m_bGroup = false; m_strDB = _T("Users"); m_bShowUser = true; m_lShowType = 0; m_bFirstFind = true; m_hSearched = NULL; } void DLG_UserSelect::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(DLG_UserSelect) DDX_Control(pDX, IDC_USERSELECT_TREE, m_wndTree); //}}AFX_DATA_MAP DDX_Control(pDX, IDC_USERSELECT_NAME, m_wndName); DDX_Text(pDX, IDC_USERSELECT_NAME, m_strName); DDV_MaxChars(pDX, m_strName, 32); } BEGIN_MESSAGE_MAP(DLG_UserSelect, CDialog) //{{AFX_MSG_MAP(DLG_UserSelect) ON_NOTIFY(NM_DBLCLK, IDC_USERSELECT_TREE, OnDblclkUserselectTree) //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_USERSELECT_NAME_S, &DLG_UserSelect::OnBnClickedUserselectNameS) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // DLG_UserSelect message handlers void DLG_UserSelect::SetParam(CMainFrame *pFrame,CString strDB , CString strRoot,BOOL bShowUser) { m_pMainFrame = pFrame; m_AdoRS.SetAdoConnection( &pFrame->m_adoConnection ); m_strDB = strDB; m_bShowUser = bShowUser; m_strRoot = strRoot; } long DLG_UserSelect::GetParam(CString & strUser) { strUser = m_strUser; return m_lUserID; } BOOL DLG_UserSelect::OnInitDialog() { CDialog::OnInitDialog(); //创建图像列表 m_Image.Create( 20 , 20 , ILC_COLOR32|ILC_MASK , 5 , 1); CBitmap bmp; bmp.LoadBitmap( IDB_F_FOLDER ); m_Image.Add( &bmp , RGB(255,255,255) ); //文件夹 bmp.DeleteObject(); bmp.LoadBitmap( IDB_F_FOLDER_O ); m_Image.Add( &bmp , RGB(255,255,255) ); //文件夹-打开 bmp.DeleteObject(); bmp.LoadBitmap( IDB_F_GADDRESS ); m_Image.Add( &bmp , RGB(255,255,255) ); //公用地址本 bmp.DeleteObject(); bmp.LoadBitmap( IDB_L_USER_NOR ); m_Image.Add( &bmp , RGB(193,193,193) ); //用户 m_strUser = m_strRoot; m_wndTree.SetBkColor( RGB(180,213,170) ); m_wndTree.SetImageList(&m_Image, TVSIL_NORMAL ); m_MainItem = m_wndTree.InsertItem( m_strRoot , 2 , 2 ); m_wndTree.SetItemData( m_MainItem , -1 ); ShowGGroup( m_MainItem , _T("") ); m_wndTree.Expand(m_MainItem,TVE_EXPAND); m_wndTree.SelectItem( m_MainItem ); if ( m_lShowType==1 ) { this->SetWindowText(_T("请选择需要查询的用户")); } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL DLG_UserSelect::ShowGGroup(HTREEITEM hMainItem, TCHAR *pGroupID) { CAdoRecordSet AdoRS; CAdoRecordSet AdoRS2; AdoRS.SetAdoConnection( &m_pMainFrame->m_adoConnection ); AdoRS2.SetAdoConnection(&m_pMainFrame->m_adoConnection); TCHAR szSelect[128]={0}; _stprintf( szSelect , _T("select * from %s where GroupID like '%s_' AND UserType='0' order by CreateTime") , m_strDB , pGroupID ); BOOL b = AdoRS.Open( szSelect ); long lCount = AdoRS.GetRecordCount(); if ( b && lCount ) { AdoRS.MoveFirst(); while( !AdoRS.IsEOF() ) { TCHAR szGroupID[16]={0}; TCHAR szName[16]={0}; long lUserID=0; AdoRS.GetCollect( _T("GroupID") , szGroupID ); AdoRS.GetCollect( _T("Name") , szName ); AdoRS.GetCollect( _T("UserID") , lUserID ); HTREEITEM hItem = m_wndTree.InsertItem( szName , 0,1 , hMainItem ); m_wndTree.SetItemData( hItem , lUserID ); /* if (m_bShowUser) { //显示完组之后,显示用户 if (_tcslen(szGroupID) <= 0) { _stprintf(szSelect, _T("select * from %s where GroupID = '' OR GroupID is NULL AND UserType='1' order by CreateTime "), m_strDB); } else { _stprintf(szSelect, _T("select * from %s where GroupID = '%s' AND UserType='1' order by CreateTime"), m_strDB, szGroupID); } b = AdoRS2.Open(szSelect); lCount = AdoRS2.GetRecordCount(); if (b && lCount) { AdoRS2.MoveFirst(); while (!AdoRS2.IsEOF()) { CString strShow; CString strName; CString strLoginName; long lUserID = 0; AdoRS2.GetCollect(_T("Name"), strName); AdoRS2.GetCollect(_T("LoginName"), strLoginName); AdoRS2.GetCollect(_T("UserID"), lUserID); strShow.Format(_T("%s [%s]"), strName, strLoginName); //if ( m_lShowType==1 ) //显示姓名+手机号 //{ // CString strMobile; // AdoRS.GetCollect( _T("Mobile") , strMobile ); // // CString strTemp; // strTemp.Format( _T("%s[%s]") , szName , strMobile ); // _tcscpy(szName , strTemp); //} HTREEITEM hItem2 = m_wndTree.InsertItem(strShow, 3, 3, hItem); m_wndTree.SetItemData(hItem2, lUserID); AdoRS2.MoveNext(); } } } */ ShowGGroup( hItem , szGroupID ); AdoRS.MoveNext(); } } if ( m_bShowUser ) { //显示完组之后,显示用户 if ( _tcslen(pGroupID)<=0 ) { _stprintf( szSelect , _T("select * from %s where GroupID = '' OR GroupID is NULL AND UserType='1' order by CreateTime "),m_strDB ); } else { _stprintf( szSelect , _T("select * from %s where GroupID = '%s' AND UserType='1' order by CreateTime") ,m_strDB, pGroupID ); } b = AdoRS.Open( szSelect ); lCount = AdoRS.GetRecordCount(); if ( b && lCount ) { AdoRS.MoveFirst(); while( !AdoRS.IsEOF() ) { CString strShow; CString strName; CString strLoginName; long lUserID=0; AdoRS.GetCollect( _T("Name") , strName); AdoRS.GetCollect(_T("LoginName"), strLoginName); AdoRS.GetCollect( _T("UserID") , lUserID ); strShow.Format(_T("%s [%s]"), strName, strLoginName); //if ( m_lShowType==1 ) //显示姓名+手机号 //{ // CString strMobile; // AdoRS.GetCollect( _T("Mobile") , strMobile ); // // CString strTemp; // strTemp.Format( _T("%s[%s]") , szName , strMobile ); // _tcscpy(szName , strTemp); //} HTREEITEM hItem = m_wndTree.InsertItem(strShow, 3,3 , hMainItem ); m_wndTree.SetItemData( hItem , lUserID ); AdoRS.MoveNext(); } } } return true; } void DLG_UserSelect::OnOK() { HTREEITEM hItem = m_wndTree.GetSelectedItem(); if ( !hItem ) { MessageBox( _T("你必须从左边的树状列表中选择一个查询对象!"),_T("错误"),MB_ICONWARNING); return ; } m_strUser = m_wndTree.GetItemText(hItem); m_lUserID = m_wndTree.GetItemData(hItem); int iImage=0; int iSImage=0; m_wndTree.GetItemImage(hItem,iImage,iSImage); if ( iImage==0 || iImage== 2 ) m_bGroup = true; else m_bGroup = false; //生成树状查询用户名 CString strTemp; HTREEITEM hParItem = m_wndTree.GetParentItem(hItem); while (hParItem != NULL && hParItem != m_MainItem) { strTemp = m_wndTree.GetItemText(hParItem); m_strUser = strTemp+"\\"+m_strUser; hParItem = m_wndTree.GetParentItem(hParItem); } if ( m_lShowType==1 ) //显示姓名+手机号 { if ( m_bGroup && m_lUserID!=-1 ) //只能选择用户,不能选择用户组 { MessageBox( _T("你必须从左边的树状列表中选择一个用户!"),_T("错误"),MB_ICONWARNING); return ; } /* //分解姓名与号码 CString strTemp= m_wndTree.GetItemText(hItem);; long lBegin=0; long lEnd=0; lBegin=strTemp.Find(_T("[")); lEnd=strTemp.Find(_T("]")); if ( lBegin>=0 ) m_strUser = strTemp.Left(lBegin); if ( lEnd>lBegin ) { m_strMobile =strTemp.Mid(lBegin+1,lEnd-lBegin-1); } if ( !::isMobileNum(m_strMobile , m_strMobile.GetLength()) ) { MessageBox( _T("所选择业务员的手机号码不正确,请修正或重选!"),_T("错误"),MB_ICONWARNING); return ; } */ } CDialog::OnOK(); } void DLG_UserSelect::OnDblclkUserselectTree(NMHDR* pNMHDR, LRESULT* pResult) { *pResult = 0; OnOK(); } void DLG_UserSelect::SetShowType(long lShowType) { m_lShowType =lShowType; //0-只显示姓名,1-显示姓名+手机号码 } BOOL DLG_UserSelect::PreTranslateMessage(MSG* pMsg) { //此举是为了用户在输入号码时按了回车然后转到输入内容 //定位客户 if (pMsg->message == WM_KEYDOWN) { if (pMsg->wParam == VK_RETURN && pMsg->hwnd == m_wndName) { SearchItem(); return true; } else { m_bFirstFind = true; //只要输入了非回车,都是当重新搜索 } } return CDialog::PreTranslateMessage(pMsg); } BOOL DLG_UserSelect::SearchItem(void) { CString strName; m_wndName.GetWindowText(strName); if (strName.GetLength() <= 0) { MessageBox(_T("请输入需要查询的关键字!"),_T("错误"), MB_ICONWARNING); m_wndName.SetFocus(); return false; } HTREEITEM hItem = m_wndTree.GetSelectedItem(); if (hItem == NULL) //如果当前选择为空,相当于重新开始搜索 { m_bFirstFind = true; m_hSearched = NULL; } HTREEITEM hItem2; if (m_bFirstFind) //重新搜索 { m_ASearch.RemoveAll(); hItem2 = FindItemName(m_MainItem, strName,NULL); m_bFirstFind = false; //不是重新搜索了。 } else { hItem2 = FindItemName(m_MainItem, strName, m_hSearched); } if (hItem2) { m_hSearched = hItem2; m_wndTree.SelectItem(hItem2); return true; } else { CString str; str.Format(_T("搜索关键字:%s,已到末尾。"), strName); MessageBox(str,_T("信息") , MB_ICONINFORMATION); m_bFirstFind = true; //重新开始搜索 m_ASearch.RemoveAll(); } return false; } HTREEITEM DLG_UserSelect::FindItemName(HTREEITEM hNodeItem, CString strName, HTREEITEM hSearched) { CString strItemName = m_wndTree.GetItemText(hNodeItem); if (strItemName.MakeUpper().Find(strName.MakeUpper()) >= 0) { long lUserID = m_wndTree.GetItemData(hNodeItem); long lLength = m_ASearch.GetCount(); for (int i = 0; i < lLength; i++) { if (lUserID == m_ASearch.GetAt(i)) break; } if (i >= lLength) //没有找过的,返回 { m_ASearch.Add(lUserID); return hNodeItem; } } //先搜子节点 HTREEITEM hCurItem = m_wndTree.GetChildItem(hNodeItem);//得到当前节点的第一个子节点的句柄 HTREEITEM hNextItem=NULL; while (hCurItem != NULL) { hNextItem = hCurItem; HTREEITEM hItem2 = FindItemName(hNextItem, strName, hSearched); if (hItem2 != NULL) { return hItem2; } hCurItem = m_wndTree.GetNextSiblingItem(hCurItem);//得到当前节点的兄弟节点的句柄 } return NULL; } void DLG_UserSelect::OnBnClickedUserselectNameS() { SearchItem(); }