Sign in Join
Wiki copytemporary

TImageList/zh CN: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**TImageList** ![](/wiki-images/c/cc/Timagelist.png) 为我们提供了 [[TImage|images]] 的一个图片列表,我们可以在 [[TActionList]] 的 [[TToolBar]] 以及其他控件中调用显示图片信息。
22
33## ComboBox例子
44
55添加TImageList到窗口上,即可使用此控件。此示例中使用了6张图片。
66
77所有的图片尺寸必须相同,如果需要不同尺寸的图片需要重新添加一个TImageList。
88
99可以在对象检查器中修改图片尺寸,本示例中使用的尺寸 50px * 18px。
1010
1111设置 Style 为 csOwnerDrawFixed.
1212
1313双击TImageList图标,打开TImageList编辑器。然后选择图标,确保选择的图标正确显示。如果图片较小,请在队形查看器\<F11\>中确定设置的大小。然后重新加载图片。
1414
15-<figure>
16-<img src="ImageListEd.png" title="ImageListEd.png" />
17-<figcaption>ImageListEd.png</figcaption>
18-</figure>
15+![](/wiki-images/1/13/ImageListEd.png)
1916
2017在窗体上放置一个 ComboBox,然后修改Name为:cbSymbols。
2118
2219在FormCreate时间中写入如下代码:
2320
2421``` pascal
2522 cbSymbols.Items.Clear;
2623 for I := 0 to 5 do
2724 cbSymbols.Items.Add('');
2825 cbSymbols.ItemIndex := 0;
2926```
3027
3128将创建6个空条目,然后默认选择第一个。
3229
3330在cbSymbols的OnDrawItem实践中,写入如下代码:
3431
3532``` pascal
3633procedure TMyForm.cbSymbolsDrawItem(
3734 aControl: TWinControl;
3835 iIndex: Integer;
3936 aRect: TRect;
4037 aState: TOwnerDrawState );
4138var
4239 cnv: TCanvas;
4340begin
4441 if not (aControl is TComboBox) then exit;
4542 cnv := TComboBox( aControl ).Canvas;
4643 Imagelist1.Draw( cnv, aRect.Left+2, aRect.Top+2, iIndex );
4744end;
4845```
4946
5047有必要可以调整(+2),将图片居中
5148
52-<figure>
53-<img src="combo.png" title="combo.png" />
54-<figcaption>combo.png</figcaption>
55-</figure>
49+![](/wiki-images/1/1a/Combo.png)
5650
5751## SpeedButton例子
5852
5953和前面的例子一样,将图片添加到ImageList1中并使用:
6054
6155``` pascal
6256 ImageList1.GetBitmap(iIndex,SpeedButton1.Glyph);
6357```
6458
6559## Lazarus 1.9以及最新版本的高分辨率设置
6660
6761在Lazarus 1.9版本中,重写了TImageList,支持一个图像的多重分辨率。
6862
69-<figure>
70-<img src="ImageListEd-1.9.png" title="ImageListEd-1.9.png" />
71-<figcaption>ImageListEd-1.9.png</figcaption>
72-</figure>
63+![](/wiki-images/5/5b/ImageListEd-1.9.png)
7364
7465现在所有支持ImageList的控件都包含一个 \[Image\]Width属性 决定是否支持 96 PPI (100% 分辨率)。 例如: TToolBar.Images/ImageWidth, TListView.LargeImages/LargeImagesWidth.
7566
7667把Scaled属性设置为True,ImageList将自动调整LCL控件的DPI比例。
7768
7869### 程序演示
7970
8071Lazarus中有2个样例:
8172
8273- examples/imagelist_highdpi_designtime
8374- examples/imagelist_highdpi_runtime
8475
8576### 影响兼容的修改
8677
8778#### 添加方法
8879
8980- 旧特性: 如果图片太大,自动切片;如果图片太小,则自动放大。
9081- 新特性: 图像将自动缩放到设置的分辨率。
9182- 原因: ImageList现在支持多种分辨率。
9283- 补救/校正: 使用AddSliced (假如Image要添加多种图片) 或者 AddSlice (如果要添加自定义尺寸的图像-超出区域也支持).
9384
9485## Lazarus 1.8以及更以前版本的DPI替代方案: 调整TImageList中图片大小
9586
9687此方法对于 [[High DPI]] 缩放来获得 TActionList TMainMenu 和 TToolBar的高分辨率图片很有效。 注意: 此方法可能不适用于所有控件. 注意2: 此代码不支持 Lazrus 1.9+.
9788
9889``` pascal
9990procedure ScaleImageList(ImgList: TImageList; NewWidth, NewHeight: Integer);
10091var
10192 TempImgList: TImageList;
10293 TempBmp1: TBitmap;
10394 TempBmp2: TBitmap;
10495 I: Integer;
10596begin
10697 TempImgList := TImageList.Create(nil);
10798 TempBmp1 := TBitmap.Create;
10899 TempBmp1.PixelFormat := pf32bit;
109100 TempBmp2 := TBitmap.Create;
110101 TempBmp2.PixelFormat := pf32bit;
111102 TempBmp2.SetSize(NewWidth, NewHeight);
112103 try
113104 TempImgList.Width := NewWidth;
114105 TempImgList.Height := NewHeight;
115106
116107 for I := 0 to ImgList.Count - 1 do begin
117108 // Load image for given index to temporary bitmap
118109 ImgList.GetBitmap(I, TempBmp1);
119110
120111 // Clear transparent image background
121112 TempBmp2.Canvas.Brush.Style := bsSolid;
122113 TempBmp2.Canvas.Brush.Color := TempBmp2.TransparentColor;
123114 TempBmp2.Canvas.FillRect(0, 0, TempBmp2.Width, TempBmp2.Height);
124115
125116 // Stretch image to new size
126117 TempBmp2.Canvas.StretchDraw(Rect(0, 0, TempBmp2.Width, TempBmp2.Height), TempBmp1);
127118 TempImgList.Add(TempBmp2, nil);
128119 end;
129120
130121 ImgList.Assign(TempImgList);
131122 finally
132123 TempImgList.Free;
133124 TempBmp1.Free;
134125 TempBmp2.Free;
135126 end;
136127end;
137128```
138129
139130## 参考资料
140131
141132- [[doc:lcl//controls/timagelist.html|TImageList doc]]
142133- [[TImage]]
143134
144135---
145136*From the Free Pascal wiki, page "TImageList/zh CN", as it stood on 2020-04-26 after a change by TyphonFinger, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history