TImageList/zh CN: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TImageList/zh CN
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TImageList**  为我们提供了 [[TImage|images]] 的一个图片列表,我们可以在 [[TActionList]] 的 [[TToolBar]] 以及其他控件中调用显示图片信息。 | |
| 2 | + | ||
| 3 | + | ## ComboBox例子 | |
| 4 | + | ||
| 5 | + | 添加TImageList到窗口上,即可使用此控件。此示例中使用了6张图片。 | |
| 6 | + | ||
| 7 | + | 所有的图片尺寸必须相同,如果需要不同尺寸的图片需要重新添加一个TImageList。 | |
| 8 | + | ||
| 9 | + | 可以在对象检查器中修改图片尺寸,本示例中使用的尺寸 50px * 18px。 | |
| 10 | + | ||
| 11 | + | 设置 Style 为 csOwnerDrawFixed. | |
| 12 | + | ||
| 13 | + | 双击TImageList图标,打开TImageList编辑器。然后选择图标,确保选择的图标正确显示。如果图片较小,请在队形查看器\<F11\>中确定设置的大小。然后重新加载图片。 | |
| 14 | + | ||
| 15 | + | <figure> | |
| 16 | + | <img src="ImageListEd.png" title="ImageListEd.png" /> | |
| 17 | + | <figcaption>ImageListEd.png</figcaption> | |
| 18 | + | </figure> | |
| 19 | + | ||
| 20 | + | 在窗体上放置一个 ComboBox,然后修改Name为:cbSymbols。 | |
| 21 | + | ||
| 22 | + | 在FormCreate时间中写入如下代码: | |
| 23 | + | ||
| 24 | + | ``` pascal | |
| 25 | + | cbSymbols.Items.Clear; | |
| 26 | + | for I := 0 to 5 do | |
| 27 | + | cbSymbols.Items.Add(''); | |
| 28 | + | cbSymbols.ItemIndex := 0; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | 将创建6个空条目,然后默认选择第一个。 | |
| 32 | + | ||
| 33 | + | 在cbSymbols的OnDrawItem实践中,写入如下代码: | |
| 34 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | procedure TMyForm.cbSymbolsDrawItem( | |
| 37 | + | aControl: TWinControl; | |
| 38 | + | iIndex: Integer; | |
| 39 | + | aRect: TRect; | |
| 40 | + | aState: TOwnerDrawState ); | |
| 41 | + | var | |
| 42 | + | cnv: TCanvas; | |
| 43 | + | begin | |
| 44 | + | if not (aControl is TComboBox) then exit; | |
| 45 | + | cnv := TComboBox( aControl ).Canvas; | |
| 46 | + | Imagelist1.Draw( cnv, aRect.Left+2, aRect.Top+2, iIndex ); | |
| 47 | + | end; | |
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | 有必要可以调整(+2),将图片居中 | |
| 51 | + | ||
| 52 | + | <figure> | |
| 53 | + | <img src="combo.png" title="combo.png" /> | |
| 54 | + | <figcaption>combo.png</figcaption> | |
| 55 | + | </figure> | |
| 56 | + | ||
| 57 | + | ## SpeedButton例子 | |
| 58 | + | ||
| 59 | + | 和前面的例子一样,将图片添加到ImageList1中并使用: | |
| 60 | + | ||
| 61 | + | ``` pascal | |
| 62 | + | ImageList1.GetBitmap(iIndex,SpeedButton1.Glyph); | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | ## Lazarus 1.9以及最新版本的高分辨率设置 | |
| 66 | + | ||
| 67 | + | 在Lazarus 1.9版本中,重写了TImageList,支持一个图像的多重分辨率。 | |
| 68 | + | ||
| 69 | + | <figure> | |
| 70 | + | <img src="ImageListEd-1.9.png" title="ImageListEd-1.9.png" /> | |
| 71 | + | <figcaption>ImageListEd-1.9.png</figcaption> | |
| 72 | + | </figure> | |
| 73 | + | ||
| 74 | + | 现在所有支持ImageList的控件都包含一个 \[Image\]Width属性 决定是否支持 96 PPI (100% 分辨率)。 例如: TToolBar.Images/ImageWidth, TListView.LargeImages/LargeImagesWidth. | |
| 75 | + | ||
| 76 | + | 把Scaled属性设置为True,ImageList将自动调整LCL控件的DPI比例。 | |
| 77 | + | ||
| 78 | + | ### 程序演示 | |
| 79 | + | ||
| 80 | + | Lazarus中有2个样例: | |
| 81 | + | ||
| 82 | + | - examples/imagelist_highdpi_designtime | |
| 83 | + | - examples/imagelist_highdpi_runtime | |
| 84 | + | ||
| 85 | + | ### 影响兼容的修改 | |
| 86 | + | ||
| 87 | + | #### 添加方法 | |
| 88 | + | ||
| 89 | + | - 旧特性: 如果图片太大,自动切片;如果图片太小,则自动放大。 | |
| 90 | + | - 新特性: 图像将自动缩放到设置的分辨率。 | |
| 91 | + | - 原因: ImageList现在支持多种分辨率。 | |
| 92 | + | - 补救/校正: 使用AddSliced (假如Image要添加多种图片) 或者 AddSlice (如果要添加自定义尺寸的图像-超出区域也支持). | |
| 93 | + | ||
| 94 | + | ## Lazarus 1.8以及更以前版本的DPI替代方案: 调整TImageList中图片大小 | |
| 95 | + | ||
| 96 | + | 此方法对于 [[High DPI]] 缩放来获得 TActionList TMainMenu 和 TToolBar的高分辨率图片很有效。 注意: 此方法可能不适用于所有控件. 注意2: 此代码不支持 Lazrus 1.9+. | |
| 97 | + | ||
| 98 | + | ``` pascal | |
| 99 | + | procedure ScaleImageList(ImgList: TImageList; NewWidth, NewHeight: Integer); | |
| 100 | + | var | |
| 101 | + | TempImgList: TImageList; | |
| 102 | + | TempBmp1: TBitmap; | |
| 103 | + | TempBmp2: TBitmap; | |
| 104 | + | I: Integer; | |
| 105 | + | begin | |
| 106 | + | TempImgList := TImageList.Create(nil); | |
| 107 | + | TempBmp1 := TBitmap.Create; | |
| 108 | + | TempBmp1.PixelFormat := pf32bit; | |
| 109 | + | TempBmp2 := TBitmap.Create; | |
| 110 | + | TempBmp2.PixelFormat := pf32bit; | |
| 111 | + | TempBmp2.SetSize(NewWidth, NewHeight); | |
| 112 | + | try | |
| 113 | + | TempImgList.Width := NewWidth; | |
| 114 | + | TempImgList.Height := NewHeight; | |
| 115 | + | ||
| 116 | + | for I := 0 to ImgList.Count - 1 do begin | |
| 117 | + | // Load image for given index to temporary bitmap | |
| 118 | + | ImgList.GetBitmap(I, TempBmp1); | |
| 119 | + | ||
| 120 | + | // Clear transparent image background | |
| 121 | + | TempBmp2.Canvas.Brush.Style := bsSolid; | |
| 122 | + | TempBmp2.Canvas.Brush.Color := TempBmp2.TransparentColor; | |
| 123 | + | TempBmp2.Canvas.FillRect(0, 0, TempBmp2.Width, TempBmp2.Height); | |
| 124 | + | ||
| 125 | + | // Stretch image to new size | |
| 126 | + | TempBmp2.Canvas.StretchDraw(Rect(0, 0, TempBmp2.Width, TempBmp2.Height), TempBmp1); | |
| 127 | + | TempImgList.Add(TempBmp2, nil); | |
| 128 | + | end; | |
| 129 | + | ||
| 130 | + | ImgList.Assign(TempImgList); | |
| 131 | + | finally | |
| 132 | + | TempImgList.Free; | |
| 133 | + | TempBmp1.Free; | |
| 134 | + | TempBmp2.Free; | |
| 135 | + | end; | |
| 136 | + | end; | |
| 137 | + | ``` | |
| 138 | + | ||
| 139 | + | ## 参考资料 | |
| 140 | + | ||
| 141 | + | - [[doc:lcl//controls/timagelist.html|TImageList doc]] | |
| 142 | + | - [[TImage]] | |
| 143 | + | ||
| 144 | + | --- | |
| 145 | + | *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).* |
