type
TDemoForm =
class(TForm)
ListBox: TListBox;
AddButton: TButton;
OpenDialog: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure AddButtonClick(Sender: TObject);
procedure ListBoxDblClick(Sender: TObject);
private
Pathes: TStrings;
end;
var
DemoForm: TDemoForm;
implementation
{$R *.dfm}
uses
ShellApi;
function AddObject(strings: TStrings;
const s:
string; obj: Pointer =
nil): Integer;
begin
Result := strings.IndexOf(s);
if Result < 0
then
begin
strings.BeginUpdate;
Result := strings.AddObject(s, obj);
strings.EndUpdate;
end;
end;
procedure TDemoForm.FormCreate(Sender: TObject);
begin
Pathes := TStringList.Create;
end;
procedure TDemoForm.FormDestroy(Sender: TObject);
begin
Pathes.Free;
end;
procedure TDemoForm.AddButtonClick(Sender: TObject);
var
fName, fPath: TFileName;
begin
with OpenDialog
do
if Execute
then
begin
fPath := ExtractFilePath(FileName);
fName := ExtractFileName(FileName);
ListBox.ItemIndex := AddObject(ListBox.Items, fName, Pointer(AddObject(Pathes, fPath)));
end;
end;
procedure TDemoForm.ListBoxDblClick(Sender: TObject);
var
h: THandle;
fn: TFileName;
begin
with ListBox
do
if ItemIndex >= 0
then
begin
fn := pathes[Integer(Items.Objects[ItemIndex])] + Items[ItemIndex];
h := ShellExecute(
Handle, '
open', PChar(fn),
nil,
nil, SW_SHOWNORMAL);
if h <= 32
then
ShowMessage(SysErrorMessage(h));
end;
end;