Pointing with an hourglass
In this first Tip-Of-The-Hat, I want to show how we can turn the mouse cursor from a normal pointer into an hourglass.
This is especially very useful at times when the application is performing a certain task that takes a lot of time (a complex calculation, big query or retrieval of some external data).
One thing that doesn't work is assigning a new value to the Cursor property of the Form.
Instead, we need to set the Screen's Cursor to crHourGlass in order to get a hourglass.
And as long as we perform the complex task inside a try-finally block, we can ensure that the Screen's Cursor property is set back to the normal (crDefault) mouse pointer.
procedure TForm1.Button1Click(Sender:TObject);
begin
Screen.Cursor := crHourglass;
try
finally
Screen.Cursor := crDefault;
end
end;
This way, even if your "complex task" fails (and of course your errors are all raised as exceptions aren't they?) the user is not left with a "stuck" hourglass.
Using Borland C++Builder, you need to write the following code inside the OnClick event handler:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TCursor oldCursor = Screen->Cursor;
Screen->Cursor = crHourGlass;
try
{
}
__finally
{
Screen->Cursor = oldCursor;
}
}
Note that this code is slightly more flexible, as it saves and restores the previous state of the Screen->Cursor instead of assuming it should be set back to crDefault.
Epilogue
Apart from the crHourglass, you can also select one of the other Cursor types (all defined in Controls.pas), such as an Arrow, Cross, IBean, UpArrow, Drag, NoDrop, HSplit, VSplit, MultiDrag, SQLWait, No, AppStart, Help, HandPoint or the Size and different Size pointers (SizeAll, SizeNESW, SizeNS, SizeNWSE, SizeWE).
Most of them will lead to confusion, of course, but sometimes I explicitly use the SQLWait cursor instead of the Hourglass in the code above.