Search code examples
delphilistviewdelphi-xe7

how do i correctly draw gif image from resource inside listview?


i have listview item that i try to add image to its subitem as status-image i already can set image from image list but i want to get ride of image list and use images from resource i already created resource file and try to add Tgifimage to item draw but now image image not drawing

here is my code

procedure TForm1.Add_Item(strCaption: String; ListView: TListView;
  strFile: String; boolBlink: Boolean; strUniqueID: String;
  CurrentStatus: string);
var
  Item: TListItem;
begin
  Item := ListView1.Items.Add;
  Item.Caption := '';
  Item.SubItems.Add(strCaption);// subitem 0
  Item.SubItems.AddObject( '0', nil ); // subitem 1
  Item.SubItems.Add( strUniqueID ); // subitem 2 // UniqueID
  Item.SubItems.Add('0'); // subitem 3 // Next User Idx (beside)
  Item.SubItems.Add(Currentstatus); // subitem 4 // StateIdx
  Item.Data := nil;
  SetItemStatusGif(Item, Currentstatus); // here start to set status
  end;

  // here setitemStatusGif procedure

  procedure TForm1.SetItemStatusGif(Item: TListItem; State: String);
  var
    ResStream: TResourceStream;
    aGif: TGifImage;
    strStateImg: String;
    ImgIdx: Integer;
  begin
    strStateImg := 'State_' + State;
    ImgIdx := StatusGifs.IndexOf(strStateImg);
    if ImgIdx <> -1 then
      aGif := TGifImage(StatusGifs.Objects[ImgIdx])
    else
    begin
      try
        ResStream := TResourceStream.Create(HInstance, strStateImg, RT_RCDATA);
        try
          aGif := TGifImage.Create;
          try
            aGif.LoadFromStream(ResStream);
            aGif.Transparent := True;
            StatusGifs.AddObject(strStateImg, aGif);
          except
            aGif.Free;
            raise;
          end;
        finally
          ResStream.Free;
        end;
      except
        aGif := nil;
      end;
    end;
    Item.SubItems.Objects[1] := aGif;
    ListView1.UpdateItems(Item.Index, Item.Index);
  end;

  // here listview draw event code
procedure TForm1.ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
    Rect: TRect; State: TOwnerDrawState);
Var
    xOff, yOff: Integer;
    R: TRect;
    i: Integer;
    NewRect: TRect;
begin
    With TListView(Sender).Canvas do
    begin // User State Image
    if (StrToint(Item.SubItems[1]) <> 0) And (Item.SubItems[1] <> '') then
    begin
    NewRect := Rect;
    NewRect.Left   := NewRect.Left + 2;
    NewRect.Width  := 24;
    Newrect.Height :=  23;
    NewRect.Top    := NewRect.Top;
    NewRect.Bottom := NewRect.Bottom;
    if Panel2.Visible AND (Item.Index = 0) then
    //do nothing
else
   Sender.Canvas.StretchDraw( NewRect, TGIFImage( Item.SubItems.Objects[1])  );
   end;
       end;
        end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
ListView1.Invalidate; // This is for animation over ListView Canvas
end;

Solution

  • We covered this a month ago in your other question:

    how do i update listview item index inside thread

    In that question, you were downloading images from online, where the download thread creates the TGifImage object and assigns it to a TListItem for drawing. Now, you want to add resource images. You still have to create a TGifImage object for them, and assign that to your TListItem object so you can draw it. You just don't need to use a thread to handle that. When you add a new item to the list, you can create the TGifImage object immediately and fill it from the resource, eg:

    type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject); 
        procedure FormDestroy(Sender: TObject); 
        procedure ListView1Deletion(Sender: TObject; Item: TListItem);
        ...
      private
        StatusGifs: TStringList;
        procedure Add_Item(strCaption: String; ListView: TListView; strFile: String; boolBlink: Boolean; strUniqueID: String; CurrentStatus: string);
        procedure StatuseHandle;
        procedure SetItemStatusGif(Item: TListItem; State: String);
        ...
      end;
    
    procedure TForm1.FormCreate(Sender: TObject); 
    begin
      StatusGifs := TStringList.Create(True);
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject); 
    begin
      StatusGifs.Free;
    end;
    
    procedure TForm1.ListView1Deletion(Sender: TObject; Item: TListItem);
    begin
      TGifImage(Item.SubItems.Objects[1]).Free;
      TGifImage(Item.Data).Free;
    end;
    
    procedure TForm1.Add_Item(strCaption: String; ListView: TListView; strFile: String; boolBlink: Boolean; strUniqueID: String; CurrentStatus: string);
    var
      Item: TListItem;
    begin
      Item := ListView1.Items.Add;
      Item.Caption := '';
      Item.SubItems.Add( strCaption ); // subitem 0
      Item.SubItems.AddObject( 'IMA', TGifImage.Create ); // subitem 1
      Item.SubItems.Add( strUniqueID ); // subitem 2 // UniqueID
      Item.SubItems.Add('0'); // subitem 3 // Next User Idx (beside)
      Item.SubItems.Add(Currentstatus); // subitem 4 // StateIdx
      Item.Data := nil; // populated by TURLDownload
    
      SetItemStatusGif(Item, Currentstatus);
      TURLDownload.Create(strFile, UpdateVisual, Item);
    end;
    
    procedure TForm1.StatuseHandle;
    var
      i : integer;
      Item : TListItem;
    begin
      try
        for i := 0 to ListView1.Items.Count-1 do
        begin
          Item := ListView1.Items[i];
          if Item.SubItems[2] = Trim(LineToid) then
          begin
            Item.SubItems[4] := LineTostatus;
            SetItemStatusGif(Item, LineTostatus);
          end;
        end;
      except
      end;
    end;
    
    procedure TForm1.SetItemStatusGif(Item: TListItem; State: String);
    var
      ResStream  : TResourceStream;
      aGif : TGifImage;
      strStateImg : String;
      ImgIdx: Integer;
    begin
      strStateImg := 'State_' + State;
    
      ImgIdx := StatusGifs.IndexOf(strStateImg);
      if ImgIdx <> -1 then
        aGif := TGifImage(StatusGifs.Objects[ImgIdx])
      else
      begin
        try
          ResStream  := TResourceStream.Create(HInstance, strStateImg, RT_RCDATA);
          try
            aGif := TGifImage.Create;
            try
              aGif.LoadFromStream(ResStream);
              aGif.Transparent := True;
              StatusGifs.AddObject(strStateImg, aGif);
            except
              aGif.Free;
              raise;
            end;
          finally
            ResStream.Free;
          end;
        except
          aGif := nil;
        end;
      end;
    
      TGifImage(Item.SubItems.Objects[1]).Assign(aGif);
      ListView1.UpdateItems(Item.Index, Item.Index);
    end;