Autor Beitrag
delphi-programmierer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 48

DOS, WIN XP
RAD Studio 2010, Turbo Pascal 6.0
BeitragVerfasst: Di 20.02.07 18:24 
Hallo Leute,

habe gerade eine Unit über Dateiopperationen mit TSHFileOpStruct geschrieben (Datei verschieben, kopieren, löschen, umbenennen). Bei der Datei_Umbenennen-Prozedur habe ich zusätzlich ein Dialog zum Eingeben des neuen Dateinamens programmiert (s. Quelltext).
Dieser Dialog besteht unter anderem aus zwei TBitBtn, bei denen ich das OnClick-Ereignis zuordnen möchte. Allerdings muss die Prozedur beim Zuordnen zum OnClick-Ereignis ein Parameter vom Typ TObject aufweisen (normal ist das Sender).
Frage: Wie kann ich die Prozedur zum Ereignis zuordnen, wenn ich nicht weiß welchen Parameter ich übergeben soll.
ausblenden Delphi-Quelltext
1:
BitBtn1.OnClick:=BitBtn1(???);					


Quelltext:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
unit FileOp;

interface
  uses Windows, Forms, ExtCtrls, Buttons, SysUtils, classes;
  var f:TForm; neu_name,alt_name:string; btn1, btn2: TBitBtn;
  procedure Datei_Kopieren(Quelle, Ziel: string);
  procedure Datei_Verschieben(Quelle, Ziel: string);
  procedure Datei_Loeschen(Quelle: string);
  procedure Datei_Umbenennen(Alter_Name: string);

implementation

uses shellapi;

var fos: TSHFileOpStruct;

function ohne_ext(FileName:string):string;
var i:byte;
begin
  i:=length(filename)-3;
  if filename[i]='.' then begin
    result:=copy(filename,1,i-1);
  end
  else result:=filename;
end;

procedure BitBtn1Click(Sender: TObject);
begin
if ansilowercase(ExtractFileExt(neu_name))<>
  ansilowercase(ExtractFileExt(alt_name)) then begin
  neu_name:=ExtractFilePath(alt_name)+
            ohne_ext(neu_name)+
            ExtractFileExt(alt_name); end
