【问题标题】:Imagelist with alpha blend icons loses Transparency带有 alpha 混合图标的图像列表失去透明度
【发布时间】:2012-04-13 18:10:50
【问题描述】:

这是(或多或少)一个相关问题:Delphi - Populate an imagelist with icons at runtime 'destroys' transparency

我已经测试了@TOndrej answer。但似乎我需要启用视觉样式(XP Manifest)才能使其正常工作(将使用 Windows 通用控件的 6.0 版 - 我现在不想要)。我在运行时通过ExtractIconExImageList_AddIcon 添加图标。

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

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

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.

视觉样式禁用

视觉样式启用


一种解决方法是使用插入器类或子类TImageList 并覆盖DoDraw as shown here,但我真正想知道的是如何创建与系统图像列表相同的图像列表。

注意:我知道TPngImageList,不想在这种情况下使用它。


编辑: @David 的回答(和 cmets)是准确的:

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

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

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: A sample code by @David 显示它是如何通过激活上下文 API 正确完成的。

【问题讨论】:

    标签: delphi delphi-7 delphi-5 imagelist common-controls


    【解决方案1】:

    图像列表控件有两个版本。 v5.8版本和v6版本。系统镜像列表是系统拥有的共享组件,使用v6版本。它在任何其他方面都没有什么特别之处,它只是一个普通的 v6 图像列表。在您的应用中,您的图像列表是 v5.8 还是 v6,具体取决于您是否包含清单。但系统拥有的图像列表始终是 v6。

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

    【讨论】:

    • 这很有意义。我什至从未想过系统映像列表可能会在我的进程之外使用不同的版本控制。根据您的回答here 我删除了if IsLibrary then 行,但我不明白如何在我的EXE 中执行此操作。特别是:ActCtx.dwFlags := ACTCTX_FLAG_RESOURCE_NAME_VALID or ACTCTX_FLAG_HMODULE_VALID;ActCtx.lpResourceName := MakeIntResource(2);
    • 我已尝试使用有效清单的ActCtx.lpSourceActCtx.dwFlags 设置为 0。这没有任何区别。图标仍然无效。
    • 您必须显式链接到ImageList_Create,否则它会在模块加载时隐式链接并绑定到 v5.8。我不能说我曾经尝试过这个。这不是一项完全微不足道的工作。您需要在调试器(例如 ms 依赖或进程资源管理器)下进行观察,并确保您正在加载 v6 comctl。
    • 好的,但我是否正确,您不希望您的应用程序使用 v6 comctl32?你只想要一个 v6 图像列表?
    • 我为你把它放在了一个 pastebin 中:pastebin.com/dvMiGJ78 我对此有点怀疑,因为当你获得图像列表句柄时,未来使用它的 API 调用将被发送到5.8 动态链接库。但是,它似乎确实适用于您的测试应用程序。祝你好运。
    猜你喜欢
    • 2015-02-03
    • 2015-11-04
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2016-12-29
    • 2019-05-18
    相关资源
    最近更新 更多