View RSS Feed

Development Team Blog

The Boring Cursor_Wait

Rate this Entry
If the program you write starts a task of which you know it will take some time it is important to indicate this to the user. You can do this by making use of the cursor_control object. The cursor_control object is an instance of the CursorShape class and created if you add:
Use DfCursor.Pkg
to your code.

Once the object is instantiated you can send the message cursor_wait to the object and you will see the mouse cursor changed from the I-beam or the arrow to a hourglass or whatever icon is used in your set of cursors which you define in Windows. Once the routine is ready you send the message cursor_ready to the cursor_control object and the original mouse cursor is restored.

The cursor_control object supports more. Behind the messages cursor_wait and cursor_ready is a message called Set_Cursor which captures or releases the mouse and can load more cursor types. The cursor types are all numeric resource numbers defined in cursor.pkg.

With a simple addition in code you can load cursors from a file. Often these are animated cursors these days and I found a nice one for this time of the year which is a rotating easter egg Click image for larger version. 

Name:	2011-04-22_085434.jpg 
Views:	242 
Size:	40.2 KB 
ID:	4272 at To load the easter egg (or any other cursor) add the following routine to the cursor_control object.

Procedure Set psFileCursor String sCursorFileName
    Integer iCursorID iRetval 
    Handle hWnd hClassCursor
    Boolean bExists
    Move (DoesFileExist (sCursorFileName)) to bExists
    If (not (bExists)) Begin
        Error DFERR_PROGRAM (SFormat ("Cursor file %1 not found", sCursorFileName))

    Send Page_Object True
    Get Window_Handle to hWnd

    Get ClassCursorHandle to hClassCursor
    If (hClassCursor = 0) Begin
        Move (GetClassLong (hWnd, GCL_HCURSOR)) to hClassCursor
        Set ClassCursorHandle to hClassCursor
    Move (WinAPI_LoadCursorFromFile (AddressOf (sCursorFileName))) to iCursorID
    If (iCursorID <> 0) Begin
        Move (SetClassLong (hWnd, GCL_HCURSOR, iCursorID)) to iRetval
        Move (SetCursor (iCursorID)) to iRetval
        Send Set_Mouse_Capture
If you now code:
Set psFileCursor Of Cursor_Control to "ani839.ani"
you will get a nice rotating easter egg instead of hourglass. Resetting the cursor to normal is as mentioned before by sending the cursor_ready to the object.

The Windows API code for loading the cursor is:
External_Function WinAPI_LoadCursorFromFile "LoadCursorFromFileA" User32.DLL Address lpFileName Returns Integer
Have a nice easter!