📄 page1.cpp
字号:
}
void CPage1::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
CString sqlstr;
COleVariant var1, var2;
m_htreeitem=m_tree.GetSelectedItem();
if(m_htreeitem!=NULL)
{
GetSelectedItemtID(m_htreeitem);
sqlstr.Format("select* from filetable where id='%s'", m_selecteditemid);
m_recordset.CreateInstance(_T("ADODB.Recordset"));
HRESULT br=m_recordset->Open(sqlstr.GetBuffer(sqlstr.GetLength()), m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(br))
{
var1=m_recordset->GetCollect("isgroup");
var2=m_recordset->GetCollect("path");
if(var1.vt!=VT_NULL && var2.vt!=VT_NULL)
{
m_isgroup=var1.boolVal;
szFilePath=var2.bstrVal;
}
}
}
*pResult = 0;
}
void CPage1::OnRclickTree(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CPoint point;
::GetCursorPos(&point);
pMenu=m_menu.GetSubMenu(0);
ASSERT(pMenu);
if(m_isgroup)
{
pMenu->EnableMenuItem(ID_PAGE1MENURUN, MF_DISABLED | MF_GRAYED);
pMenu->EnableMenuItem(ID_PAGE1MENUOPEN, MF_DISABLED | MF_GRAYED);
}
else
{
pMenu->EnableMenuItem(ID_PAGE1MENURUN, MF_ENABLED);
pMenu->EnableMenuItem(ID_PAGE1MENUOPEN, MF_ENABLED);
}
pMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
*pResult = 0;
}
void CPage1::OnKeydownTree(NMHDR* pNMHDR, LRESULT* pResult)
{
TV_KEYDOWN* pTVKeyDown = (TV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
WORD nKey=pTVKeyDown->wVKey;
if(nKey==VK_DELETE)
{
MenuRemove();
}
*pResult = 0;
}
void CPage1::GetFileProperty(CString name, CString path)
{
szFileName=name;
szFilePath=path;
m_addfile=TRUE;
m_addfloder=TRUE;
m_addgroup=FALSE;
DealwithNewNode();
}
void CPage1::GetGroupProperty(CString name, CString path)
{
szFileName=name;
szFilePath=path;
m_addgroup=TRUE;
m_addfile=FALSE;
m_addfloder=FALSE;
DealwithNewNode();
}
void CPage1::DealwithNewNode()
{
TCHAR sqlstr[128];
CString date, cstr, groupname, szName, cstrlen;
BYTE *pbuf;
HICON hico;
HTREEITEM hparent, hcurrent,hchild, hsibling; //...
int ico, index, Flag, filelen, childitemcount;
SHFILEINFO fileinfo;
CFile file;
CFileException e;
VARIANT var;
COleVariant ColeVar;
SYSTEMTIME systime;
/*获取选择的文件图标*/
Flag=SHGFI_ICON;
SHGetFileInfo(szFilePath, 0, &fileinfo, sizeof(fileinfo), Flag);
hico=fileinfo.hIcon;
index=m_imaglist.Add(hico);
AutoGenerateID(m_htreeitem);//自动产生新结点的ID值
if(m_htreeitem==NULL)
{
if(m_addfile||m_addfloder)
{
szName=szFileName+" "+"\r\n"+szFilePath;//结点名称
hcurrent=m_tree.InsertItem(szName, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
else if(m_addgroup)
{
szName=szFileName+" "+"\r\n"+"0 Item";
hcurrent=m_tree.InsertItem(szName, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
}//if(m_htreeitem==NULL)
else /*(m_htreeitem!=NULL)*/
{
m_selecteditemid.Empty();
if(m_isgroup)//如果选中的结点是组
{
if(m_addfile||m_addfloder)
{
szName=szFileName+" "+"\r\n"+szFilePath;//结点名称
hcurrent=m_tree.InsertItem(szName, m_htreeitem, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
else if(m_addgroup)
{
szName=szFileName+" "+"\r\n"+"0 Item";
hcurrent=m_tree.InsertItem(szName, m_htreeitem, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
/*更新组text*/
childitemcount=0;
hchild=m_tree.GetNextItem(m_htreeitem, TVGN_CHILD);
hcurrent=hchild;
while(hcurrent!=NULL)//获取该组子结点的数目
{
hsibling=m_tree.GetNextItem(hcurrent, TVGN_NEXT);
hcurrent=hsibling;//
childitemcount++;
}
cstr.Format("%d", childitemcount);
if(childitemcount<2) //......
groupname=m_GroupName+" "+"\r\n"+cstr+" "+"Item";
else //......
groupname=m_GroupName+" "+"\r\n"+cstr+" "+"Items";
m_tree.SetItemText(m_htreeitem, groupname); //更新父结点信息
}//if(m_isgroup)
else//处理选中结点不是组的情况
{
hparent=m_tree.GetParentItem(m_htreeitem);//判断选中结点是否是根结点
if(hparent==NULL)//处理所选结点是根结点的情况
{
if(m_addfile||m_addfloder)
{
szName=szFileName+" "+"\r\n"+szFilePath;
hcurrent=m_tree.InsertItem(szName, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
else if(m_addgroup)
{
szName=szFileName+" "+"\r\n"+"0 Item";
hcurrent=m_tree.InsertItem(szName, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hcurrent, index, index);
}
}//if(hparent==NULL)
else
{
hparent=m_tree.GetParentItem(m_htreeitem);
if(m_addfile||m_addfloder)
{
szName=szFileName+" "+"\r\n"+szFilePath;
hcurrent=m_tree.InsertItem(szName, hparent, TVI_LAST);
// m_tree.SetItemImage(hcurrent, index, index);
}
else if(m_addgroup)
{
szName=szFileName+" "+"\r\n"+"0 Item";
hcurrent=m_tree.InsertItem(szName, hparent, TVI_LAST);
}
m_tree.SetItemImage(hcurrent, index, index);
/*更新组text*/
childitemcount=0;
hchild=m_tree.GetNextItem(hparent, TVGN_CHILD);
hcurrent=hchild;
while(hcurrent!=NULL)//获取该组子结点的数目
{
hsibling=m_tree.GetNextItem(hcurrent, TVGN_NEXT);
hcurrent=hsibling;//
childitemcount++;
}
cstr.Format("%d", childitemcount);
if(childitemcount<2) //......
groupname=m_GroupName+" "+"\r\n"+cstr+" "+"Item";
else //......
groupname=m_GroupName+" "+"\r\n"+cstr+" "+"Items";
m_tree.SetItemText(hparent, groupname); //更新父结点信息
}//else(hparent!=NULL)
}//else 处理选中结点不是组的情况
}//else 处理选中结点情况
GetSystemTime(&systime);
cstr.Format("%d", systime.wYear);
date+=cstr+'-';
cstr.Format("%d", systime.wMonth);
date+=cstr+'-';
cstr.Format("%d", systime.wDay);
date+=cstr;
wsprintf(sqlstr, _T("select* from filetable"));
m_recordset.CreateInstance(_T("ADODB.Recordset"));
try
{
m_recordset->Open(sqlstr, m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
m_recordset->AddNew();
m_recordset->PutCollect("parentid", _variant_t(m_parentid));
m_recordset->PutCollect("id", _variant_t(m_itemid));
if(m_addgroup)
m_recordset->PutCollect("isgroup", _variant_t("TRUE"));
else
m_recordset->PutCollect("isgroup", _variant_t("FALSE"));
m_recordset->PutCollect("name", _variant_t(szFileName));
m_recordset->PutCollect("path", _variant_t(szFilePath));
m_recordset->PutCollect("created_date", _variant_t(date));
m_recordset->Update();
}
catch(...)
{
int count;
CString errorinfo;
count=m_connect->GetErrors()->GetCount();
for(int i=0;i<count;i++)
{
errorinfo=(char*)m_connect->GetErrors()->GetItem((short)i)->GetDescription();
MessageBox(errorinfo);
}
}
}
void CPage1::AutoGenerateID(HTREEITEM hitem) //自动产生item id
{
CString itemid, cstr, cstr2, sqlstr;
int index = 1, childcount=0;
COleVariant ColeVar, ColeVar2;
HTREEITEM hprevsubling,hcurrent,hparent,hfirstchild, hsibling,hroot;
if(hitem==NULL)
{
GetSelectedItemtID(NULL);
if(m_selecteditemid=="000")//第一个结点
m_parentid="Null";
cstr2=m_selecteditemid.GetAt(m_selecteditemid.GetLength()-3);
if(cstr2!='0')
{
cstr2=m_selecteditemid.Right(3);
}
else
{
cstr2=m_selecteditemid.GetAt(m_selecteditemid.GetLength()-2);
if(cstr2!='0')
{
cstr2=m_selecteditemid.Right(2);//提取后两位数
}
else
{
cstr2=m_selecteditemid.GetAt(m_selecteditemid.GetLength()-1);//提取最后一位
}
}//
index=atoi(cstr2.GetBuffer(cstr2.GetLength()));//新结点的索引号
cstr2.Format("%d", index+1);
switch(cstr2.GetLength())
{
case 1: m_itemid="00"+cstr2;
break;
case 2: m_itemid="0"+cstr2;
break;
case 3: m_itemid=cstr2;
break;
}
return;
}//if
GetSelectedItemtID(m_htreeitem);//...
sqlstr.Format("select* from filetable where id='%s'", m_selecteditemid);
m_recordset.CreateInstance(_T("ADODB.Recordset"));
HRESULT br=m_recordset->Open(sqlstr.GetBuffer(sqlstr.GetLength()), m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(br))
{
ColeVar=m_recordset->GetCollect("isgroup");
ColeVar2=m_recordset->GetCollect("name");
if(!ColeVar.boolVal)//已选中结点,但非组
{
m_isgroup=FALSE;
hparent=m_tree.GetParentItem(m_htreeitem);
//判断是否为根结点
if(hparent==NULL)
{
m_parentid="Null";
index=1;
hroot=m_tree.GetRootItem();
while(hroot!=NULL) //计算所属根结点序号
{
if(hcurrent==hroot)
break;
hroot=m_tree.GetNextItem(hroot, TVGN_NEXT);
index++;
}//while
if(index<=9)
{
cstr.Format("%d", index);
cstr="00"+cstr;
m_itemid=cstr;
}
else if(index>9 && index<=99)
{
cstr.Format("%d", index);
cstr="0"+cstr;
m_itemid=cstr;
}
else
{
cstr.Format("%d", index);
m_itemid=cstr;
}
}//if parent==null 处理为根结点的情况
else //处理非根结点的情况
{
hparent=m_tree.GetParentItem(hitem);
index=1;
hfirstchild=m_tree.GetNextItem(hparent, TVGN_CHILD);
hcurrent=hfirstchild;
while(hcurrent!=NULL)//计算当前父结点的孩子数
{
hsibling=m_tree.GetNextItem(hcurrent, TVGN_NEXT);
hcurrent=hsibling;
index++;
}
m_parentid=m_selecteditemid.Left(m_selecteditemid.GetLength()-3);
cstr.Format("%d", index);
if(index<=9)
{
cstr="00"+cstr;
m_itemid=m_parentid+cstr;
}
else if(index>9 && index<100)
{
cstr="0"+cstr;
m_itemid=m_parentid+cstr;
}
else
{
m_itemid=m_parentid+cstr;
}
}//处理非根结点的情况
}////已选中结点,但非组
else if(ColeVar.boolVal)//处理选中结点是组的情况
{
m_isgroup=TRUE;
m_GroupName=ColeVar2.bstrVal;
hfirstchild=m_tree.GetNextItem(hitem, TVGN_CHILD);//获取根结点第一个子结点
hcurrent=hfirstchild;//
index=1;
while(hcurrent!=NULL)
{
hsibling=m_tree.GetNextItem(hcurrent, TVGN_NEXT);
hcurrent=hsibling;
index++;
}
cstr.Format("%d", index);
switch(cstr.GetLength())
{
case 1:
m_itemid=m_selecteditemid+"00"+cstr;
break;
case 2:
m_itemid=m_selecteditemid+"0"+cstr;
break;
case 3:
m_itemid=m_selecteditemid+cstr;
break;
}//switch
m_parentid=m_selecteditemid;
}// else if 处理选中结点是组的情况
}//if(SUCCEEDED(br))
}
void CPage1::GetSelectedItemtID(HTREEITEM hitem)//求选中结点的ID
{
HTREEITEM hprevsibling,hcurrent,hparent,hroot;
int index=0,nodeindex=0;
CString cstr;
m_selecteditemid.Empty();
hprevsibling=hitem;
hcurrent=hitem;
while(TRUE)
{
hparent=m_tree.GetParentItem(hcurrent);//判断是否是根结点
if(hparent==NULL)//当前结点是根
{
nodeindex=1;
hroot=m_tree.GetRootItem();
if(hroot==NULL)//当前没有任何结点
{
m_selecteditemid="000";
return;
}
while(hroot!=NULL) //计算所属根结点序号
{
if(hcurrent==hroot)
break;
hroot=m_tree.GetNextItem(hroot, TVGN_NEXT);
nodeindex++;
}//while
if(nodeindex<=9)
{
cstr.Format("%d", nodeindex);
cstr="00"+cstr;
m_selecteditemid=cstr+m_selecteditemid;
}
else if(nodeindex>9 && nodeindex<=99)
{
cstr.Format("%d", nodeindex);
cstr="0"+cstr;
m_selecteditemid=cstr+m_selecteditemid;
}
else
{
cstr.Format("%d", nodeindex);
m_selecteditemid=cstr+m_selecteditemid;
}
break;
}//if
index=0;
hprevsibling=hcurrent;
while(hprevsibling!=NULL)
{
hcurrent=hprevsibling;
hprevsibling=m_tree.GetPrevSiblingItem(hcurrent);
index++;
}
cstr.Format("%d", index);
switch(cstr.GetLength())
{
case 1:
{
cstr="00"+cstr;
m_selecteditemid=cstr+m_selecteditemid;
}
break;
case 2:
{
cstr="0"+cstr;
m_selecteditemid=cstr+m_selecteditemid;
}
break;
case 3:
{
m_selecteditemid=cstr+m_selecteditemid;
}
break;
}
hcurrent=m_tree.GetParentItem(hcurrent);
}//while
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -