.cpp
// --------------------------------------------------------------------------- #define NO_WIN32_LEAN_AND_MEAN #include <vcl.h> #pragma hdrstop #include <shlobj.h> #include <shellapi.h> #include <stdio.h> #include <FileCtrl.hpp> #include <ComCtrls.hpp> #include "Unit1.h" // --------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; TImageList *List; LPMALLOC mem; LPMALLOC pShellMalloc; // para SHGetMalloc // Estructura para TDir typedef struct { LPSHELLFOLDER ParentShellFolder; LPITEMIDLIST pidl; LPITEMIDLIST ppidl; bool Sfogliato; }TDir; // variables chkbox TTreeNode *ClickNode; bool NodeChecked; // declaracion de la funcion chkbox void SetChildChecked(TTreeNode * ANode, bool yn); // --------------------------------------------------------------------------- __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); SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &net); desk->GetDisplayNameOf(net, SHGDN_NORMAL, &nome); GetNome(&nome, net, scom); SHGetFileInfo((LPCSTR)net, 0, &fi, sizeof(SHFILEINFO), SHGFI_PIDL | SHGFI_SYSICONINDEX | 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); SetWindowLong(TreeView->Handle,GWL_STYLE, GetWindowLong(TreeView->Handle,GWL_STYLE) | TVS_CHECKBOXES); SetWindowLong(TreeView->Handle,GWL_STYLE, GetWindowLong(TreeView->Handle,GWL_STYLE) | TVS_CHECKBOXES); // // chkbox // SetWindowLong(TreeView->Handle, GWL_STYLE, GetWindowLong(TreeView->Handle, // GWL_STYLE) | TVS_CHECKBOXES); } // --------------------------------------------------------------------------- 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; } // ------------------ 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 //originalmente decia TreeView->Items->AddChild(c, "PP"); if (fi.dwAttributes & SFGAO_HASSUBFOLDER) TreeView->Items->AddChild(c, ""); d = new TDir; d->ParentShellFolder = ShellFolder; d->pidl = idfiglio; d->ppidl = lpNew; d->Sfogliato = false; c->Data = d; } enumlist->Release(); } } } } // ----------- 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; } //ok para mostar checbox SetChildChecked(ClickNode, NodeChecked); } // --------------------------------------------------------------------------- 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; } } // --------------------------------------------------------------------------- // funcioneschkbox bool GetNodeChecked(TTreeNode *ANode) { TVITEM tvItem; tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = ANode->ItemId; tvItem.stateMask = TVIS_STATEIMAGEMASK; TreeView_GetItem(ANode->TreeView->Handle, &tvItem); return((BOOL)(tvItem.state >> 12) - 1); } // ----------- void SetNodeChecked(TTreeNode *ANode, bool IsSetToChecked) { TVITEM tvItem; tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = ANode->ItemId; tvItem.stateMask = TVIS_STATEIMAGEMASK; tvItem.state = INDEXTOSTATEIMAGEMASK(IsSetToChecked ? 2 : 1); TreeView_SetItem(ANode->TreeView->Handle, &tvItem); } // -------------- void SetChildChecked(TTreeNode *ANode, bool yn) { int count = ANode->Count; SetNodeChecked(ANode, yn); if (count != 0) for (int i = 0; i < count; i++) SetChildChecked(ANode->Item[i], yn); } // ---------- void __fastcall TForm1::TreeViewMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (ClickNode != NULL) if (GetNodeChecked(ClickNode) != NodeChecked) { NodeChecked = !NodeChecked; SetChildChecked(ClickNode, NodeChecked); } } // --------------------------------------------------------------------------- void __fastcall TForm1::TreeViewMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { ClickNode = TreeView->GetNodeAt(X, Y); if (ClickNode != NULL) NodeChecked = GetNodeChecked(ClickNode); } // --------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Memo1->Clear(); for (int i=0 ; i<TreeView->Items->Count; i++) if (GetNodeChecked(TreeView->Items->Item[i])&& !TreeView->Items->Item[i]->Text.IsEmpty()){ Memo1->Lines->Add(TreeView->Items->Item[i]->Text); } } //---------------------------------------------------------------------------
.h
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ComCtrls.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TLabel *Label1; TTreeView *TreeView; TMemo *Memo1; TButton *Button1; void __fastcall TreeViewExpanding(TObject *Sender, TTreeNode *Node, bool &AllowExpansion); void __fastcall TreeViewClick(TObject *Sender); void __fastcall TreeViewMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y); void __fastcall TreeViewMouseMove(TObject *Sender, TShiftState Shift, int X, int Y); void __fastcall Button1Click(TObject *Sender); private: // User declarations void __fastcall GetNome(LPSTRRET nome, LPITEMIDLIST id, char *display); void __fastcall BrowseFolder(LPSHELLFOLDER padre, LPITEMIDLIST pidl, LPITEMIDLIST ppidl, TTreeNode *nodo); LPITEMIDLIST __fastcall MergeIDList(LPITEMIDLIST a, LPITEMIDLIST b); int __fastcall GetPIDLSize(LPITEMIDLIST lpidl); LPITEMIDLIST __fastcall GetNextItem(LPITEMIDLIST pidl); public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif