Delphi通过编码点击图片

时间:2019-07-06 09:22:29

标签: delphi

我正在做一个项目,如果您按下图像,则图像将通过编码导致再次按下按钮。

我尝试了Image.click;Image.OnClick := Imageclick;

procedure TForm1.RandomRekenaar;
var
  z: integer;
begin
  z := 0;
  if (rgpSpelTipe.ItemIndex = 0) and (rgpSpelers.ItemIndex = 2) then
  begin
    z := Random(16 - 1 + 1);
    case z of
      1:
          if Prent41.Visible <> False 
          then Prent41.OnClick := Prent41Click
          else RandomRekenaar;
      2:
        begin
          if Prent42.Visible <> False then
          begin
            Prent42.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      3:
        begin
          if Prent43.Visible <> False then
          begin
            Prent43.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      4:
        begin
          if Prent44.Visible <> False then
          begin
            Prent44.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      5:
        begin
          if Prent45.Visible <> False then
          begin
            Prent45.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      6:
        begin
          if Prent46.Visible <> False then
          begin
            Prent46.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      7:
        begin
          if Prent47.Visible <> False then
          begin
            Prent47.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      8:
        begin
          if Prent48.Visible <> False then
          begin
            Prent48.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      9:
        begin
          if Prent49.Visible <> False then
          begin
            Prent49.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      10:
        begin
          if Prent410.Visible <> False then
          begin
            Prent410.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      11:
        begin
          if Prent411.Visible <> False then
          begin
            Prent411.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      12:
        begin
          if Prent412.Visible <> False then
          begin
            Prent412.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      13:
        begin
          if Prent413.Visible <> False then
          begin
            Prent413.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      14:
        begin
          if Prent414.Visible <> False then
          begin
            Prent414.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      15:
        begin
          if Prent415.Visible <> False then
          begin
            Prent415.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;
      16:
        begin
          if Prent416.Visible <> False then
          begin
            Prent416.Click;
          end
          else
          begin
            RandomRekenaar;
          end;
        end;

    end;
  end;
end;

2 个答案:

答案 0 :(得分:1)

尝试更多类似的方法:

var
  Images: array[0..15] of TImage;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Images[0] := Prent41;
  Images[1] := Prent42;
  ...
  Images[15] := Prent416;

  { or:
  for I := 0 to 15 do
    Images[I] := TImage(FindComponent('Prent4'+IntToStr(I+1)));
  }
end;

function TForm1.RandomImage: TImage;
var
  VisibleImages: array[0..15] of TImage;
  I, Count: Integer;
begin
  Result := nil;
  Count := 0;
  for I := 0 to 15 do
  begin
    if Images[I].Visible then
    begin
      VisibleImages[Count] := Images[I];
      Inc(Count);
    end;
  end;
  if Count > 0 then
    Result := VisibleImages[Random(Count)];
end;

procedure TForm1.RandomRekenaar;
var
  Image: TImage;
begin
 if (rgpSpelTipe.ItemIndex = 0) and (rgpSpelers.ItemIndex = 2) then
 begin
   Image := RandomImage;
   if Image <> nil then
     Image.Click; // or Image.OnClick(Image);
  end;
end;

答案 1 :(得分:0)

您可以执行以下操作:

ImageClick(Image);

无论如何,我建议定义一个单独的函数:

procedure TForm1.SelectRandomImage();
begin
  //...
end;


procedure TForm1.ImageClick(Sender : TObject);
begin
  SelectRandomImage();
end;

这样,您可以直接调用SelectRandomImage过程,我认为代码将更具可读性。