Instantly share code, notes, and snippets.

Embed
What would you like to do?
#delphi helper class to work with cursor
//MMWIN:CLASSCOPY
unit uLazyCursorHelper;
interface
uses
Controls;
type
/// <summary>
/// Changes screen cursor when method Change is called. And automatically restores it back.
/// </summary>
/// <remarks>
/// F.e.: TLazyCursorHelper.Change(crHourGlass);
/// </remarks>
TLazyCursorHelper = class(TInterfacedObject, IUnknown)
strict private
FOldCursor: TCursor;<><>
public
class function Change(const aNewCursor: TCursor): IUnknown;
class function ChangeToHourglass: IUnknown;
class function ChangeToSqlWait: IUnknown;
destructor Destroy; override;
end;
implementation
uses
Forms;
class function TLazyCursorHelper.ChangeToHourglass: IUnknown;
begin
result := Change(crHourGlass);
end;
class function TLazyCursorHelper.ChangeToSqlWait: IUnknown;
begin
result := Change(crSQLWait);
end;
destructor TLazyCursorHelper.Destroy;
begin
Screen.Cursor := FOldCursor;
inherited;
end;
class function TLazyCursorHelper.Change(const aNewCursor: TCursor): IUnknown;
var
tmpInstance: TLazyCursorHelper;
begin
tmpInstance := TLazyCursorHelper.Create;
tmpInstance.FOldCursor := Screen.Cursor;
Result := tmpInstance;
Screen.Cursor := aNewCursor;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment