Tags:
delphi
Hey er der nogle der kan fortælle mig hvordan man kan få fil navne fra en mappe ind i fx. en PopupMenu og så når man klikker på navnet i popupmenuen så starter den fx. genvejen eller filen der stod der......
John det er mig...!!!
Tror jeg vist nok..... :-)
2 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 3 karma
Heyyyyy John
Jeps, jeg har lige strikket noget kode sammen til dig.
Lav et nyt projekt med en TButton, og indsæt denne kode.
--------- Im of to fight evil.............................................
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus,
ShellApi; // <<<--------- HUSK DENNE HER !!!!!!!!!
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FileList : TStringList;
Menu : TPopupmenu;
procedure GetFileList(Dir : String);
procedure OnPopClick(Sender : TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.GetFileList(Dir : String);
var Rec : TSearchRec;
Error : integer;
begin
if not assigned(FileList) then
FileList := TStringlist.Create
else Filelist.Clear;
try
Error := FindFirst(Dir + '*.*', faAnyFile, Rec);
while (Error = 0) do begin
if (Rec.Attr <> faDirectory) then FileList.Add(Rec.Name);
Error := FindNext(Rec);
end;
finally
FindClose(Rec);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
item : TMenuItem;
begin
// Find filerne
GetFileList('c:\\Dokumenter\\'); // Husk den sidste "\\"
// Lav en ny menu
if assigned(Menu) then Menu.Free;
Menu := TPopupmenu.Create(Form1);
Menu.Name := 'PopupMenu1';
// Føj dem til popup menuen
if FileList.Count > 0 then
for i := 0 to FileList.Count - 1 do begin
Item := TMenuItem.Create(Menu);
Item.Caption := Filelist.Strings;
// Set menupunktets OnClick event (er den samme for alle menu punkter)
Item.OnClick := OnPopClick;
Menu.Items.Add(Item);
end;
// Vis popup'en
Menu.Popup(Left + 20, Top + 30);
end;
procedure TForm1.OnPopClick(Sender : TObject);
begin
// Kald filen der blev klikket på
with Sender as TMenuItem do
ShellExecute(Handle, PCHAR('open'), PCHAR(Caption), PCHAR(''), PCHAR('c:\\Dokumenter\\'), SW_NORMAL);
{ Klippet fra hjælpen ----------------------------------
HINSTANCE ShellExecute(
HWND hwnd, // handle to parent window
LPCTSTR lpOperation, // pointer to string that specifies operation to perform
LPCTSTR lpFile, // pointer to filename or folder name string
LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory, // pointer to string that specifies default directory
INT nShowCmd // whether file is shown when opened
);
}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Memory leaks - nej tak
if assigned(FileList) then FileList.Free;
if assigned(Menu) then Menu.Free;
end;
end.
0
Hmm jeg må lige rette dig i noget men eller så tak for svar........
Rettelserne er makeret med ------------------- bagefter
--------- Im of to fight evil.............................................
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus,
ShellApi; // <<<--------- HUSK DENNE HER !!!!!!!!!
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FileList : TStringList;
Menu : TPopupmenu;
procedure GetFileList(Dir : String);
procedure OnPopClick(Sender : TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.GetFileList(Dir : String);
var Rec : TSearchRec;
Error : integer;
begin
if not assigned(FileList) then
FileList := TStringlist.Create
else Filelist.Clear;
try
Error := FindFirst(Dir + '*.*', faAnyFile, Rec);
while (Error = 0) do begin
if (Rec.Attr <> faDirectory) then FileList.Add(Rec.Name);
Error := FindNext(Rec);
end;
finally
FindClose(Rec);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
item : TMenuItem;
begin
// Find filerne
GetFileList('c:\\Dokumenter\\'); // Husk den sidste "\\"
// Lav en ny menu
if assigned(Menu) then Menu.Free;
Menu := TPopupmenu.Create(Form1);
Menu.Name := 'PopupMenu1';
// Føj dem til popup menuen
if FileList.Count > 0 then
for i := 0 to FileList.Count - 1 do begin
Item := TMenuItem.Create(Menu);
Item.Caption := Filelist.Strings;
// Set menupunktets OnClick event (er den samme for alle menu punkter)
Item.OnClick := OnPopClick;
Menu.Items.Add(Item);
end;
// Vis popup'en
Menu.Popup(Left + 20, Top + 30);
end;
procedure TForm1.OnPopClick(Sender : TObject);
var )------------------
noget : String; )------------------
begin
// Kald filen der blev klikket på
noget := Caption; )---------------------------
Delete(noget, 1,1); )--------------------------- Dette er fordi forand hvert Filnavn der er i
)------------- PopupMenuen tegnet & og det skal man lige ha slettet eller kan man ikke execute filen..
with Sender as TMenuItem do
ShellExecute(Handle, PCHAR('open'), PCHAR(Caption), PCHAR(''), PCHAR('c:\\Dokumenter\\'), SW_NORMAL);
{ Klippet fra hjælpen ----------------------------------
HINSTANCE ShellExecute(
HWND hwnd, // handle to parent window
LPCTSTR lpOperation, // pointer to string that specifies operation to perform
LPCTSTR lpFile, // pointer to filename or folder name string
LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory, // pointer to string that specifies default directory
INT nShowCmd // whether file is shown when opened
);
}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Memory leaks - nej tak
if assigned(FileList) then FileList.Free;
if assigned(Menu) then Menu.Free;
end;
end.
--------------------------------------------------------
Men tak skal du sku ha der du får lige 25 UP mere for svar