I want to add a bitmap to a TMenuItem created dynamically. With this code it doesn't work, I don't have the image on my menu. What's wrong?
procedure TForm3.FormCreate(Sender: TObject);
var
item : TmenuItem;
icon : TIcon;
begin
item := TMenuItem.Create(PopupMenu1);
item.Caption := 'coucou';
icon := TIcon.Create;
icon.LoadFromFile('d:\SmallIcon.ico');
icon.Height := 16;
icon.Width := 16;
item.Bitmap.Canvas.Draw(0,0,icon);
PopupMenu1.Items.Add(item);
end;
The Bitmap
property on TMenuItem
isn't the way to go here. You really should use image lists instead. This will allow you to share images between your UI elements in a manageable fashion.
TImageList
.Images
property on the menu (i.e. PopupMenu1
) to refer to the image list.Of course, you really ought to be using actions too, in which case you simply need to set the ImageIndex
for the action and the framework takes care of assigning it to the menu item.
As an aside, I would note that the Delphi implementation of Vista themed menus has a large number of subtle bugs, many related to drawing of images. However, these bugs are relatively minor in visual impact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With