1

I'm using the script here to add some context menu to my Inno Setup pages:
Adding context menu to Inno Setup page

is there any way to add an icon image to every menu item?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • I think it was mentioned that you use `SetMenuItemBitmaps`. In your linked answer you have examples of how the MFC API methods were transferred to Delphi. The definition for that API here here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setmenuitembitmaps – Andrew Truckle Sep 28 '20 at 08:42

1 Answers1

3

Use 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, 0, 0,
    LR_LOADFROMFILE or LR_CREATEDIBSECTION);
  SetMenuItemBitmaps(Menu, Position, MF_BYPOSITION, Bitmap, Bitmap);
end;

Use the AddMenuItem instead of InsertMenu calls in the code from Adding context menu to Inno Setup page:

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

The above obviously assumes, that you have the transparent bitmap images added to the installer:

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

I've used PixelFormer to create the transparent .bmp images.

enter image description here

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992