Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Created February 5, 2026 16:06
Show Gist options
  • Select an option

  • Save jpluimers/cc9e48130eadc73556562d9bc24c7c94 to your computer and use it in GitHub Desktop.

Select an option

Save jpluimers/cc9e48130eadc73556562d9bc24c7c94 to your computer and use it in GitHub Desktop.
unit hcTemporaryCursor;
interface
{$I delphi.inc}
uses
{$ifdef FMX}
FMX.Platform
,FMX.Types
,System.UITypes;
{$else}
Controls,
Forms;
{$endif}
// .NET/C# Equivalent: http://wiert.me/2012/01/26/netc-using-idisposable-to-restore-temporary-settrings-example-temporarycursor-class/
type
ITemporaryCursor = interface(IInterface)
['{495ADE0F-EFBE-4A0E-BF37-F1ACCACCE03D}']
end;
TTemporaryCursor = class(TInterfacedObject, ITemporaryCursor)
{$ifdef DELPHI12_UP}strict {$endif} private
{$ifdef FMX}
FCursorService :IFMXCursorService;
{$endif}
FCursor: TCursor;
public
constructor Create(const ACursor: TCursor);
destructor Destroy; override;
class function SetTemporaryCursor(const ACursor: TCursor = crHourGlass): ITemporaryCursor;
end;
implementation
{ TTemporaryCursor }
constructor TTemporaryCursor.Create(const ACursor: TCursor);
begin
inherited Create();
{$ifdef FMX}
if TPlatformServices.Current.SupportsPlatformService(IFMXCursorService) then
begin
FCursorService := TPlatformServices.Current.GetPlatformService(IFMXCursorService) as IFMXCursorService;
FCursor := FCursorService.GetCursor;
FCursorService.SetCursor(ACursor);
end;
{$else}
FCursor := Screen.Cursor;
Screen.Cursor := ACursor;
{$endif}
end;
destructor TTemporaryCursor.Destroy;
begin
{$ifdef FMX}
FCursorService.SetCursor(FCursor);
{$else}
if Assigned(Screen) then
begin
Screen.Cursor := FCursor;
end;
{$endif}
inherited Destroy();
end;
class function TTemporaryCursor.SetTemporaryCursor(const ACursor: TCursor = crHourGlass): ITemporaryCursor;
begin
Result := TTemporaryCursor.Create(ACursor);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment