Site hosted by Angelfire.com: Build your free website today!
 

Delphi - Source Code - Controlling the Cursor (HourGlass correctly)


Function : f_set_cursor


The f_set_cursor function enhances the changing of the Screen's cursor property.  I wrote the function to return the previous cursor as well as the automatically yield to the operating system and allow other processes a time slice if the new or the previous cursor was a hourglass.

It make seem stupid to give up time to the operating system because it will make your application perform slower, but in reality it will make the application seem more "cooperative".  This is a long time customer satisfaction thing that isn't easily measured.  This code will allow the app to appear to run more smoothly because pending screen updates will be completed, disk writes will completed, ...

Caveats :


Source Code :

FUNCTION TfrmGlobal.f_set_cursor( newCursor : TCursor ) : TCursor;

Var
   lcur_old_cursor : TCursor;

Begin
   (* Don't bother formally setting the script name so as not to log it *)
   gErrorEvent := 'TfrmGlobal.f_set_cursor';

   (* Get the "old" cursor *)
   lcur_old_cursor := Screen.Cursor;

   (* If the new cursor is an HourGlass, change to it, and ProcessMessages *)
   If (newCursor = crHourGlass) Then
      Begin
         Screen.Cursor := newCursor;
         Application.ProcessMessages;
      End
   (* If the "current" cursor is an HourGlass, ProcessMessages, and then change *)
   Else If (lcur_old_cursor = crHourGlass) Then
      Begin
         Application.ProcessMessages;
         Screen.Cursor := newCursor;
      End
   (* Change to the new cursor *)
   Else
      Screen.Cursor := newCursor;

   (* Return the "old" cursor *)
   Result := lcur_old_cursor;
End;


Example Usage :

Var
   lcur_temp          : TCursor;

Begin
   ...
   lcur_temp := frmGlobal.f_set_cursor(crHourGlass);
   Try
      ...
   Finally
      ...
      frmGlobal.f_set_cursor(lcur_temp);
   End;
End;