简体   繁体   中英

How to load a custom cursor from file in Firemonkey to be used on a Mac app?

I want to load a custom cursor from a file (or any other way) to be used on my FireMonkey application. I know I want to lead with IFMXCursorService as described here and write my own class which implements IFMXCursorService interface: SetCursor/GetCursor. The problem is that I want to build the app for Mac so I can't use any call to Windows API like all the examples I have found do (LoadCursorFromFile, etc.). So:

1) Is there a way to load a cursor from file without using Windows API?

2) If there is a way: Can the implementation be "adjusted" using compiler directives to have the Windows and Mac implementation on the same unit so that I can build the app for Windows and Mac respectively ?

PS: I'm using Delphi 10.2.3 (Tokyo)

1) On macOS? Absolutely:

function LoadNSImage(const AFileName: string): NSImage;
var
  LStream: TMemoryStream;
  LData: NSData;
begin
  LStream := TMemoryStream.Create;
  try
    LStream.LoadFromFile(AFileName);
    LData := TNSData.Wrap(TNSData.OCClass.dataWithBytes(LStream.Memory, LStream.size));
    Result := TNSImage.Wrap(TNSImage.Create.initWithData(Data));
  finally
    LStream.Free;
  end;
end;

Use the resulting NSImage in an NSCursor that calls initWithImage:

https://developer.apple.com/documentation/appkit/nscursor/1524612-initwithimage?language=objc

2) There's no other way to support multiple platforms than to use directives that determine which code is compiled. In this case, you'd need to have a class for each platform that implements IFMXCursorService, and you'd need to "override" Delphi's default implementation for platforms where you need to use your own by calling TPlatformServices.Current.RemovePlatformService to remove the existing implementation and the corresponding AddPlatformService method to add your own.

Not going to go into more detail here about 1) or 2); you should really write separate questions for each issue you encounter, after doing some research and trying it yourself first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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