📄 page1.cpp
字号:
// Page1.cpp : implementation file
//
#include "stdafx.h"
#include "PStart.h"
#include "Page1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPage1 dialog
CPage1::CPage1(CWnd* pParent /*=NULL*/)
: CDialog(CPage1::IDD, pParent)
{
//{{AFX_DATA_INIT(CPage1)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_htreeitem=NULL;
m_isgroup=TRUE;
}
void CPage1::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPage1)
DDX_Control(pDX, IDC_TREE1, m_tree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPage1, CDialog)
//{{AFX_MSG_MAP(CPage1)
ON_NOTIFY(NM_RCLICK, IDC_TREE1, OnRclickTree)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree)
ON_NOTIFY(TVN_KEYDOWN, IDC_TREE1, OnKeydownTree)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPage1 message handlers
BOOL CPage1::OnInitDialog()
{
CDialog::OnInitDialog();
m_imaglist.Create(31, 31, ILC_COLOR24, 100, 100);
m_imaglist.SetBkColor(RGB(255,255,255));
m_tree.SetItemHeight(34);
m_tree.SetImageList(&m_imaglist, TVSIL_NORMAL);
m_menu.LoadMenu(IDR_PAGE1MENU);
ASSERT(m_menu);
CoInitialize(NULL); //初始化COM
m_connect.CreateInstance(_T("ADODB.Connection"));
m_recordset.CreateInstance(_T("ADODB.Recordset"));
try
{
m_connect->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb", "", "", adModeUnknown);
}
catch(...)
{
int errorcount;
CString errorinfo;
errorcount = m_connect->GetErrors()->GetCount();
for(int i=0; i< errorcount; i++)
{
errorinfo=(char*)m_connect->GetErrors()->GetItem((short)i)->GetDescription();
MessageBox(errorinfo);
}
}//catch
CString sqlstr,cstr, parentid, id, name, prename, path,cstr2;
int prelen, curlen, index;
HTREEITEM hparent, hpreitem, hcuritem, hroot;
BOOL isgroup;
HICON hico;
COleVariant var;
SHFILEINFO sfi;
prelen=0;
curlen=0;
sqlstr="select* from filetable order by id ASC";
m_recordset.CreateInstance(_T("ADODB.Recordset"));
HRESULT br=m_recordset->Open(sqlstr.GetBuffer(sqlstr.GetLength()), m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(br))
{
m_head=new node;
m_head->left=NULL;
m_head->right=NULL;
m_current=m_head;
while(!(m_recordset->adoEOF))
{
var=m_recordset->GetCollect("parentid");
parentid=var.bstrVal;
var=m_recordset->GetCollect("id");
id=var.bstrVal;
var=m_recordset->GetCollect("name");
name=var.bstrVal;
var=m_recordset->GetCollect("path");
path=var.bstrVal;
var=m_recordset->GetCollect("isgroup");
isgroup=var.boolVal;
SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON);
hico=sfi.hIcon;
index=m_imaglist.Add(hico);
curlen=id.GetLength();
if(parentid=="Null")
{
if(isgroup)
{
cstr=name+" "+"\r\n"+"0 Item";
hroot=m_tree.InsertItem(cstr, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hroot, index, index);
hparent=hroot;
hcuritem=hparent;
}
else
{
cstr=name+" "+"\r\n"+path;
hroot=m_tree.InsertItem(cstr, TVI_ROOT, TVI_LAST);
m_tree.SetItemImage(hroot, index, index);
}
}
else //parentid!=NULL
{
if(curlen>prelen)
{
if(isgroup)
{
cstr=name+" "+"\r\n"+" 0 Item";
}
else
cstr=name+" "+"\r\n"+path;
hparent=hpreitem;//前一个结点是父结点
hcuritem=m_tree.InsertItem(cstr, hparent, TVI_LAST);
m_tree.SetItemImage(hcuritem, index, index);
m_temp=new node;
m_temp->childcount=0;
m_temp->childcount++;
m_temp->name=prename;
m_temp->right=NULL;
m_current->right=m_temp;
m_temp->left=m_current;
m_current=m_temp;
//更新父结点text
cstr.Format("%d Item", m_current->childcount);
cstr=m_current->name+" "+"\r\n"+cstr;
m_tree.SetItemText(hparent, cstr);
}
else if(curlen==prelen)
{
if(isgroup)
{
cstr=name+" "+"\r\n"+" 0 Item";
}
else
{
// cstr.Empty();
cstr=name+" "+"\r\n"+path;
}
hcuritem=m_tree.InsertItem(cstr, hparent, TVI_LAST);
m_tree.SetItemImage(hcuritem, index, index);
m_current->childcount++;
//更新父结点text
cstr.Format("%d Items", m_current->childcount);
cstr=m_current->name+" "+"\r\n"+cstr;
m_tree.SetItemText(hparent, cstr);
}
else // curlen<prelen
{
//寻找父结点
m_temp=m_current;
hcuritem=m_tree.GetParentItem(hcuritem); //
while(hcuritem!=NULL)
{
prelen-=3;
if(curlen==prelen)
{
hparent=m_tree.GetParentItem(hcuritem);
m_temp=m_temp->left;
break;
}
hcuritem=m_tree.GetParentItem(hcuritem);
m_current=m_temp;
m_temp=m_temp->left;
delete m_current;
}//while
if(isgroup)
cstr=name+" "+"\r\n"+" 0 Item";
else
cstr=name+" "+"\r\n"+path;
hcuritem=m_tree.InsertItem(cstr, hparent, TVI_LAST);
m_tree.SetItemImage(hcuritem, index, index);
//更新父结点text....
cstr.Format("%d Items", m_current->childcount);
cstr=m_temp->name+" "+"\r\n"+cstr;
m_tree.SetItemText(hparent, cstr);
m_current=m_temp;
m_current->childcount++;
}//else {curlen<prelen}
}//else
prename=name;
prelen=curlen;
hpreitem=hcuritem;
m_recordset->MoveNext();
}//while
delete m_head;
}//if (SUCCEEDED(br))
return TRUE;
}
HRESULT CPage1::WindowProc(UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wparam))
{
case ID_PAGE1MENURUN:
MenuRun();
break;
case ID_PAGE1MENUOPEN:
MemuOpenContainingFloder();
break;
case ID_PAGE1MENUADDFILE:
MenuAddFile();
break;
case ID_PAGE1MENUADDFLODER:
break;
case ID_PAGE1MENUADDGROUP:
MenuAddGroup();
break;
case ID_PAGE1MENUREMOVE:
MenuRemove();
break;
case ID_PAGE1MENUPROPERTY:
break;
}
}
return(CDialog::WindowProc(message, wparam, lparam));
}
void CPage1::MenuRun()
{
ShellExecute(NULL, _T("open"), szFilePath, NULL, NULL, SW_SHOW);
}
void CPage1::MemuOpenContainingFloder()
{
CString FilePath,cstr;
int pos;
pos=szFilePath.GetLength()-1;
cstr="\\";
while(pos>0)
{
if(szFilePath.GetAt(pos)==cstr)
{
FilePath=szFilePath.Left(pos);
break;
}
pos--;
}
ShellExecute(NULL, _T("open"), FilePath, NULL, NULL, SW_SHOW);
}
void CPage1::MenuAddFile()
{
CFileDialog FileDlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, _T("executable file(*.exe;*.scr;*.com;*.bat)|*.exe;*.scr;*.com;*.bat"),NULL);
if(FileDlg.DoModal()!=IDOK)
return;
szFileName=FileDlg.GetFileTitle();
szFilePath=FileDlg.GetPathName();
if(szFileName!=""||szFilePath!="")
{
m_FileProperty.m_name=szFileName;
m_FileProperty.m_path=szFilePath;
if(m_FileProperty.DoModal()==-1)
{
TCHAR szerror[50];
wsprintf(szerror, _T("error! error code: %d"), GetLastError());
MessageBox(szerror);
}
}
}
void CPage1::MenuAddFloder()
{
m_FileProperty.DoModal();
}
void CPage1::MenuAddGroup()
{
m_GroupProperty.DoModal();//打开组属性对话框
}
void CPage1::MenuRemove()
{
CString sqlstr, parentid, id, selitemid;
COleVariant var, parentvar, newvar;
BOOL delid=FALSE;
HTREEITEM hparent, hcuritem, hnextitem, hdelitem, hselitem, hnextsibling, hcursibling, hselitemparent;
IdNode *head,*temp,*current;
if(m_htreeitem!=NULL)
{
head=new IdNode;
head->next=NULL;
current=head;
GetSelectedItemtID(m_htreeitem);
selitemid=m_selecteditemid; //保存选中结点的ID
hselitemparent=m_tree.GetParentItem(m_htreeitem);
hnextsibling=m_tree.GetNextItem(m_htreeitem, TVGN_NEXT);
hnextitem=hnextsibling;
while(hnextsibling!=NULL) //保存将会被影响的结点ID
{
while(hnextitem!=NULL)
{
hcuritem=hnextitem;
GetSelectedItemtID(hcuritem);//计算当前结点的ID
temp=new IdNode;
temp->oldid=m_selecteditemid;
temp->parentid=parentid;
temp->next=NULL;
current->next=temp;
current=temp;
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_CHILD);
}
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_NEXT);
if(hnextitem==NULL)//如果不存在兄弟结点
{
hparent=m_tree.GetParentItem(hcuritem);
hcuritem=hparent;
if(hselitemparent==hparent)
break;
hnextitem=m_tree.GetNextItem(hparent, TVGN_NEXT);
}
}//while
/*删除指定结点*/
hselitem=m_htreeitem;
hnextitem=hselitem;
while(1)
{
while(hnextitem!=NULL)
{
hcuritem=hnextitem;
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_CHILD);
}
hdelitem=hcuritem;
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_NEXT);
if(hnextitem==NULL)//如果不存在兄弟结点
{
hparent=m_tree.GetParentItem(hcuritem);
hnextitem=hparent;
}
//删除结点
m_tree.DeleteItem(hdelitem);
if(hselitem==hcuritem)
break;
}//while
current=head->next;
hnextitem=hnextsibling;
while(hnextsibling!=NULL) //保存已改变的ID值
{
while(hnextitem!=NULL)
{
hcuritem=hnextitem;
GetSelectedItemtID(hcuritem);//计算当前结点的ID
current->newid=m_selecteditemid;
if(m_selecteditemid.GetLength()>3)
{
id=m_selecteditemid.Left(m_selecteditemid.GetLength()-3);
current->parentid=id;
}
else
{
id="Null";
current->parentid=id;
}
current=current->next;
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_CHILD);
}
hnextitem=m_tree.GetNextItem(hcuritem, TVGN_NEXT);
if(hnextitem==NULL)//如果不存在兄弟结点
{
hparent=m_tree.GetParentItem(hcuritem);
hcuritem=hparent;
if(hselitemparent==hparent)
break;
hnextitem=m_tree.GetNextItem(hparent, TVGN_NEXT);
}
}//while
sqlstr="select* from filetable order by id ASC";
m_recordset.CreateInstance(_T("ADODB.Recordset"));
HRESULT br=m_recordset->Open(sqlstr.GetBuffer(sqlstr.GetLength()), m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(br))
{
while(!(m_recordset->adoEOF))
{
var=m_recordset->GetCollect("id");
if(var.vt!=VT_NULL)
{
parentid=var.bstrVal;
if(parentid.GetLength()>=selitemid.GetLength())
{
parentid=parentid.Left(selitemid.GetLength());
if(lstrcmp(parentid, selitemid)==0)
{
m_recordset->Delete(adAffectCurrent);
}
}
}//(var.vt!=VT_NULL)
m_recordset->MoveNext();
}//while
}//if
current=head->next;
sqlstr="select* from filetable order by id ASC";
m_recordset.CreateInstance(_T("ADODB.Recordset"));
br=m_recordset->Open(sqlstr.GetBuffer(sqlstr.GetLength()), m_connect.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(br))
{
while(!(m_recordset->adoEOF))
{
var=m_recordset->GetCollect("id");
if(hnextsibling!=NULL)
{
if(var.vt!=VT_NULL)
{
id=var.bstrVal;
if(lstrcmp(id, current->oldid)==0)
{
newvar=current->newid;
parentvar=current->parentid;
m_recordset->Update(_variant_t("id"), newvar);//修改ID
m_recordset->Update(_variant_t("parentid"), parentvar);
// temp=current;
if(current->next!=NULL)
current=current->next;
// delete temp;
}//if
}//if(var.vt!=VT_NULL)
}//if(hnextsibling!=NULL)
m_recordset->MoveNext();
}//while
}//if(SUCCEEDED(br))
}//if
}
void CPage1::MenuShowProperty()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -