I have multiplatform FMX application for Windows and OSX. For windows, I want to use a TMenubar for the menus but for OSX I want to use a TMainMenu. So I design everything with a TMainMenu and for windows, when I create the Main form, I move the menu items from MainMenu to the MenuBar with the code below: All works great except the OnClick events for the top items do not work. The menu drills down and everything below works. I use the OnClick to trigger some enabling/disabling of the menu items below it. I traced this back all the way to FMX.Platform.Win and it seems that it is not getting WM_LBUTTONUP events. Oddly, I do get WM_RBUTTONUP that will call the OnClick. If I reassign another procedure to the same OnClick event it does not work. If I leave every thing on the TMainMenu it works correctly.
Thougths?
procedure TPartViewer.CopyMenu(MenuBar: TMenuBar);
var
smi: TMenuItem;
begin
while MainMenu1.ItemsCount > 0 do begin
smi := TMenuItem(MainMenu1.items[0]);
MenuBar.AddObject(smi);
end;
end;
(Improved below)
It seems that the missing event is MouseUp. Without the MouseUp you also never get the OnClick. But the MouseDown works. While this is not a publish property it is public. As such as I run thru the top menu items and check to see if it there is an OnClick event. If so, I transform this to a MouseDown event.
//tiny component to hold event.
type
TEventHolder = class(TComponent)
public
Notify: TNotifyEvent;
end;
// transformation event
procedure TPartViewer.FixMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
TEventHolder(TMenuItem(Sender).TagObject).Notify(sender);
end;
procedure TPartViewer.CopyMenu(MenuBar: TMenuBar);
var
smi: TMenuItem;
holder: TEventHolder;
begin
while MainMenu1.ItemsCount > 0 do begin
smi := TMenuItem(MainMenu1.items[0]);
if assigned(smi.OnClick) then begin
smi.OnMouseDown := FixMouseDown;
holder := TEventHolder.Create(self);
holder.Notify := smi.OnClick;
smi.TagObject := holder;
// nuke original just in case it gets fix later
// avoiding double calls
smi.OnClick := nil;
end;
MenuBar.AddObject(smi);
end;
end;
self.Menu:= nil;