|
Вопрос # 3 495/ вопрос открыт / |
|
Доброго времени суток, уважаемые эксперты!
Как получить файлs в TreeView как в проводнике? Чтобы в TreeView двойным щелчком я мог открыть файлы и любой из файлов переместить в listbox илив listView? Помагите пожалуйста весь интернет облазил ниче подобного не могу найти? Заранее большое большое спасибо!
 |
Вопрос задал: Kraken (статус: Посетитель)
Вопрос отправлен: 5 декабря 2009, 20:40
Состояние вопроса: открыт, ответов: 1.
|
Ответ #1. Отвечает эксперт: Жикльор
Здравствуйте, Kraken!
Наконец-то я закончил написание программы!!!!! Она ищет по всем жестком диске любые файлы и отображает их в иерархической структуре в TreeView. При перетягивании с TreeView в ListBox, получаем полный путь к файлу. По двойном щелчку по файлу в ListBox вы его открываете, правда только exe. Вот собственно и сам код:
З.Ы. В этом коде все файлы отображаются как папки, брал со стандартной библиотеки Дельфи иконки.
Приложение: Переключить в обычный режим- unit Unit1;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ComCtrls, StdCtrls;
-
- type
- TForm1 = class(TForm)
- TreeView1: TTreeView;
- ListBox1: TListBox;
- Label1: TLabel;
- Label2: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
- var AllowExpansion: Boolean);
- procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
- procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure ListBox1DblClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
- Count: integer;
-
- implementation
-
- {$R *.dfm}
- {$R FileCtrl}
-
- procedure NextLevel(ParentNode: TTreeNode);
- function DirectoryName(name: string): boolean;
- begin
- result := (name <> '.') and (name <> '..');
- end;
- var
- sr, srChild: TSearchRec;
- node: TTreeNode;
- path: string;
- begin
- node := ParentNode;
- path := '';
- repeat
- path := node.Text + '\' + path;
- node := node.Parent;
- until node = nil;
- if FindFirst(path + '*.*', faAnyFile, sr) = 0 then begin
- repeat
- if (sr.Attr and faAnyFile <> 0) and DirectoryName(sr.Name) then begin
- node := Form1.TreeView1.Items.AddChild(ParentNode, sr.Name);
- node.ImageIndex := 0;
- node.SelectedIndex := 1;
- node.HasChildren := false;
- if FindFirst(path + sr.Name + '\*.*', faAnyFile, srChild) = 0 then begin
- repeat
- if (srChild.Attr and faAnyFile <> 0) and DirectoryName(srChild.Name)
- then node.HasChildren := true;
- until (FindNext(srChild) <> 0) or node.HasChildren;
- end;
- FindClose(srChild);
- end;
- until FindNext(sr) <> 0;
- end else ParentNode.HasChildren := false;
- FindClose(sr);
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- const
- IconNames: array [0..6] of string = ('CLOSEDFOLDER', 'OPENFOLDER',
- 'FLOPPY', 'HARD', 'NETWORK', 'CDROM', 'RAM');
- var
- c: char;
- s: string;
- node: TTreeNode;
- DriveType: integer;
- bm, mask: TBitmap;
- i: integer;
- begin
- TreeView1.Items.BeginUpdate;
- TreeView1.Images := TImageList.CreateSize(16, 16);
- bm := TBitmap.Create;
- mask := TBitmap.Create;
- for i := low(IconNames) to high(IconNames) do begin
- bm.Handle := LoadBitmap(HInstance, PChar(IconNames[i]));
- bm.Width := 16;
- bm.Height := 16;
- mask.Assign(bm);
- mask.Mask(clBlue);
- TreeView1.Images.Add(bm, mask);
- end;
- for c := 'A' to 'Z' do begin
- s := c + ':';
- DriveType := GetDriveType(PChar(s));
- if DriveType = 1 then continue;
- node := Form1.TreeView1.Items.AddChild(nil, s);
- case DriveType of
- DRIVE_REMOVABLE: node.ImageIndex := 2;
- DRIVE_FIXED: node.ImageIndex := 3;
- DRIVE_REMOTE: node.ImageIndex := 4;
- DRIVE_CDROM: node.ImageIndex := 5;
- else node.ImageIndex := 6;
- end;
- node.SelectedIndex := node.ImageIndex;
- node.HasChildren := true;
- end;
- TreeView1.Items.EndUpdate;
- end;
-
- procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
- var AllowExpansion: Boolean);
- begin
- TreeView1.Items.BeginUpdate;
- node.DeleteChildren;
- NextLevel(node);
- TreeView1.Items.EndUpdate;
- end;
-
- procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
- var
- name: string;
- nod: TTreeNode;
- begin
- name := Form1.TreeView1.Selected.Text;
- nod := Form1.TreeView1.Selected;
- while true do
- begin
- if nod.Parent <> nil then
- begin
- name:=nod.Parent.Text + '/' + name;
- nod:=nod.Parent;
- end
- else
- break;
- end;
- Form1.ListBox1.Items.Add(name);
- end;
-
- procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- begin
- if Sender is TTreeView then Accept:=true;
- end;
-
- procedure TForm1.ListBox1DblClick(Sender: TObject);
- begin
- WinExec(PChar(Form1.ListBox1.Items[Form1.ListBox1.ItemIndex]),0);
- end;
-
- end.
 |
Ответ отправил: Жикльор (статус: 5-ый класс)
Время отправки: 6 декабря 2009, 02:51
Оценка за ответ: 5
|
Мини-форум вопроса
Всего сообщений: 21; последнее сообщение — 13 декабря 2009, 20:41; участников в обсуждении: 3.
Страницы: [« Предыдущая] [1] [2]
|
Kraken (статус: Посетитель), 13 декабря 2009, 20:41 [#21]:
ясно!
|
Страницы: [« Предыдущая] [1] [2]
Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте.
|