delphifiremonkeymouse-cursor

How to load custom cursor in 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;

Solution

  • 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;
    

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

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