Mega Code Archive

 
Categories / Delphi / Graphic
 

How to create a animated (rotating) hourglass

Title: How to create a animated (rotating) hourglass. Question: when you have a prolonged job you can show a animated (rotating) hourglass as cursor. Here is the way how to do it. Answer: If your program has a prolonged job, you can show an hourglass in the meantime. When the job takes a very long time the user may still think the computer has given up. If it isn't possible to use a Progressbar, you can use a rotating hourglass. Therefore Windows has a file named hourglas.ani that you must load at the startup of your program. Pay attention that that the name is in the Dos 8.3-format, in which 'hourglas' is with one 's'. This code snippet shows how to load the cursor in the OnCreate of your main-form: procedure TForm1.FormCreate(Sender: TObject); begin Screen.Cursors[1]:= LoadCursorFromFile('C:\windows\cursors\hourglas.ani'); end; Instead of using a 1 between the '['-brackets you can use any positive number (negative numbers and zero are used by Windows). For instance you use this number like: procedure TForm1.Button1Click(Sender: TObject); begin Screen.Cursor:= 1; //long time job Screen.Cursor:= crDefault; end; If you have NT instead of Windows, the rotating hourglass will be in 'C:\winnt\cursors\hourglas.ani'. If you have a program that is used by NT as well as Windows, it will be smart if the cursor is looked up in the right directory. This can be done as followed: procedure TForm1.FormCreate(Sender: TObject); var sPath: array[0..MAX_PATH-1] of Char; P: PChar; begin GetWindowsDirectory(sPath, SizeOf(sPath)); P:= PChar(IncludeTrailingBackslash(sPath) + 'cursors\hourglas.ani'); Screen.Cursors[1]:= LoadCursorFromFile(P); end; The function IncludeTrailingBackslash exists since Delphi 5. If you have an earlier version you must write some code yourself. If you have Delphi 6 you can use IncludeTrailingPathDelimiter which is compatible with Kylix.