Advertisement
Guest User

delphi S.FATEH

a guest
Jan 4th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.71 KB | None | 0 0
  1. a form3 contain button and virtualtreeview
  2.  
  3. unit Unit3;
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  9.   Dialogs, ImgList, StdCtrls, VirtualTrees, Contnrs;
  10.  
  11. type
  12.   TForm3 = class(TForm)
  13.     VirtualStringTree1: TVirtualStringTree;
  14.     Button1: TButton;
  15.     ImageList1: TImageList;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure VirtualStringTree1GetNodeDataSize(Sender: TBaseVirtualTree;
  20.       var NodeDataSize: Integer);
  21.     procedure VirtualStringTree1GetImageIndex(Sender: TBaseVirtualTree;
  22.       Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  23.       var Ghosted: Boolean; var ImageIndex: Integer);
  24.     procedure VirtualStringTree1GetText(Sender: TBaseVirtualTree;
  25.       Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  26.       var CellText: string);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32.  
  33.   TFile = class(TObject)
  34.   private
  35.     FFullPath: string;
  36.     FFileName: string;
  37.     FIconIndex: Integer;
  38.     FFileIcon : hIcon;
  39.   public
  40.     constructor Create(Path: string);
  41.     property FullPath: string read FFullPath write FFullPath;
  42.     property FileName: string read FFileName write FFileName;
  43.     property IconIndex: Integer read FIconIndex write FIconIndex;
  44.     property FileIcon: hIcon read FFileIcon write FFileIcon;
  45.   end;
  46.  
  47.   TFiles = class(TObjectList)
  48.   private
  49.     function SameIcon(HIcon1, HIcon2: HIcon): Boolean;
  50.     function StreamsAreIdentical(Stream1, Stream2: TStream): Boolean;
  51.   public
  52.     function Add(Path: string): TFile; reintroduce;
  53.   end;
  54.  
  55.   PFileNode =^TFileNode;
  56.   TFileNode = record
  57.     FileObj: TFile;
  58.   end;
  59.  
  60. var
  61.   Form3: TForm3;
  62.   Files: TFiles;
  63.  
  64. implementation
  65.  
  66. uses ShellApi, CommCtrl;
  67.  
  68. {$R *.dfm}
  69.  
  70. procedure TForm3.Button1Click(Sender: TObject);
  71. var
  72.   i: Integer;
  73.   OpenDialog: TOpenDialog;
  74.   FileRec: PFileNode;
  75. begin
  76.   OpenDialog := TOpenDialog.Create(Self);
  77.   OpenDialog.Options := [ofAllowMultiSelect];
  78.   if not(OpenDialog.Execute) then Exit;
  79.  
  80.   for I := 0 to OpenDialog.Files.Count - 1 do
  81.   begin
  82.     FileRec := VirtualStringTree1.GetNodeData(VirtualStringTree1.AddChild(nil));
  83.     FileRec.FileObj :=  Files.Add(OpenDialog.Files[i]);
  84.   end;
  85. end;
  86.  
  87. { TFile }
  88.  
  89. constructor TFile.Create(Path: string);
  90. var
  91.   FileInfo: SHFILEINFO;
  92. begin
  93.   FullPath := Path;
  94.   SHGetFileInfo(PChar(FullPath), FILE_ATTRIBUTE_NORMAL, FileInfo, SizeOf(FileInfo), SHGFI_DISPLAYNAME
  95.       or SHGFI_TYPENAME
  96.       or SHGFI_ICON
  97.       or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);
  98.   FileName := ExtractFileName(FullPath);
  99.   FileIcon := FileInfo.hIcon;
  100. end;
  101.  
  102. { TFiles }
  103.  
  104. function TFiles.Add(Path: string): TFile;
  105. var
  106.   i: Integer;
  107.   TmpFile: TFile;
  108. begin
  109.   TmpFile := TFile.Create(Path);
  110.   TmpFile.IconIndex := ImageList_AddIcon(Form3.ImageList1.Handle, TmpFile.FileIcon);
  111.   Result := TmpFile;
  112.   inherited Add(Result);
  113.  
  114.  
  115.   //here where i want to check for duplicates
  116.  { for I := 0 to Count - 1 do
  117.   begin
  118.     if SameIcon(TmpFile.FileIcon, TFile(Items[i]).FileIcon) then
  119.       TmpFile.IconIndex := TFile(Items[i]).IconIndex else
  120.       // if the icon found in Items[i] don't add the icon to imagelsist
  121.        // dont add new icon to imagelist just get the iconindex of the existing icon
  122.       TmpFile.IconIndex := ImageList_AddIcon(Form3.ImageList1.Handle, TmpFile.FileIcon);
  123.       //else add the new icon
  124.      Result := TmpFile;
  125.      inherited Add(Result);
  126.   end;}
  127.  
  128. end;
  129.  
  130. function TFiles.SameIcon(HIcon1, HIcon2: HIcon): Boolean;
  131. var
  132.   Icon1, Icon2: TIcon;
  133.   MS1, MS2: TMemoryStream;
  134. begin
  135.     Icon1 := TIcon.Create;
  136.     Icon1.Handle := HIcon1;
  137.     Icon2 := TIcon.Create;
  138.     Icon2.Handle := HIcon2;
  139.     MS1 := TMemoryStream.Create;
  140.     MS2 := TMemoryStream.Create;
  141.   try
  142.     Icon1.SaveToStream(MS1);
  143.     Icon2.SaveToStream(MS2);
  144.     Result := StreamsAreIdentical(MS1, MS2);
  145.   finally
  146.     MS1.Free;
  147.     MS2.Free;
  148.     Icon1.Free;
  149.     Icon2.Free;
  150.   end;
  151. end;
  152.  
  153. function TFiles.StreamsAreIdentical(Stream1, Stream2: TStream): Boolean;
  154. const
  155.   Block_Size = 4096;
  156. var
  157.   Buffer_1: array[0..Block_Size-1] of byte;
  158.   Buffer_2: array[0..Block_Size-1] of byte;
  159.   Buffer_Length: integer;
  160. begin
  161.   Result := False;
  162.   if Stream1.Size <> Stream2.Size then exit;
  163.   Stream1.Position := 0;
  164.   Stream2.Position := 0;
  165.   while Stream1.Position < Stream1.Size do
  166.   begin
  167.     Buffer_Length := Stream1.Read(Buffer_1, Block_Size);
  168.     Stream2.Read(Buffer_2, Block_Size);
  169.     if not CompareMem(@Buffer_1, @Buffer_2, Buffer_Length) then exit;
  170.   end;
  171.   Result := True;
  172. end;
  173.  
  174. procedure TForm3.FormCreate(Sender: TObject);
  175. begin
  176.   Files := TFiles.Create;
  177. end;
  178.  
  179. procedure TForm3.FormDestroy(Sender: TObject);
  180. begin
  181.   Files.Free;
  182. end;
  183.  
  184. procedure TForm3.VirtualStringTree1GetImageIndex(Sender: TBaseVirtualTree;
  185.   Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  186.   var Ghosted: Boolean; var ImageIndex: Integer);
  187. var
  188.   FileRec: PFileNode;
  189. begin
  190.   FileRec := Sender.GetNodeData(Node);
  191.   if Kind in [ikNormal , ikSelected] then
  192.   begin
  193.     if Column = 1 then ImageIndex := FileRec.FileObj.IconIndex;
  194.   end;
  195. end;
  196.  
  197. procedure TForm3.VirtualStringTree1GetNodeDataSize(Sender: TBaseVirtualTree;
  198.   var NodeDataSize: Integer);
  199. begin
  200.   NodeDataSize := SizeOf(TFileNode);
  201. end;
  202.  
  203. procedure TForm3.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
  204.   Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  205.   var CellText: string);
  206. var
  207.   FileRec: PFileNode;
  208. begin
  209.   FileRec := Sender.GetNodeData(Node);
  210.   case Column of
  211.     0: CellText := FileRec.FileObj.FullPath;
  212.     1: CellText := FileRec.FileObj.FileName;
  213.   end;
  214. end;
  215.  
  216. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement