Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 741
Saeid Avatar asked Sep 24 '14 20:09

Saeid


1 Answers

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

like image 68
Sebastian Z Avatar answered Oct 05 '22 23:10

Sebastian Z