在Inno Setup中的上下文菜单中添加图标图像

问题描述

在这里使用脚本在Inno Setup页面添加一些上下文菜单
Adding context menu to Inno Setup page

是否可以将图标图像添加到每个菜单项?

解决方法

使用SetMenuItemBitmaps

[Code]
const
  IMAGE_BITMAP = 0;
  LR_LOADFROMFILE = $10;
  LR_CREATEDIBSECTION = $2000;

function LoadImage(
  hInst: Integer; ImageName: string; ImageType: UINT; X,Y: Integer;
  Flags: UINT): THandle; external 'LoadImageW@User32.dll stdcall';  
function SetMenuItemBitmaps(
  hMenu: THandle; uPosition: Cardinal; uFlags: Cardinal;
  hBitmapUnchecked: THandle; hBitmapChecked: THandle): Boolean;
  external 'SetMenuItemBitmaps@User32.dll stdcall';

procedure AddMenuItem(
  Menu: THandle; Position: Integer; ID: Integer; Caption: string;
  ImageFileName: string);
var
  Bitmap: THandle;
begin
  InsertMenu(Menu,Position,MF_BYPOSITION or MF_STRING,ID,Caption);
  ExtractTemporaryFile(ImageFileName);
  Bitmap := LoadImage(
    0,ExpandConstant('{tmp}\') + ImageFileName,IMAGE_BITMAP,LR_LOADFROMFILE or LR_CREATEDIBSECTION);
  SetMenuItemBitmaps(Menu,MF_BYPOSITION,Bitmap,Bitmap);
end;

Adding context menu to Inno Setup page中的代码中使用AddMenuItem而不是InsertMenu调用:

AddMenuItem(PopupMenu,ID_MUTE,'Mute','mute.bmp');
AddMenuItem(PopupMenu,1,ID_STOP,'Stop','stop.bmp');

以上显然假定您已将透明位图图像添加到安装程序中:

[Files]
Source: "mute.bmp"; Flags: dontcopy
Source: "stop.bmp"; Flags: dontcopy

我已经使用PixelFormer来创建透明的.bmp图像。

enter image description here