else neu_name:=ExtractFilePath(alt_name)+neu_name;
{***********************************************************}
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_RENAME;
    pFrom := PChar(alt_name + #0);
    pTo   := PChar(neu_name + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Umbenennen'nil,
MB_IconError);
{***********************************************************}
f.Free;
end;

procedure BitBtn2Click(Sender: TObject);
begin
f.Free;
end;

procedure Datei_Kopieren(quelle, ziel: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_COPY;
    pFrom := PChar(quelle + #0);
    pTo   := PChar(ziel + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Kopieren'nil,
MB_IconError);
end;

procedure Datei_Verschieben(quelle, ziel: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_MOVE;
    pFrom := PChar(quelle + #0);
    pTo   := PChar(ziel + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Verschieben'nil,
MB_IconError);
end;

procedure Datei_Loeschen(quelle: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_DELETE;
    pFrom := PChar(quelle + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Löschen'nil,
MB_IconError);
end;

procedure Datei_Umbenennen(Alter_Name: string);
var labedit:TLabeledEdit;
begin
  f:=TForm.Create(application);
  f.Caption:='Datei umbennenen';
  f.Width:=354;
  f.Height:=143;
  f.Position:=PoMainFormCenter;
  f.Show;
  {LableledEdit}
  labedit:=TLabeledEdit.Create(f);
  labedit.Parent:=f;
  labedit.Show;
  labedit.EditLabel.Caption:='Neuer Name';
  labedit.Height:=21;
  labedit.Left:=16;
  labedit.Top:=32;
  labedit.Width:=315;
  {OK-Button}
  btn1:=TBitBtn.Create(f);
  btn1.Parent:=f;
  btn1.Show;
  btn1.Kind:=bkOk;
  btn1.Height:=25;
  btn1.Left:=16;
  btn1.Top:=80;
  btn1.Width:=110;
  btn1.OnClick:=BitBtn1Click(???)                   //hier liegt Fehler Nr. 1
  {Abbrechen-Button}
  btn2:=TBitBtn.Create(f);
  btn2.Parent:=f;
  btn2.Show;
  btn2.Kind:=bkAbort;
  btn2.Height:=25;
  btn2.Left:=221;
  btn2.Top:=80;
  btn2.Width:=110;
  btn2.OnClick:=BitBtn2Click(???)                   //hier liegt Fehler Nr. 2
  {******************************************}
  alt_name:=Alter_Name;
  neu_name:=labedit.Text;
end;

end.
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Di 20.02.07 18:33 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Type
    TForm1 = Class(TForm)
        Button1: TButton;
        Procedure FormCreate(Sender: TObject);
        Procedure Klick(Sender: TObject); //wichtig, muss Methode sein
    Private
    { Private-Deklarationen }
    Public
    { Public-Deklarationen }
    End;

Var
    Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Klick(Sender: TObject);
Begin
    showmessage('asd');
End;

Procedure TForm1.FormCreate(Sender: TObject);
Begin
    Button1.OnClick := Klick;
End;

Dieser Code funktioniert bei mir, alsonur der Name der Prozedur, ohne Parameter.

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
delphi-programmierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 48

DOS, WIN XP
RAD Studio 2010, Turbo Pascal 6.0
BeitragVerfasst: Di 20.02.07 18:38 
Wenn ich es ohne Parameter mache, kommt folgende Fehlermeldung:

[Pascal Fehler] FileOp.pas(120): E2009 Inkompatible Typen: 'Methodenzeiger und reguläre Prozedur'
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Di 20.02.07 18:39 
Siehe die in meinem QT als "Wichtig" markierte Zeile, deine Prozedur muss eine Methode sein. Also Teil einer Klasse (wie TForm1 in meinem Beispiel)

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
delphi-programmierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 48

DOS, WIN XP
RAD Studio 2010, Turbo Pascal 6.0
BeitragVerfasst: Di 20.02.07 19:06 
Hi Leute,

habe jetzt eine extra Klasse für alle Prozeduren/Funktionen angelegt. Funktioniert beim reinen Unit-Compilieren und im VCL-Project jetzt prima! :P

Hier nochmal mein aktuellen Quelltext:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
unit FileOp;

interface
  uses Windows, Forms, ExtCtrls, Buttons, SysUtils;
  type TFileOp=class
    procedure Datei_Kopieren(Quelle, Ziel: string);
    procedure Datei_Verschieben(Quelle, Ziel: string);
    procedure Datei_Loeschen(Quelle: string);
    procedure Datei_Umbenennen(Alter_Name: string);
    private
      neu_name,alt_name:string;
      f:TForm;
      labedit:TLabeledEdit;
      btn1, btn2: TBitBtn;
      procedure bitbtn1Click(sender:TObject);
      procedure bitbtn2Click(sender:TObject);
      function ohne_ext(FileName:string):string;
  end;

implementation

uses shellapi;

var fos: TSHFileOpStruct;

function TFileOp.ohne_ext(FileName:string):string;
var i:byte;
begin
  i:=length(filename)-3;
  if filename[i]='.' then begin
    result:=copy(filename,1,i-1);
  end
  else result:=filename;
end;

procedure TFileOp.bitbtn1Click(sender: TObject);
var obj:TFileOp;
begin
obj:=TFileOp.Create;           //  Objekt/Klasse erzeugen
obj.neu_name:=ExtractFilePath(obj.alt_name)+
          ohne_ext(obj.labedit.Text)+
          ExtractFileExt(obj.alt_name);
{***********************************************************}
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_RENAME;
    pFrom := PChar(obj.alt_name + #0);
    pTo   := PChar(obj.neu_name + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Umbenennen'nil,
MB_IconError);
{***********************************************************}
obj.f.Free;
obj.Free;
end;

procedure TFileOp.bitbtn2Click(sender: TObject);
begin
obj:=TFileOp.Create;           //  Objekt/Klasse erzeugen
obj.f.Free;
obj.Free;
end;

procedure TFileOp.Datei_Kopieren(quelle, ziel: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_COPY;
    pFrom := PChar(quelle + #0);
    pTo   := PChar(ziel + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Kopieren'nil,
MB_IconError);
end;

procedure TFileOp.Datei_Verschieben(quelle, ziel: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_MOVE;
    pFrom := PChar(quelle + #0);
    pTo   := PChar(ziel + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Verschieben'nil,
MB_IconError);
end;

procedure TFileOp.Datei_Loeschen(quelle: string);
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do begin
    wFunc := FO_DELETE;
    pFrom := PChar(quelle + #0);
  end;
  if (SHFileOperation(fos)<>0then
    MessageBox(0'Fehler beim Löschen'nil,
MB_IconError);
end;

procedure TFileOp.Datei_Umbenennen(Alter_Name: string);
var obj:TFileOp;
begin
  obj:=TFileOp.Create;           //  Objekt/Klasse erzeugen
  obj.alt_name:=alter_name;      //  Dateiname übergeben
  obj.f:=TForm.Create(application);
  obj.f.Caption:='Datei umbennenen';
  obj.f.Width:=354;
  obj.f.Height:=143;
  obj.f.Position:=PoMainFormCenter;
  obj.f.Show;
  {LableledEdit}
  obj.labedit:=TLabeledEdit.Create(obj.f);
  obj.labedit.Parent:=obj.f;
  obj.labedit.Show;
  obj.labedit.EditLabel.Caption:='Neuer Name:';
  obj.labedit.Height:=21;
  obj.labedit.Left:=16;
  obj.labedit.Top:=32;
  obj.labedit.Width:=315;
  {OK-Button}
  obj.btn1:=TBitBtn.Create(obj.f);
  obj.btn1.Parent:=obj.f;
  obj.btn1.Show;
  obj.btn1.Kind:=bkOk;
  obj.btn1.Height:=25;
  obj.btn1.Left:=16;
  obj.btn1.Top:=80;
  obj.btn1.Width:=110;
  obj.btn1.OnClick:=BitBtn1Click;
  {Abbrechen-Button}
  obj.btn2:=TBitBtn.Create(obj.f);
  obj.btn2.Parent:=obj.f;
  obj.btn2.Show;
  obj.btn2.Kind:=bkAbort;
  obj.btn2.Height:=25;
  obj.btn2.Left:=221;
  obj.btn2.Top:=80;
  obj.btn2.Width:=110;
  obj.btn2.OnClick:=bitbtn2click;
  obj.Free;
end;

end.