0

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;
5
  • Since one can only add components in the master you have to have both a MenuBar and MainMenu loaded and ready in the master? I can hide the Toolbar in OSX but MainMenu can not be hidden. I expect I can free it or delete all its items at runtime?
    – Bill F
    Jun 7 at 19:17
  • You should be able to unset a form's menu: self.Menu:= nil;
    – AmigoJack
    Jun 7 at 22:33
  • "one can only add components in the master" - Oh, I forgot about that. I don't do mobile development. My suggestion still stands, though. Don't move the menu items around. Let each menu have its own items, just make them share event handlers. Then you can freeing hide/destroy each menu without affecting the other. Jun 7 at 23:07
  • self.Menu := nil does not work on FMX but MainMenu.Free does. It still seem like a kluge as you have to maintain two menu systems. Instead of moving them, I can copy them at runtime but then references in code to them by name become invalid. When I free MainMenu after copying them.
    – Bill F
    Jun 8 at 3:45
  • I added an improved copy routine to move menu items that "fixes" the missing OnClick events.
    – Bill F
    Jun 8 at 5:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.