简体   繁体   English

用于OSX的Delphi任务栏图标(NSStatusItem)

[英]Delphi tray icon (NSStatusItem) for OSX

I am trying to add NSStatusItem in a Delphi application for OSX. 我试图在OS X的Delphi应用程序中添加NSStatusItem。 Searched for sample code to help me with that but got stuck when defining an interface: 搜索示例代码以帮助我解决此问题,但在定义接口时陷入困境:

Here is the code: 这是代码:

// Source: https://forums.embarcadero.com/thread.jspa?threadID=108449

unit Unit2;

interface

uses Macapi.ObjectiveC, Macapi.CocoaTypes, Macapi.Foundation, Macapi.AppKit,
Macapi.Helpers, Macapi.ObjcRuntime, System.TypInfo, FMX.Platform, FMX.Platform.Mac;

type
TFMXTrayItem = class(TOCLocal)
private
NSStatItem : NSStatusItem;
public
constructor Create;
destructor Destroy; override;
function GetObjectiveCClass: PTypeInfo; override;
procedure call_mymethod; cdecl;
end;

implementation

constructor TFMXTrayItem.Create;
var
NSContMenu : NSMenu;
NSContItem : NSMenuItem;
NSStatBar : NSStatusBar;
NSImg : NSImage;
AppBundle : NSBundle;
NSpImg: Pointer;
Path: String;
begin
inherited Create;

NSStatBar := TNSStatusBar.Create;
NSStatBar := TNSStatusBar.Wrap(TNSStatusBar.OCClass.systemStatusBar);
NSStatItem:= NSStatBar.statusItemWithLength(NSVariableStatusItemLength);
NSStatItem.setTarget(GetObjectID);

// Create context menu
NSContMenu := TNSMenu.Create;
NSContMenu := TNSMenu.Wrap(NSContMenu.initWithTitle(StrToNSStr('The caption')));

NSContItem:=TNSMenuItem.Create;
NSContItem:=TNSMenuItem.Wrap(NSContItem.initWithTitle(StrToNSStr('1. menuitem'),sel_getUid(PAnsiChar('call_mymethod')),StrToNSStr('')));
NSContItem.setTarget(GetObjectID);
NSContMenu.addItem(NSContItem);
NSContItem.release;


// Add menu
NSStatItem.retain;
NSStatItem.setHighlightMode(true);
NSStatItem.setMenu(NSContMenu);
NSContMenu.release;

// Get path to dir
AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
Path:=AppBundle.bundlePath.UTF8String+'/Contents/yourimage16x16.png';
NSpImg := TNSImage.Alloc.initWithContentsOfFile(StrToNSStr(Path));
// Create Icon
NSImg := TNSImage.Create;
NSImg := TNSImage.Wrap(NSpImg);
NSStatItem.setImage(NSImg);
NSImg.release;
end;

destructor TFMXTrayItem.Destroy;
begin
NSStatItem.release;
inherited;
end;

function TFMXTrayItem.GetObjectiveCClass: PTypeInfo;
begin
Result :=TypeInfo(IFMXTrayItem);
end;

procedure TFMXTrayItem.call_properties;
begin
// your event code of the menu item
end;

end.

Does anyone have any idea on how to declare the IFMXTrayItem interface? 是否有人对如何声明IFMXTrayItem接口有任何想法?

Got it to work like this: 使它像这样工作:

type

IFMXTrayItem = interface(NSObject)
['{7d2e4b38-61d9-4cf4-b78b-5f7c4188e9c0}']
  procedure call_mymethod; cdecl;
end;

later edit: 以后编辑:

Added a GUID to the interface after reading this : 阅读以下内容后,在界面中添加了GUID:

This GUID is used by the compiler to identify uniquely this interface. 编译器使用此GUID唯一标识此接口。 Strictly speaking, you can use an interface without the GUID, but you can't get very far using them as much of the RTL and most frameworks that take advantage of interfaces will require that they have a GUID. 严格来说,您可以在没有GUID的情况下使用接口,但由于使用了太多的RTL,因此使用它们的距离不会太远,而大多数利用接口的框架都要求它们具有GUID。

So that is a random GUID I generated but if you use this in your code you should generate your own GUID. 因此,这是我生成的随机GUID,但是如果您在代码中使用此GUID,则应该生成自己的GUID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM