⬆️ ⬇️

Little tricks: automatic recovery of the cursor pointer view

Good afternoon, colleagues!



Those of you who write client applications are probably faced with the need to change the type of cursor in order to show the user that the application is currently performing some kind of data processing (long-term or not) or performs a query to the database. I want to share a little trick, how to simplify your life. Details under the cut.



You will still have to change the appearance of the cursor yourself, but you can automatically restore the appearance of the cursor. To do this, here is the code:



type ICursorSaver = interface end; TCursorSaver = class(TInterfacedObject, ICursorSaver) private FCursor: TCursor; public constructor Create; destructor Destroy; override; end; implementation constructor TCursorSaver.Create; begin FCursor := Screen.Cursor; end; destructor TCursorSaver.Destroy; begin Screen.Cursor := FCursor; inherited; end; 


')

Next, in the right place of the code, we declare a variable of type ICursorSaver and initialize it.



 var saveCursor: ICursorSaver; begin saveCursor := TCursorSaver.Create; Screen.Cursor := crSQLWait; //   ,    end; 




How it works? TInterfacedObject keeps track of references to the interface, when the counter drops to zero - the destructor is called. At the beginning of the scope, we create an object and initialize the interface variable to it, and the current view of the cursor is captured. At the end of the scope, the interface variable is destroyed, the interface is released, the destructor returns the cursor to its original state.



This method can be used to save the state of not only the cursor, but also the state of any other objects - only in this case you need to make a deep copy of the object.



UPD: Colleagues romik and koreec suggest setting the type of cursor directly in the constructor. Then the constructor will look like this:



 constructor TCursorSaver.Create(ACursor: TCursor = crHourGlass); begin FCursor := Screen.Cursor; Screen.Cursor := ACursor; end; 

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



All Articles