首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有alpha混合图标的图像列表失去了透明度

带有alpha混合图标的图像列表失去了透明度
EN

Stack Overflow用户
提问于 2012-03-30 03:11:58
回答 1查看 4K关注 0票数 5

这里(或多或少)有一个相关的问题:Delphi -在运行时用图标填充图像列表“破坏”透明度。

我测试了@TOndrej 回答。但我似乎需要启用可视化样式(XP Manifest)才能工作( Windows公共控件的6.0版将被使用--我现在不想这么做)。我通过ExtractIconExImageList_AddIcon在运行时添加图标。

显然,将ImageList.Handle设置为使用系统图像列表句柄,不需要XP .因此,即使是我在D3中回写的一个旧程序,当我使用系统映像列表显示文件列表(带有TListView)时,也会正确地显示α混合图标。

我在徘徊,系统图像列表有什么特别之处,以及它是如何创建的,以便在所有情况下都支持alpha混合?--我搞不清楚。下面是一些示例代码:

代码语言:javascript
复制
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ImgList, StdCtrls, ShellAPI, ExtCtrls, Commctrl;

type
  TForm1 = class(TForm)
    ImageList1: TImageList;
    PopupMenu1: TPopupMenu;
    MenuItem1: TMenuItem;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FileName: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
// {$R WindowsXP.res}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PopupMenu1.Images := ImageList1;
  FileName := 'C:\Program Files\Mozilla Firefox\firefox.exe';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  IconPath: string;
  IconIndex: Integer;
  hIconLarge, hIconSmall: HICON;
begin
  IconPath := FileName;
  IconIndex := 0; // index can be other than 0

  ExtractIconEx(PChar(IconPath), IconIndex, hIconLarge, hIconSmall, 1);

  Self.Refresh; // erase form
  DrawIconEx(Canvas.Handle, 10, 10, hIconSmall, 0, 16, 16, 0,
    DI_IMAGE or DI_MASK); // this will draw ok on the form

  // ImageList1.DrawingStyle := dsTransparent;
  ImageList1.Handle := ImageList_Create(ImageList1.Width, ImageList1.Height,
    {ILC_COLORDDB} ILC_COLOR32 or ILC_MASK, 0, ImageList1.AllocBy);
  ImageList_AddIcon(ImageList1.Handle, hIconSmall);

  MenuItem1.ImageIndex := 0;

  DestroyIcon(hIconSmall);
  DestroyIcon(hIconLarge);

  PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

procedure TForm1.Button2Click(Sender: TObject);
// using sys image-list will work with or without Manifest
type
  DWORD_PTR = DWORD;
var
  ShFileINfo :TShFileInfo;
  SysImageList: DWORD_PTR;
  FileName: string;
begin
  SysImageList := ShGetFileInfo(nil, 0, ShFileInfo, SizeOf(ShFileInfo),
    SHGFI_SYSICONINDEX OR SHGFI_SMALLICON);

  if SysImageList = 0 then Exit;
  ImageList1.Handle := SysImageList;
  ImageList1.ShareImages := True;

  if ShGetFileInfo(PChar(FileName), 0, ShFileInfo, SizeOf(ShFileInfo),
    SHGFI_SYSICONINDEX OR SHGFI_ICON OR SHGFI_SMALLICON) <> 0 then
  begin
    MenuItem1.ImageIndex := ShFileInfo.IIcon;
    Self.Refresh; // erase form
    DrawIconEx(Canvas.Handle, 10, 10, ShFileInfo.hIcon, 0, 16, 16, 0,
      DI_IMAGE or DI_MASK);
    DestroyIcon(ShFileInfo.hIcon); // todo: do I need to destroy here? 

    PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
  end;      
end;

end.

视觉样式禁用

可视化样式启用

解决方法是使用interposer类或子类TImageList并重写DoDraw 如图所示,但我真正想知道的是如何创建与系统图像列表相同的图像列表。

注意:我了解TPngImageList,不想在本例中使用它。

编辑:@David的回答(和评论)是正确的:

您必须显式地链接到ImageList_Create (v6),因为否则它将在模块加载时被隐式链接,并且将绑定到v5.8

示例代码(不使用激活上下文API):

代码语言:javascript
复制
function ImageList_Create_V6(CX, CY: Integer; Flags: UINT; Initial, Grow: Integer): HIMAGELIST;
var
  h: HMODULE;
  _ImageList_Create: function(CX, CY: Integer; Flags: UINT;
    Initial, Grow: Integer): HIMAGELIST; stdcall;
begin
  // TODO: find comctl32.dll v6 path programmatically
  h := LoadLibrary('C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll');
  if h <> 0 then
  try
    _ImageList_Create := GetProcAddress(h, 'ImageList_Create');
    if Assigned(_ImageList_Create) then
      Result := _ImageList_Create(CX, CY, Flags, Initial, Grow);
  finally
    FreeLibrary(h);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ...
  ImageList1.Handle := ImageList_Create_V6(ImageList1.Width, ImageList1.Height,
    ILC_COLOR32 or ILC_MASK, 0, ImageList1.AllocBy);
  ...
end;

Edi2: @David的示例代码,它展示了如何通过Activation正确地完成它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-30 06:42:44

图像列表控件有两个版本。v5.8版本和v6版本。系统映像列表是系统拥有的共享共体,并使用v6版本。它在任何其他方面都没有什么特别之处,它只是一个普通的v6图像列表。在您的应用程序中,您的图像列表要么是v5.8,要么是v6,这取决于您是否包括清单。但是系统拥有的图像列表总是v6。

我不知道你为什么不想在你的应用程序中使用v6通用控件。但是,有了这个约束,您就可以在创建映像列表时使用激活上下文API在本地使用v6公共控件。这将解决您的问题,并让您的应用程序的其余部分与v5.8通用控件。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9936518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档