📄 lefttreeview.cpp
字号:
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
if( pEmployeeRecordset->RecordCount!=0)
dlg.m_SubDepartmentName=(LPCSTR)_bstr_t(pDepartmentRecordset->GetCollect("Cname"));
else
dlg.m_SubDepartmentName="该雇员未登记部室。";
pDepartmentRecordset->Close();
}
//MessageBox("here");
dlg.DoModal();
}
else
AfxMessageBox( "此处暂时不提供属性功能!" );
}
void CLeftTreeView::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
HWND hwnd=(HWND)pNMHDR->hwndFrom;
m_TreeJoint=(CTreeCtrl*)FromHandle(hwnd);
//找到当前选中的树项
CPoint pt;
GetCursorPos(&pt);
m_TreeJoint->ScreenToClient(&pt);
HTREEITEM item=m_TreeJoint->HitTest(pt);
//CurrentDeviceID=m_TreeJoint->GetItemData(item);
//dlg.str=m_TreeJoint->GetItemText(item);
//MessageBox(dlg.str);
/*CSjsysView *pView;
CRichEditCtrl &m_edit = pView->GetRichEditCtrl();
CString str = "ABCDEFG"; //要写的内容为ABCDEFG
str += "\r\n"; //加入换行
int nTextLength = m_edit.GetWindowTextLength(); //获得文字的长度
m_edit.SetSel(nTextLength, nTextLength); //将光标放在文本最末
m_edit.ReplaceSel(str); //写入文本
AfxMessageBox( "here!" );
// AfxMessageBox(m_TreeJoint->GetItemText(item));
//bool stard=tree->GetCheck(item);*/
*pResult = 0;
}
void CLeftTreeView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
GetDocument()->UpdateAllViews(this,0,this);
//UpdateTree();
*pResult = 0;
}
void CLeftTreeView::UpdateTree()
{
CTreeCtrl &m_TreeCtrl=GetTreeCtrl();
//AfxMessageBox("UpdateTree");
///////////////////////////////////
m_TreeCtrl.DeleteAllItems();//先删除先前显示的树项
TV_INSERTSTRUCT tvRoot;//第一层
TV_INSERTSTRUCT tvSecond;//第二层
TV_INSERTSTRUCT tvThird;//第三层
TV_INSERTSTRUCT tvFourth;//第四层
HTREEITEM item_root;
HTREEITEM item_second[100];
int i=0,j=0;
HTREEITEM item_third[200];
for(i=0;i<100;i++)
item_second[i]=NULL;
for(j=0;j<200;j++)
item_third[j]=NULL;
tvRoot.hParent=NULL;
tvRoot.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvRoot.item.iImage=0;
tvRoot.item.iSelectedImage=0;
bool IsHaveCompany=true;
//插入已经分类的
//插入公司名(第一级)
try
{
if(pConnection)
{
SQLStr="select * from Department where Upnodeid=0";
//打开数据集
pDepartmentRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(pDepartmentRecordset->adoEOF)
{
AfxMessageBox("系统没有查找到任何公司");
IsHaveCompany=false;
}
CString value="";
_variant_t var;
while(!pDepartmentRecordset->adoEOF)
{
var = pDepartmentRecordset->GetCollect("CName");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
tvRoot.item.pszText=(LPTSTR)(LPCTSTR)value;
item_root=m_TreeCtrl.InsertItem (&tvRoot);
//设置树项对应的ID号
var = pDepartmentRecordset->GetCollect("ID");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
int ID=atoi(value);
m_TreeCtrl.SetItemData(item_root,ID);
pDepartmentRecordset->MoveNext();
}
pDepartmentRecordset->Close();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//如果找到公司,再去查找它下属的部门。
if(IsHaveCompany)
{
//插入部门名(第二级)
tvSecond.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvSecond.item.iImage=1;
tvSecond.item.iSelectedImage=1;
try
{
if(pConnection)
{
// HTREEITEM hroot=m_TreeCtrl.GetRootItem();
i=0;
while(item_root!=NULL)
{
tvSecond.hParent=item_root;
int nID=m_TreeCtrl.GetItemData(item_root); //获取父项ID,用于寻找属于该父项的所有子项
CString value="";
_variant_t var;
SQLStr.Format("select * from Department where Upnodeid=%d",nID);
//打开数据集
pDepartmentRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
while(!pDepartmentRecordset->adoEOF)
{
var = pDepartmentRecordset->GetCollect("CName");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
tvSecond.item.pszText=(LPTSTR)(LPCTSTR)value;
item_second[i]=m_TreeCtrl.InsertItem (&tvSecond);
//设置树项对应的ID号
var = pDepartmentRecordset->GetCollect("ID");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
int ID=atoi(value);
m_TreeCtrl.SetItemData(item_second[i],ID);
i++;
pDepartmentRecordset->MoveNext();
}
pDepartmentRecordset->Close();
item_root=m_TreeCtrl.GetPrevSiblingItem(item_root);
}
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//插入部室名(第三级)
tvThird.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvThird.item.iImage=2;
tvThird.item.iSelectedImage=2;
try
{
if(pConnection)
{
j=0;
i=0;
while(item_second[i]!=NULL)
{
//MessageBox("here!");
tvThird.hParent=item_second[i];
int nID=m_TreeCtrl.GetItemData(item_second[i]); //获取父项ID,用于寻找属于该父项的所有子项
CString value;
_variant_t var;
SQLStr.Format("select * from Department where Upnodeid=%d",nID);
//打开数据集
pDepartmentRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
while(!pDepartmentRecordset->adoEOF)
{
var = pDepartmentRecordset->GetCollect("CName");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
tvThird.item.pszText=(LPTSTR)(LPCTSTR)value;
item_third[j]=m_TreeCtrl.InsertItem (&tvThird);
/*CString str123;
str123.Format("%d",i);
AfxMessageBox(str123);*/
//设置树项对应的ID号
var = pDepartmentRecordset->GetCollect("ID");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
//AfxMessageBox(value);
int ID=atoi(value);
m_TreeCtrl.SetItemData(item_third[j],ID);
j++;
tvFourth.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
//MessageBox("here1!");
pDepartmentRecordset->MoveNext();
}
pDepartmentRecordset->Close();
i++;
}
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//插入设备名(第四级)
tvFourth.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
CString online;
int bonline; //标识是否在线
try
{
if(pConnection)
{
i=0;
while(item_third[i]!=NULL)
{
tvFourth.hParent=item_third[i];
int nID=m_TreeCtrl.GetItemData(item_third[i]); //获取父项ID,用于寻找属于该父项的所有子项
CString value="";
_variant_t var;
//
SQLStr.Format("select Device.Devicename,Device.ID,Device.Deviceip,Filterconf.bOnline,Device.Employeeid from Device left outer join Filterconf on Device.ID=Filterconf.Deviceid where Device.Employeeid in (select ID from Employee where Departmentid=%d) order by bOnline desc",nID);
//打开数据集
pDeviceRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
while(!pDeviceRecordset->adoEOF)
{
//
var = pDeviceRecordset->GetCollect("Devicename");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
tvFourth.item.pszText=(LPTSTR)(LPCTSTR)value;
var = pDeviceRecordset->GetCollect("bOnline");
if(var.vt != VT_NULL)online=(LPCSTR)_bstr_t(var);
bonline=atoi(online);
if(bonline==1)
{
tvFourth.item.iImage=4;
tvFourth.item.iSelectedImage=4;
}
else
{
tvFourth.item.iImage=3;
tvFourth.item.iSelectedImage=3;
}
HTREEITEM item_fourth=m_TreeCtrl.InsertItem(&tvFourth);
UINT id;
var = pDeviceRecordset->GetCollect("ID");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
id=atoi(value);
m_TreeCtrl.SetItemData(item_fourth,id);
pDeviceRecordset->MoveNext();
}
pDeviceRecordset->Close();
//AfxMessageBox("here!");
i++;
//MessageBox("here2!");
}
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
//插入未分类的
//插入第一层
tvRoot.hParent=NULL;
tvRoot.item.pszText="未分类";
tvRoot.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvRoot.item.iImage=0;
tvRoot.item.iSelectedImage=0;
item_root=m_TreeCtrl.InsertItem (&tvRoot);
hUnDivide=item_root;
//插入第二层
HTREEITEM item_device;
tvSecond.hParent=item_root;
tvSecond.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
try
{
if(pConnection)
{
SQLStr="select Device.ID,Device.Deviceip,Filterconf.bOnline,Device.Employeeid from Device left outer join Filterconf on Device.ID=Filterconf.Deviceid where Device.Employeeid=0 order by bOnline desc";
//打开数据集
pDeviceRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
CString value="";
_variant_t var;
while(!pDeviceRecordset->adoEOF)
{
var = pDeviceRecordset->GetCollect("Deviceip");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
tvSecond.item.pszText=(LPTSTR)(LPCTSTR)value;
CString online;
var = pDeviceRecordset->GetCollect("bOnline");
if(var.vt != VT_NULL)online=(LPCSTR)_bstr_t(var);
UINT bonline=atoi(online);
if(bonline==1)
{
tvSecond.item.iImage=4;
tvSecond.item.iSelectedImage=4;
}
else
{
tvSecond.item.iImage=3;
tvSecond.item.iSelectedImage=3;
}
item_device=m_TreeCtrl.InsertItem(&tvSecond);
UINT ID;
var = pDeviceRecordset->GetCollect("ID");
if(var.vt != VT_NULL)value=(LPCSTR)_bstr_t(var);
ID=atoi(value);
//item_device=m_TreeCtr.InsertItem(&tvSecond);
m_TreeCtrl.SetItemData(item_device,ID);
pDeviceRecordset->MoveNext();
}
pDeviceRecordset->Close();
///////////////////////
// 获得最大的ID索引值
///////////////////////
SQLStr="select max(ID) as MaxID from Device";
//打开数据集
pDeviceRecordset->Open(_bstr_t(SQLStr),
_variant_t(pConnection,true),
adOpenDynamic,
adLockOptimistic,
adCmdText);
CString maxindex="";
while(!pDeviceRecordset->adoEOF)
{
var = pDeviceRecordset->GetCollect("MaxID");
if(var.vt != VT_NULL)maxindex=(LPCSTR)_bstr_t(var);
DeviceRecordIndex=atoi(maxindex);
break;
}
pDeviceRecordset->Close();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//设置列表视图的风格
m_TreeCtrl.Expand(item_root,TVE_EXPAND);
DWORD dwStyle=GetWindowLong(m_TreeCtrl.m_hWnd ,GWL_STYLE);
dwStyle|=TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;
::SetWindowLong (m_TreeCtrl.m_hWnd ,GWL_STYLE,dwStyle);
//AfxMessageBox("here!");
}
void CLeftTreeView::OnPopUpRefresh()
{
// TODO: Add your command handler code here
UpdateTree();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -