How to load custom cursor in Firemonkey?

I need to use a custom cursor in my Firemonkey work project. I can use LoadCursorFromFile in a VCL project to load a custom cursor in my project. I tried to do the same for Firemonkey, but it doesn’t load the cursor. Is there any working way to load 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; 
+5
source share
2 answers

I did this only for Mac, but the general idea is that you implement your own IFMXCursorService. Keep in mind that this is almost all or nothing fits. You will also have to execute the FMX cursors by default.

 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 may be necessary to add a flag to TWinCursorService so that it does not allow the FMX framework to redefine your cursor.

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

+6
source

Unfortunately, FireMonkey does not support user cursors. This has already been submitted as a function request in the quality portal:

RSP-17651 Unable to load user cursors in Firemonkey .

Based on the above, the code you showed will not work in VCL. LoadCursorFromFile() returns a HCURSOR handle, but the TControl.Cursor property instead calculates the index value from the TCursor enumeration. This is not the same thing. When loading a custom cursor, you must add it to the TScreen.Cursors[] list. This is clearly stated in the documentation:

Vcl.Controls.TControl.Cursor

The cursor value is the cursor index in the list of cursors supported by the global variable, Screen . In addition to the built-in cursors provided by TScreen, applications can add custom cursors to the list .

Vcl.Forms.TScreen.Cursors

Custom cursors can be added to the Cursors property for use by the application or any of its controls. To add a custom cursor to an application, you can: ......
2. Declare the cursor constant with a value that does not contradict the existing cursor constant.
...
4. Set the Cursors property, indexed by the new cursor constant, to the handle received from LoadCursor.

For instance:

 const mycursor: TCursor = 1; // built-in values are <= 0, user-defined values are > 0 procedure Tform1.Button1Click(Sender: TObject); begin Screen.Cursors[mycursor] := LoadCursorFromFile('C:\...\Arrow.cur'); Button1.Cursor := mycursor; end; 
+3
source

Source: https://habr.com/ru/post/1203329/


All Articles