-1

我的代码有问题但找不到解决方案,已经以各种方式修改但没有成功。

代码:

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”和“过程、无类型指针或无类型参数”

怎么了?谢谢!

4

2 回答 2

5

OnClick事件处理程序TImage是 a 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使用Imageand代替局部变量。毕竟,您希望能够轻松访问这些控件。您还可以将 的 设置为的当前值,然后在 中,您可以检查访问,例如。AssociatedEditControl1AssociatedEditControl2TagTImageiImageClickHandlerSelf.TagImageData[Self.Tag].AssociatedEditControl1

(但我仍然认为您应该更好地将内部数据与 GUI 分开。您还需要修复内存泄漏。)

于 2013-08-01T18:57:32.907 回答
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;
于 2013-08-01T19:38:11.597 回答