如何在Delphi Firemonkey

时间:2016-04-14 02:24:56

标签: delphi firemonkey delphi-10-seattle

我正在尝试将位图分配给FMX Delphi中的一个速度按钮。在设计时,我创建一个TImageList,加载我的图像,然后将一个指定给速度按钮。

我如何以编程方式执行此操作?

2 个答案:

答案 0 :(得分:2)

var
  Size: TSizeF;
begin
  Size := TSize.Create(64,64)
  Bitmap1.Assign(Imagelist1.Bitmap(Size, Index));
end

答案 1 :(得分:1)

在TSpeedButton中,您应该设置Images和ImageIndex。 要将图片加载到TImageList,您可以使用AddOrSet 或者您可以使用此示例

procedure TForm11.Button2Click(Sender: TObject);
const
  SourceName = 'Картинка';
  procedure LoadPicture(const Source: TCustomSourceItem; const Scale: Single; const FileName: string);
  var
    BitmapItem: TCustomBitmapItem;
    TmpBitmap: TBitmap;
  begin
    BitmapItem := Source.MultiResBitmap.ItemByScale(Scale, True, True);
    if BitmapItem = nil then
    begin
      BitmapItem := Source.MultiResBitmap.Add;
      BitmapItem.Scale := Scale;
    end;
    BitmapItem.FileName := FileName;
    TmpBitmap := BitmapItem.CreateBitmap;
    try
      if TmpBitmap <> nil then
        BitmapItem.Bitmap.Assign(TmpBitmap);
    finally
      TmpBitmap.Free;
    end;
  end;
var
  NewSource: TCustomSourceItem;
  NewDestination: TCustomDestinationItem;
  NewLayer: TLayer;
begin
  if ImageList1.Source.IndexOf(SourceName) = -1 then
  begin
    NewSource := ImageList1.Source.Add;
    NewSource.Name := SourceName;
    NewSource.MultiResBitmap.TransparentColor := TColorRec.Fuchsia;
    NewSource.MultiResBitmap.SizeKind := TSizeKind.Custom;
    NewSource.MultiResBitmap.Width := 16;
    NewSource.MultiResBitmap.Height := 16;
    LoadPicture(NewSource, 1, 'D:\Мои веселые картинки\Icons\16x16\alarm16.bmp');
    LoadPicture(NewSource, 1.5, 'D:\Мои веселые картинки\Icons\24x24\alarm24.bmp');
    NewDestination := ImageList1.Destination.Add;
    NewLayer := NewDestination.Layers.Add;
    NewLayer.SourceRect.Rect := TRectF.Create(TPoint.Zero, NewSource.MultiResBitmap.Width,
      NewSource.MultiResBitmap.Height);
    NewLayer.Name := SourceName;
    ControlAction1.ImageIndex := NewDestination.Index;
  end;
end;