Vcl Styles issue for menu items added during program load

712 views Asked by At

I have a Delphi 10.4.2 program (32-bit) where menu items are added during program load (the Application.OnActivate event, coded to run only once). Without a vcl style the new items are displayed correctly, however when a style is applied (such as the very nice Iceberg Classico in the screenshot) the display is not correct. The menu options are there, and can be clicked on; but the text and the icon are not drawn.

screenshot showing blank menu options

Any workrounds? I'm assuming that it’s because those particular menu options are added after the style is applied. Is there a way to refresh the style?, or am I missing a setup property when creating the menu items?

Thanks.

Edit: Yes, the 'File' menu and sub menu items are displayed correctly. Code that creates the new menu and items (simplified) is:

procedure TDbHelper.CreateHelpMenu;
// Called by OnApplicationActivated event, and run just once 
var
   aMenu: TMainMenu;
   mnHelp, mnItem: TMenuItem;
   idx: Integer;
begin
   aMenu := Application.MainForm.Menu;
   // create new menu
   mnHelp         := aMenu.CreateMenuItem;
   mnHelp.Name    := 'WISHelp1';               
   mnHelp.Caption := 'WIS Help';
   aMenu.Items.Add(mnHelp);
   // now the submenu items
   for idx := 0 to HelpLinks.Count - 1 do
   begin
      mnItem := TMenuItem.Create(mnHelp);
      mnItem.Name := HelpLinks[idx].Key;
      mnItem.Caption := HelpLinks[idx].Text;
      mnItem.ImageIndex := HelpLinks[idx].ImageIndex;                            
      mnItem.OnClick := WISHelpItemClick;
      mnHelp.Add(mnItem);
   end;
end;
2

There are 2 answers

1
PhilW On BEST ANSWER

Finally decided to switch off the vcl styles for the menus. I followed the advice of RRUZ on another question and added a line to the dpr source so that it became:

  Application.Initialize;
  TStyleManager.TrySetStyle('Iceberg Classico');
  with TStyleManager do SystemHooks := SystemHooks - [shMenus];
  Application.Title :=  'blah, blah, etc'

The menu items have re-appeared, and they look fine:

screenshot showing unstyled menu options

Thank you to SilverWarior for their input and suggestions.

3
SilverWarior On

Tried to recreate this scenario in Delphi 10.3 and it works fine for me.

But then with some fiddling I managed to recreate your "end result". And in order to do so I had to pass empty strings for mnItem.Name and mnItem.caption.

So I believe that the problem you are facing is not by using VCL Styles but in fact by your HelpLinks[idx].Key and HelpLinks[idx].Text methods returning empty strings. So you end up with menu items with no name and no caption therefore it appears as they are rendered wrong.

If I'm correct disabling VCL styles will still have same result.