我如何正确地从内部列表视图资源绘制gif图片? [英] how do i correctly draw gif image from resource inside listview?

查看:171
本文介绍了我如何正确地从内部列表视图资源绘制gif图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我尝试将图像添加到子项目状态列表视图项目-image 我已经可以设置从图像列表中的图像,但我想获得的图像列表的车程,从资源使用的图片我已经创建的资源文件,并尝试添加 Tgifimage 项画,但现在图像不拉伸

这是我的code

 程序TForm1.Add_Item(strCaption:字符串; ListView控件:TListView的;
  strFile:字符串; boolBlink:布尔; strUniqueID:字符串;
  CurrentStatus:字符串);
VAR
  项目:TListItem;
开始
  项目:= ListView1.Items.Add;
  Item.Caption:='';
  Item.SubItems.Add(strCaption); // 0分项目
  Item.SubItems.AddObject('0',无); //子项目1
  Item.SubItems.Add(strUniqueID); //子项目2 // UniqueID的
  Item.SubItems.Add('0'); //子项目3 //下一步并按Idx用户(旁)
  Item.SubItems.Add(Currentstatus); //子项目4 // StateIdx
  Item.Data:=零;
  SetItemStatusGif(项目,Currentstatus); //这里开始设置状态
  结束;  //这里setitemStatusGif程序  程序TForm1.SetItemStatusGif(货号:TListItem;状态:字符串);
  VAR
    ResStream:TResourceStream;
    AGIF:TGifImage;
    strStateImg:字符串;
    ImgIdx:整数;
  开始
    strStateImg:='STATE_'+国;
    ImgIdx:= StatusGifs.IndexOf(strStateImg);
    如果ImgIdx<>然后-1
      AGIF:= TGifImage(StatusGifs.Objects [ImgIdx])
    其他
    开始
      尝试
        ResStream:= TResourceStream.Create(HINSTANCE,strStateImg,RT_RCDATA);
        尝试
          AGIF:= TGifImage.Create;
          尝试
            aGif.LoadFromStream(ResStream);
            aGif.Transparent:= TRUE;
            StatusGifs.AddObject(strStateImg,AGIF);
          除
            aGif.Free;
            提高;
          结束;
        最后
          ResStream.Free;
        结束;
      除
        AGIF:=零;
      结束;
    结束;
    Item.SubItems.Objects [1]:= AGIF;
    ListView1.UpdateItems(Item.Index,Item.Index);
  结束;  //这里的ListView画事件code
程序TForm1.ListView1DrawItem(发件人:TCustomListView;项目:TListItem;
    RECT:TRect;状态:TOwnerDrawState);
瓦尔
    x关,YOFF:整数;
    R:TRect;
    我:整数;
    NewRect:TRect;
开始
    随着的TListView(发件人).Canvas做
    开始//用户国家形象
    如果(StrToint(Item.SubItems [1])&所述;大于0)和(Item.SubItems [1];>''),然后
    开始
    NewRect:=矩形;
    NewRect.Left:= NewRect.Left + 2;
    NewRect.Width:= 24;
    Newrect.Height:= 23;
    NewRect.Top:= NewRect.Top;
    NewRect.Bottom:= NewRect.Bottom;
    如果Panel2.Visible AND(Item.Index = 0),那么
    //没做什么
其他
   Sender.Canvas.StretchDraw(NewRect,TGIFImage(Item.SubItems.Objects [1]));
   结束;
       结束;
        结束;
程序TForm1.Timer1Timer(发件人:TObject的);
开始
ListView1.Invalidate; //这是在动画画布的ListView
结束;


解决方案

我们在您的其他问题包括在本一个月前:

如何更新线程内ListView项指数

在这个问题,你下载的网络上,下载线程创建 TGifImage 对象,并将其分配给 TListItem 绘图。现在,您要添加的资源的图像。你仍然必须创建一个 TGifImage 对象对他们来说,并分配给你的 TListItem 对象,这样你就可以画出来。你只是不需要使用一个线程来处理。当您添加一个新的项目列表中,您可以立即创建 TGifImage 对象,并从资源填充它,例如:

 键入
  TForm1 =类(TForm的)
    程序FORMCREATE(发件人:TObject的);
    程序FormDestroy(发件人:TObject的);
    程序ListView1Deletion(发件人:TObject的;项目:TListItem);
    ...
  私人的
    StatusGifs:TStringList中;
    程序Add_Item(strCaption:字符串; ListView控件:TListView的; strFile:字符串; boolBlink:布尔; strUniqueID:字符串; CurrentStatus:字符串);
    程序StatuseHandle;
    程序SetItemStatusGif(货号:TListItem;状态:字符串);
    ...
  结束;程序TForm1.FormCreate(发件人:TObject的);
开始
  StatusGifs:= TStringList.Create(真);
结束;程序TForm1.FormDestroy(发件人:TObject的);
开始
  StatusGifs.Free;
结束;程序TForm1.ListView1Deletion(发件人:TObject的;项目:TListItem);
开始
  TGifImage(Item.SubItems.Objects [1])免费。
  TGifImage(Item.Data)。免费;
结束;程序TForm1.Add_Item(strCaption:字符串; ListView控件:TListView的; strFile:字符串; boolBlink:布尔; strUniqueID:字符串; CurrentStatus:字符串);
VAR
  项目:TListItem;
开始
  项目:= ListView1.Items.Add;
  Item.Caption:='';
  Item.SubItems.Add(strCaption); // 0分项目
  Item.SubItems.AddObject(IMA,TGifImage.Create); //子项目1
  Item.SubItems.Add(strUniqueID); //子项目2 // UniqueID的
  Item.SubItems.Add('0'); //子项目3 //下一步并按Idx用户(旁)
  Item.SubItems.Add(Currentstatus); //子项目4 // StateIdx
  Item.Data:=零; //通过TURLDownload填充  SetItemStatusGif(项目,Currentstatus);
  TURLDownload.C​​reate(strFile,UpdateVisual,项目);
结束;程序TForm1.StatuseHandle;
VAR
  我:整数;
  项目:TListItem;
开始
  尝试
    对于i:= 0至ListView1.Items.Count-1做
    开始
      项目:= ListView1.Items [I]
      如果Item.SubItems [2] =修剪(LineToid)然后
      开始
        Item.SubItems [4]:= LineTostatus;
        SetItemStatusGif(项目,LineTostatus);
      结束;
    结束;
  除
  结束;
结束;程序TForm1.SetItemStatusGif(货号:TListItem;状态:字符串);
VAR
  ResStream:TResourceStream;
  AGIF:TGifImage;
  strStateImg:字符串;
  ImgIdx:整数;
开始
  strStateImg:='STATE_'+国;  ImgIdx:= StatusGifs.IndexOf(strStateImg);
  如果ImgIdx<>然后-1
    AGIF:= TGifImage(StatusGifs.Objects [ImgIdx])
  其他
  开始
    尝试
      ResStream:= TResourceStream.Create(HINSTANCE,strStateImg,RT_RCDATA);
      尝试
        AGIF:= TGifImage.Create;
        尝试
          aGif.LoadFromStream(ResStream);
          aGif.Transparent:= TRUE;
          StatusGifs.AddObject(strStateImg,AGIF);
        除
          aGif.Free;
          提高;
        结束;
      最后
        ResStream.Free;
      结束;
    除
      AGIF:=零;
    结束;
  结束;  TGifImage(Item.SubItems.Objects [1]),分配(AGIF)。
  ListView1.UpdateItems(Item.Index,Item.Index);
结束;

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;

解决方案

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;

这篇关于我如何正确地从内部列表视图资源绘制gif图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