我的代码出现问题,但找不到解决方案,已经以各种方式修改但没有成功。
代码:
private
{ Private declarations }
procedure getImgInfo(Sender: TObject; A, B: String);
和
procedure TfMain.Button1Click(Sender: TObject);
var
i, Idx, Left, Top, Count : integer;
Graph : TGraphic;
Img : TImage;
EdPath, EdFileName : TEdit;
begin
openImg.Execute;
Left := 5;
Top := 5;
Count := 0;
Idx := 0;
for i:=0 to openImg.Files.Count-1 do
begin
try
begin
Graph := TPngImage.Create;
Graph.LoadFromFile(openImg.Files[i]);
EdPath := TEdit.Create(pImgs);
EdPath.Left := Left + 101;
EdPath.Visible := False;
EdPath.Text := ExtractFilePath(openImg.Files[i]);
EdFileName := TEdit.Create(pImgs);
EdFileName.Left := Left + 101;
EdFileName.Visible := False;
EdFileName.Text := ExtractFileName(openImg.Files[i]);
Img := TImage.Create(pImgs);
Img.Parent := pImgs;
Idx := Idx + 1;
Img.Name := 'Img_'+IntToStr(Idx);
Img.Width := 100;
Img.Height := 100;
Img.Left := Left;
Img.Proportional := True;
Left := Left + 101;
Img.Top := Top;
Img.Picture.Assign(Graph);
Img.BringToFront;
Count := Count + 1;
Img.OnClick := getImgInfo(Img, edPath.Text, edFileName.Text); //Error line
if Count = 2 then
begin
Left := 5;
Top := Top + 101;
Count := 0;
end;
end;
except on E : Exception do
ShowMessage('Error: :' + E.Message);
end;
end;
end;
错误:
[dcc32错误] uMain.pas(74):E2010不兼容的类型:'TNotifyEvent'和'过程,无类型指针或无类型参数'
有什么问题? 谢谢!
答案 0 :(得分:5)
OnClick
的{{1}}事件处理程序是TImage
,因此您只能 为其分配此类过程。这是方法(属于对象的过程),它采用TNotifyEvent
类型的单个参数Sender
。所以这将有效:
TObject
...
procedure TfMain.ImageClickHandler(Sender: TObject);
begin
// Do something
end;
您需要某种数据结构来存储数据。也许
Img.OnClick := ImageClickHandler;
和
type
TImageData = record
Image: TImage;
ImageTitle: string;
ImageFileName: string;
Photographer: string;
DateTaken: TDateTime;
end;
或者,更类似于您的代码:
var
ImageData = array of TImageData;
然后,您将type
TImageData = record
Image: TImage;
AssociatedEditControl1,
AssociatedEditControl2: TEdit;
end;
var
ImageData = array of TImageData;
的长度设置为ImageData
,并使用openImg.Files.Count
和Image
以及AssociatedEditControl1
代替本地变量。毕竟,您希望能够轻松访问这些控件。您还可以将AssociatedEditControl2
的{{1}}设置为Tag
的当前值,然后在TImage
中,您可以检查i
以访问{{1} ,比如说。
(但我仍然认为你应该更好地将内部数据与GUI分开。你还需要修复你的内存泄漏。)
答案 1 :(得分:0)
尝试更像这样的东西:
type
PImageInfo = ^ImageInfo;
ImageInfo = record
Path: String;
FileName: String;
Img: TImage;
EdPath: TEdit;
EdFileName : TEdit;
// anything else you need...
end;
private
{ Private declarations }
Images: array of ImageInfo;
procedure ImageClicked(Sender: TObject);
procedure TfMain.Button1Click(Sender: TObject);
var
i, ImgLeft, ImgTop, Count : integer;
Graph : TGraphic;
Info: PImageInfo;
begin
if not openImg.Execute then Exit;
ImgLeft := 5;
ImgTop := 5;
Count := Length(Images);
try
SetLength(Images, Count + openImg.Files.Count);
for I := 0 to openImg.Files.Count-1 do
begin
Graph := TPngImage.Create;
try
Graph.LoadFromFile(openImg.Files[i]);
Info := @Images[Count];
Info.Path := ExtractFilePath(openImg.Files[i]);
Info.FileName := ExtractFileName(openImg.Files[i]);
Info.Img := nil;
Info.EdPath := nil;
Info.EdFileName := nil;
try
Info.EdPath := TEdit.Create(pImgs);
Info.EdPath.Left := ImgLeft + 101;
Info.EdPath.Visible := False;
Info.EdPath.Text := Path;
Info.EdFileName := TEdit.Create(pImgs);
Info.EdFileName.Left := ImgLeft + 101;
Info.EdFileName.Visible := False;
Info.EdFileName.Text := Images[Count].FileName;
Info.Img := TImage.Create(pImgs);
Info.Img.Parent := pImgs;
Info.Img.Tag := Count;
Info.Img.Name := 'Img_'+IntToStr(Count);
Info.Img.SetBounds(ImgLeft, ImgTop, 100, 100);
Info.Img.Proportional := True;
Info.Img.OnClick := ImageClicked;
Info.Img.Picture.Assign(Graph);
Info.Img.BringToFront;
except
Info.EdPath.Free;
Info.EdFileName.Free;
Info.Img.Free;
raise;
end;
finally
Graph.Free;
end;
Inc(Count);
if (Count mod 2) = 0 then
begin
ImgLeft := 5;
Inc(ImgTop, 101);
end else
Inc(ImgLeft, 101);
end;
except
on E : Exception do
begin
SetLength(Images, Count);
ShowMessage('Error: :' + E.Message);
end;
end;
end;
procedure TfMain.ImageClicked(Sender: TObject);
var
Info: PImageInfo;
begin
Info := @Images[(Sender as TImage).Tag];
// use Info as needed...
end;