Delphi - 在运行时使用图标填充图像列表,“破坏”透明度 [英] Delphi - Populate an imagelist with icons at runtime 'destroys' transparency

查看:218
本文介绍了Delphi - 在运行时使用图标填充图像列表,“破坏”透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时,这个(简单的)一个,没有找到解决方案:/



我正在使用D7和TImageList。 ImageList被分配给工具栏。
当在设计时填充ImageList时,图标(部分透明度)看起来很好。
但是我需要在运行时填充它,当我这样做时,这些图标看起来很漂亮 - 完全松散了部分透明度。



我刚刚尝试从.res文件加载图标 - 具有相同的结果。
我也试过第三方图像列表也没有成功。
我没有任何线索我可以做什么:/
感谢2所有;)



编辑:


$ b老实说,我不知道究竟发生了什么。 Alpha混合是相关的术语...
这是两个屏幕:



在设计时添加的图标:
文字http://shs-it.de/tmp/icon-designtime.JPG



在运行时添加的图标:
alt text http://shs-it.de/tmp /icon-runtime.JPG



您的评论不支持Alpha混合,只是带来了解决方案:
我已经编辑了一个图像编辑并删除了alpha混合像素 - 现在看起来不错。
但它仍然奇怪,图标在运行时而不是设计时添加时看起来是另一个。如果你(或其他人)可以解释一下,我会很高兴;)
感谢您的支持!

解决方案

p>要支持Alpha透明度,您需要创建映像列表并在运行时填充它:

  function AddIconFromResource(ImageList:TImageList ; ResID:Integer):整数; 
var
图标:TIcon;
begin
图标:= TIcon.Create;
try
Icon.LoadFromResourceID(HInstance,ResID);
结果:= ImageList.AddIcon(Icon);
finally
Icon.Free;
结束
结束

function AddPngFromResource(ImageList:TImageList; ResID:Integer):Integer;
var
Png:TPngGraphic;
ResStream:TStream;
位图:TBitmap;
begin
ResStream:= nil;
Png:= nil;
位图:= nil;
try
ResStream:= TResourceStream.CreateFromID(HInstance,ResID,RT_RCDATA);
Png:= TPNGGraphic.Create;
Png.LoadFromStream(ResStream);
FreeAndNil(ResStream);
位图:= TBitmap.Create;
Bitmap.Assign(Png);
FreeAndNil(Png);
结果:= ImageList.Add(Bitmap,nil);
finally
Bitmap.Free;
ResStream.Free;
Png.Free;
结束
结束

//这可以是例如在表单或数据模块的OnCreate事件中
begin
//创建imagelist
ImageList:= TImageList.Create(Self);
ImageList.Name:='ImageList';
ImageList.DrawingStyle:= dsTransparent;
ImageList.Handle:= ImageList_Create(ImageList.Width,ImageList.Height,ILC_COLOR32或ILC_MASK,0,ImageList.AllocBy);
//使用资源中的png图像填充imagelist
AddPngFromResource(ImageList,...);
//或图标
AddIconFromResource(ImageList,...);

end;


I've spended hours for this (simple) one and don't find a solution :/

I'm using D7 and the TImageList. The ImageList is assigned to a toolbar. When I populate the ImageList at designtime, the icons (with partial transparency) are looking fine. But I need to populate it at runtime, and when I do this the icons are looking pretty shitty - complete loose of the partial transparency.

I just tried to load the icons from a .res file - with the same result. I've tried third party image lists also without success. I have no clue what I could do :/ Thanks 2 all ;)

edit:

To be honest I dont know exactly whats going on. Alpha blending is the correkt term... Here are 2 screenies:

Icon added at designtime: alt text http://shs-it.de/tmp/icon-designtime.JPG

Icon added at runtime: alt text http://shs-it.de/tmp/icon-runtime.JPG

Your comment that alpha blending is not supported just brought the solution: I've edited the image in an editor and removed the "alpha blended" pixels - and now it looks fine. But its still strange that the icons look other when added at runtime instead of designtime. If you (or somebody else ;) can explain it, I would be happy ;) thanks for you support!

解决方案

To support alpha transparency, you need to create the image list and populate it at runtime:

function AddIconFromResource(ImageList: TImageList; ResID: Integer): Integer;
var
  Icon: TIcon;
begin
  Icon := TIcon.Create;
  try
    Icon.LoadFromResourceID(HInstance, ResID);
    Result := ImageList.AddIcon(Icon);
  finally
    Icon.Free;
  end;
end;

function AddPngFromResource(ImageList: TImageList; ResID: Integer): Integer;
var
  Png: TPngGraphic;
  ResStream: TStream;
  Bitmap: TBitmap;
begin
  ResStream := nil;
  Png := nil;
  Bitmap := nil;
  try
    ResStream := TResourceStream.CreateFromID(HInstance, ResID, RT_RCDATA);
    Png := TPNGGraphic.Create;
    Png.LoadFromStream(ResStream);
    FreeAndNil(ResStream);
    Bitmap := TBitmap.Create;
    Bitmap.Assign(Png);
    FreeAndNil(Png);
    Result := ImageList.Add(Bitmap, nil);              
  finally
    Bitmap.Free;
    ResStream.Free;
    Png.Free;
  end;
end;

// this could be e.g. in the form's or datamodule's OnCreate event
begin
  // create the imagelist
  ImageList := TImageList.Create(Self);
  ImageList.Name := 'ImageList';
  ImageList.DrawingStyle := dsTransparent;
  ImageList.Handle := ImageList_Create(ImageList.Width, ImageList.Height, ILC_COLOR32 or ILC_MASK, 0, ImageList.AllocBy);
  // populate the imagelist with png images from resources
  AddPngFromResource(ImageList, ...);
  // or icons
  AddIconFromResource(ImageList, ...);

end;

这篇关于Delphi - 在运行时使用图标填充图像列表,“破坏”透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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