如何在Firemonkey中加载自定义光标?

问题描述:

我需要在Firemonkey桌面项目中使用自定义光标。
我可以在VCL项目中使用LoadCursorFromFile在项目中加载自定义光标。
我曾尝试对Firemonkey执行相同的操作,但是它没有加载光标。
是否有任何可行的方法来实现在Firemonkey中加载自定义光标?

I need to use custom cursor in my Firemonkey desktop project. I can use LoadCursorFromFile in VCL project to load a custom cursor in my project. I have tried to do the same for Firemonkey but it is not loading the cursor. Is there any working way to achieve loading custom cursors in Firemonkey?

uses Winapi.Windows;

procedure Tform1.Button1Click(Sender: TObject);
const mycursor= 1;
begin
  Screen.Cursors[mycursor] := LoadCursorFromFile('C:\...\Arrow.cur');
  Button1.Cursor := mycursor;
end;


我只在Mac上这样做,想法是您实现自己的IFMXCursorService。请记住,这几乎是一种全有或全无的方法。您也必须实现默认的FMX光标。

I only did this for the Mac, but the general idea is that you implement your own IFMXCursorService. Keep in mind that this pretty much an all or nothing approach. You'll have to implement the default FMX cursors, too.

type
  TWinCursorService = class(TInterfacedObject, IFMXCursorService)
  private
    class var FWinCursorService: TWinCursorService;
  public
    class constructor Create;
    procedure SetCursor(const ACursor: TCursor);
    function GetCursor: TCursor;
  end;

{ TWinCursorService }

class constructor TWinCursorService.Create;
begin
  FWinCursorService := TWinCursorService.Create;
  TPlatformServices.Current.RemovePlatformService(IFMXCursorService);
  TPlatformServices.Current.AddPlatformService(IFMXCursorService, FWinCursorService);
end;

function TWinCursorService.GetCursor: TCursor;
begin
  // to be implemented
end;

procedure TWinCursorService.SetCursor(const ACursor: TCursor);
begin
  Windows.SetCursor(Cursors[ACursor]); // you need to manage the Cursors list that contains the handles for all cursors
end;

可能有必要在TWinCursorService中添加一个标志,以防止FMX框架

It might be a necessary to add a flag to the TWinCursorService so that it will prevent the FMX framework to override your cursor.

注册您自己的光标服务时,计时很重要。在FMX调用TPlatformServices.Current.AddPlatformService(IFMXCursorService,PlatformCocoa)之后,必须完成此操作。

Timing is important when registering your own cursor service. It will have to be done after FMX calls TPlatformServices.Current.AddPlatformService(IFMXCursorService, PlatformCocoa);