Put TMainMenu on the form and attach this handler to some TMenuItem. It will hide the main menu, but not if Custom Vcl Style is active.
procedure TForm1.HideMenuClick(Sender: TObject);
begin
Menu := nil;
{ raised exception class $C0000005 with message 'access violation at 0x0038f397: read of address 0x00000008'. }
end;
This will work, but only if the delay is large enough:
procedure TForm1.HideMenuClick(Sender: TObject);
begin
TThread.ForceQueue(nil, procedure
begin
Menu := nil
end,
20);
end;
Is there a clean and reliable way to do this?
(Note: The question is only about TMainMenu and its OnClick events, not about other ways to remove the menu or other controls.)
Edit:
Use the mouse to reproduce the problem. If you use the keyboard to open the menu and activate the item, then single 'ForceQueue(nil, procedure begin Menu := nil; end)' will be enough to avoid access violation error. If the menu item is activated with a keyboard shortcut, then even simple 'Menu := nil' will work.