在运行时将远程图像加载到功能区栏项目中

时间:2014-01-24 18:18:54

标签: delphi delphi-xe5 ribbon-control

我正在使用Delphi XE-5和TRibbon控件,我正在尝试使用来自远程位置(URL)的图像填充我的BarItems。除了Ribbon控件本身,TImageList和indy组件(idHTTP)之外,所有内容都在运行时创建。

这是我正在寻找的效果

enter image description here

以下是加载图片之前的内容

enter image description here

这是加载图像后的样子

enter image description here

下面的代码读取包含选项卡,组和按钮的JSON文件,并在TRibbon控件上创建选项卡,组和按钮。对于每个按钮,它从互联网上检索png并将其分配给Bitmap并将其添加到ImageList,并通过其imageindex属性将其分配给BarItem。图像是50X35(我已经更改了Imagelist属性以反映这一点。)加载图像的代码显示在// ********** //

之间
procedure TfrmMain.Button1Click(Sender: TObject);
var
 I, J, K: Integer;
 JSONData, TabNames, GroupNames, ButtonNames: TStringList;
 NavData: TJSONObject;
 Tabs, Groups, Buttons, Tab, Group, Button: TJSONValue;
 TabItem: TRibbonTabItem;
 RGroup: TRibbonGroup;
 BarItem: TActionBarItem;
 BarAction: TActionClientItem;
 asset: String;
 HTTP: TIdHTTP;
 MS : TMemoryStream;
 png: TPngImage;
 bmp: TBitmap;
begin
 JSONData:= TStringList.Create;
 if openDialog1.Execute then
 begin
  JSONData.LoadFromFile(openDialog1.FileName);
  try
    NavData:= getJSONObj(JSONData.Text);
    if NavData <> nil then
    begin
     Tabs:= getTabs(NavData);
     TabNames:= TStringList.Create;
     getTabNames(Tabs, TabNames);
     for I:= 0 to TabNames.Count-1 do
     begin
      TabItem := Ribbon1.Tabs.Add;
      TabItem.Caption := TabNames[I];
      Tab:= getTabByName(Tabs, TabNames[I]);
      Groups:= getGroups(TJSonObject(Tab));
      GroupNames:= TStringList.Create;
      getGroupNames(Groups, GroupNames);
      for J:= 0 to GroupNames.Count-1 do
      begin
       RGroup := TRibbonGroup.Create(Ribbon1);
       RGroup.Parent := TabItem.Page;
       RGroup.Rows:= 1;
       RGroup.AutoSizing:= True;
       RGroup.Caption := GroupNames[J];
       Group:= getGroupByName(Groups, GroupNames[J]);
       Buttons:= getButtons(TJSonObject(Group));
       ButtonNames:= TStringList.Create;
       getButtonNames(Buttons, ButtonNames);
       BarItem := ActionManager1.ActionBars.Add;
       BarItem.ActionBar := RGroup;
       BarItem.AutoSize:= True;
       BarItem.GlyphLayout:= blGlyphTop;
       BarItem.BackgroundLayout:= blStretch;
       for K:= 0 to ButtonNames.Count-1 do
       begin
        BarAction := BarItem.Items.Add;
        BarAction.Caption := ButtonNames[K];
        BarAction.ImageIndex:= 0;
        Button:= getButtonByName(Buttons, ButtonNames[K]);
        asset:= StringReplace(TJSONObject(Button).GetValue('asset').ToString, '"','',[rfReplaceAll]);
        //******************************************************************************************
        //get and place asset on button
         MS := TMemoryStream.Create;
         png := TPngImage.Create;
         png.Transparent:= True;
         try
          idHTTP1.get(asset,MS);
          Ms.Seek(0,soFromBeginning);
          png.LoadFromStream(MS);
          //add to image list here
          bmp:= TBitmap.Create;
          bmp.Width:= 50;
          bmp.Height:= 35;
          png.AssignTo(bmp);
          ImageList1.Add(bmp, nil);
          BarAction.ImageIndex:= ImageList1.Count-1;
          bmp.Free;
         finally
         FreeAndNil(png);
         FreeAndNil(MS);
         end;
        //******************************************************************************************
       end; //for K:= 0 to ButtonNames.Count-1 do
      ButtonNames.Free;
      end; //for J:= 0 to GroupNames.Count-1 do
      GroupNames.Free;
     end;
     TabNames.Free;
    end;
  finally
    NavData.Free;
  end;
 end;
 JSONData.free;
end;

0 个答案:

没有答案