程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 網上鄰居目錄樹

網上鄰居目錄樹

編輯:關於C++

//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//#include <shlobj.h>
#include <shellapi.h>
#include <stdio.h>
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TImageList *List;
LPMALLOC mem;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
LPSHELLFOLDER desk;
LPITEMIDLIST net;
SHFILEINFO fi;
STRRET nome;
char scom[255];
TTreeNode *n;
TDir * d;
List = new TImageList(this);
List->ShareImages = true;
List->Handle =
SHGetFileInfo("",0,&fi,sizeof(SHFILEINFO),SHGFI_SMALLICON|SHGFI_SYSICONINDEX
);
TreeView->Images = List;
SHGetMalloc(&mem);
SHGetDesktopFolder(&desk);
SHGetSpecialFolderLocation(NULL,CSIDL_NETWORK ,&net);
desk->GetDisplayNameOf(net,SHGDN_NORMAL ,&nome);
GetNome(&nome,net,scom);
SHGetFileInfo((LPCSTR)net,0,&fi,sizeof(SHFILEINFO),SHGFI_PIDL|SHGFI_SYSICONI
NDEX|SHGFI_DISPLAYNAME |SHGFI_ATTRIBUTES|SHGFI_TYPENAME );
n=TreeView->Items->Add(NULL,scom);
n->ImageIndex = fi.iIcon;
n->StateIndex = fi.iIcon;
n->SelectedIndex = fi.iIcon;
d=new TDir;
d->ParentShellFolder = desk;
d->pidl = net;
d->ppidl = net;
d->Sfogliato = true;
n->Data = d;
BrowseFolder(desk,net,net,n);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::BrowseFolder(LPSHELLFOLDER padre, LPITEMIDLIST
pidl,LPITEMIDLIST ppidl,TTreeNode *nodo)
{
TTreeNode *c;
if(nodo != NULL)
{
while(nodo->Count > 0) nodo->Item[0]->Delete();
}
if(padre!= NULL)
{
LPSHELLFOLDER ShellFolder=NULL;
padre->BindToObject(pidl,NULL,IID_IShellFolder ,(void **)&ShellFolder);
if(ShellFolder != NULL)
{
LPENUMIDLIST enumlist=NULL;
ShellFolder->EnumObjects(NULL,SHCONTF_FOLDERS|SHCONTF_INCLUDEHIDDEN
,&enumlist);
if(enumlist != NULL)
{
int i;
LPSTRRET nome ;
LPITEMIDLIST idfiglio;
LPITEMIDLIST pp;
char scom[255];
SHFILEINFO fi;
TDir *d;
int r;
int conta1,conta2;
enumlist->Reset();
while(enumlist->Next(1,&idfiglio,NULL)!=S_FALSE)
{
LPITEMIDLIST lpNew = MergeIDList(ppidl,idfiglio);
if(!idfiglio) break;
if(lpNew== NULL) continue;
nome = (LPSTRRET)mem->Alloc(sizeof (STRRET));
memset(&fi,0,sizeof(SHFILEINFO));
r=SHGetFileInfo((LPCSTR)lpNew,0,&fi,sizeof(fi),SHGFI_PIDL|SHGFI_SYSICONINDEX
|SHGFI_DISPLAYNAME |SHGFI_ATTRIBUTES|SHGFI_TYPENAME|SHGFI_SMALLICON );
ZeroMemory(nome,sizeof(STRRET));
nome->uType = STRRET_CSTR;
ShellFolder->GetDisplayNameOf(idfiglio,SHGDN_NORMAL ,nome);
GetNome(nome,idfiglio,scom);
c=TreeView->Items->AddChild(nodo,scom);
c->ImageIndex = fi.iIcon;
c->StateIndex = fi.iIcon;
c->SelectedIndex = fi.iIcon;
// Cre elelemnto fittizio
if(fi.dwAttributes & SFGAO_HASSUBFOLDER)
TreeView->Items->AddChild(c,"PP");
d = new TDir;
d->ParentShellFolder = ShellFolder;
d->pidl = idfiglio;
d->ppidl = lpNew;
d->Sfogliato = false;
c->Data = d;
}
enumlist->Release();
}
}
}
}
void __fastcall TForm1::GetNome(LPSTRRET nome, LPITEMIDLIST id, char *
display)
{
LPSTR lpsz;
int cch;
switch (nome->uType)
{
case STRRET_WSTR:
cch = WideCharToMultiByte( CP_ACP,
0,
nome->pOleStr,
-1,
NULL,
0,
NULL,
NULL);
lpsz = (LPSTR) mem->Alloc(cch);
if (lpsz != NULL)
{
WideCharToMultiByte( CP_ACP,
0,
nome->pOleStr,
-1,
lpsz,
cch,
NULL,
NULL);
sprintf(display,"%s", lpsz);
mem->Free(lpsz);
}
break;
case STRRET_OFFSET:
sprintf(display,"%s", ((char *) id) + nome->uOffset);
break;
case STRRET_CSTR:
sprintf(display,"%s", nome->cStr);
break;
}
}
LPITEMIDLIST __fastcall TForm1::MergeIDList(LPITEMIDLIST a, LPITEMIDLIST b)
{
int cb1, cb2;
if (a) cb1 = GetPIDLSize(a);
if (cb1) cb1 = cb1 - 2;
if (b) cb2 = GetPIDLSize(b);
if (cb2) cb2 = cb2 - 2;
int total_size = cb1 + cb2 + 2;
LPITEMIDLIST lpidlNew;
if(!a || !b) return NULL;
lpidlNew = (LPITEMIDLIST)mem->Alloc(total_size);
if (lpidlNew)
{
ZeroMemory(lpidlNew, total_size);
CopyMemory(lpidlNew, a, cb1);
CopyMemory(((LPBYTE)lpidlNew) + cb1, b, cb2);
FillMemory(((LPBYTE)lpidlNew) + cb1 + cb2, 2, 0);
return lpidlNew;
}
return NULL;
}
int __fastcall TForm1::GetPIDLSize(LPITEMIDLIST lpidl)
{
unsigned short cb = 0;
while (lpidl)
{
cb = (unsigned short)(cb + lpidl->mkid.cb);
lpidl = GetNextItem(lpidl);
}
return (unsigned short)(cb + 2);
}
LPITEMIDLIST __fastcall TForm1::GetNextItem(LPITEMIDLIST pidl)
{
unsigned short nLen = pidl->mkid.cb;
if (nLen == 0) return NULL;
return (LPITEMIDLIST)((LPBYTE)pidl + nLen);
}
void __fastcall TForm1::TreeViewExpanding(TObject *Sender, TTreeNode *Node,
bool &AllowExpansion)
{
TDir *d;
if(Node != NULL)
{
d = (TDir *)Node->Data;
if(!d->Sfogliato)
{
BrowseFolder(d->ParentShellFolder,d->pidl,d->ppidl,Node);
}
d->Sfogliato = true;
}
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::TreeViewClick(TObject *Sender)
{
TDir *d;
if(TreeView->Selected)
{
char scom[MAX_PATH];
d=(TDir *)(TreeView->Selected->Data);
SHGetPathFromIDList(d->ppidl,scom);
Label1->Caption = scom;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved