如何添加在OSX上按预期工作的菜单项分隔符? [英] How to add menu items separators which work as expected on OSX?
问题描述
在 Windows 平台上,使用 VCL ,当我们要在菜单中添加分隔符时,我们添加一个 TMenuItem
与 Caption:=' - '
;
On Windows platform, with the VCL, when we want to add a separator in a menu, we add a TMenuItem
with a Caption := '-'
;
使用 FireMonkey ,我们添加一个 TMenuItem
与 Text:=' - '
;
With FireMonkey, we add a TMenuItem
with a Text := '-'
;
它在Windows平台上的预期工作,具有Text =' - '的项目显示为分隔符。
It works as expected on Windows platform, the item with the Text='-' is displayed as a separator.
但是当我在 OSX 上运行相同的应用程序时,我有减号可见...
But, when I run the same application on OSX, I have the minus sign visible...
我没有找到任何属性 TMenuItem
指定它是一个分隔符 ...
I haven't found any property on the TMenuItem
to specify it is a separator...
我尝试过 TMainMenu
和 TMenuBar
( UseOSMenu:= True | False;
),我仍然有这个问题。
I have tried with a TMainMenu
and a TMenuBar
(UseOSMenu := True|False;
) and I still have this issue.
创建一个真正的分隔符的想法(否则,我将检查操作系统并将其删除如果OSX ...)
Any idea to create a real separator? (otherwise, I will check the OS and remove it if OSX...)
推荐答案
这是FireMonkey中的一个错误。我相信他们会解决的。但同时你可以使用下面的代码。在主窗体的OnActivate事件中调用FixSeparatorItemsForMac。
This is a bug in FireMonkey. I am sure they will solve it. But meanwhile you can use the below code. Call the procedure FixSeparatorItemsForMac in the OnActivate event of your main form.
不要忘记使用列表中的mac特定文件。
Dont forget mac specific files in the uses list.
uses
...
{$IFDEF MACOS}
,Macapi.ObjectiveC,MacApi.AppKit,MacApi.Foundation,FMX.Platform.Mac
{$ENDIF}
{$IFDEF MACOS}
Procedure FixSeparatorItemsForMenuItem(MenuItem:NSMenuItem);
var i:Integer;
subItem:NSMenuItem;
begin
if (MenuItem.hasSubmenu = false) then exit;
for i := 0 to MenuItem.submenu.itemArray.count -1 do
begin
subItem := MenuItem.submenu.itemAtIndex(i);
if (subItem.title.isEqualToString(NSSTR('-'))= true) then
begin
MenuItem.submenu.removeItemAtIndex(i);
MenuItem.submenu.insertItem(TNSMenuItem.Wrap(TNSMenuItem.OCClass.separatorItem),i);
end else begin
FixSeparatorItemsForMenuItem(subItem);
end;
end;
end;
Procedure FixSeparatorItemsForMac;
var NSApp:NSApplication;
MainMenu:NSMenu;
AppItem: NSMenuItem;
i: Integer;
begin
NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
MainMenu := NSApp.mainMenu;
if (MainMenu <> nil) then
begin
for i := 0 to MainMenu.itemArray.count -1 do
begin
AppItem := mainMenu.itemAtIndex(i);
FixSeparatorItemsForMenuItem(AppItem);
end;
end;
end;
{$ENDIF}
这篇关于如何添加在OSX上按预期工作的菜单项分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!