Delphi 同时添加多个图标,加入到ImageList控件,再展现到ListView控件
这个问题是群里一位网友发的问题.我请教了猫哥,学到了点东西,所以把这个知识点记下来.
设计图:

成品图:

以下是代码:
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ComCtrls, System.ImageList, Vcl.ImgList, Vcl.ExtCtrls; type TForm1 = class(TForm) ImageList1: TImageList; ListView1: TListView; OpenDialog1: TOpenDialog; Panel1: TPanel; BitBtn2: TBitBtn; BitBtn1: TBitBtn; Button1: TButton; Edit1: TEdit; procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.BitBtn1Click(Sender: TObject); var ico: TIcon; i, j, k: integer; strFileName: string; FF: Tstrings; begin OpenDialog1.Filter := '文件类型 |*.Ico;*.Png;*.Jpg;*.Bmp;'; if OpenDialog1.Execute then begin ico := TIcon.Create;
ico.Width := 128; //设置图标尺寸
ico.Height := 128;
FF := OpenDialog1.Files; //这里包含了所有被选择的文件路径+文件名 for j := 0 to FF.Count - 1 do begin strFileName := FF[j]; //取出文件名 ico.LoadFromFile(strFileName); //加载文件 ImageList1.AddIcon(ico); // with ListView1.Items.Add do //同是加入到listview列表 begin caption := ChangeFileExt(ExtractFileName(strFileName), ''); SubItems.Add(strFileName); end; i := ListView1.Items.Count - 1; ListView1.Items.Item[i].ImageIndex := i; // 指定对应索引 end; ico.Free; //操作结束后释放ico end; end; //切换显示模式 procedure TForm1.BitBtn2Click(Sender: TObject); begin if bitbtn2.Caption = 'vsIcon' then begin listview1.ViewStyle := vsReport; bitbtn2.Caption := 'vsReport'; end else if bitbtn2.Caption = 'vsReport' then begin listview1.ViewStyle := vsIcon; bitbtn2.Caption := 'vsIcon'; end; end; end.

浙公网安备 33010602011771号