Steve
Steve

Reputation: 2520

Showing an animated .ani cursor on a form in Delphi 7

My application is loading list items from a database in the background with a separate thread. During the loading process the user can interact with the application, so I don't want to change the default cursor. Still, to visually signal the loading process, I'd like to display the Windows busy (hourglass, IDC_WAIT) cursor on my form like an animated image. What would be the easiest way to do that? I was thinking of converting to a GIF and displaying in a GIFImage but I'd like to display the default Windows cursor (thus getting the resource from the system dynamically).

Note: I'm using Delphi7.

Upvotes: 0

Views: 395

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

Just a partial solution: Declare variables

h: HICON;
FFrameIdx: Integer;

Then load the cursor:

h := LoadImage(0, MakeIntResource(OCR_WAIT), IMAGE_CURSOR, 0, 0, LR_SHARED);

Then draw the current frame in the OnPaint handler:

DrawIconEx(Canvas.Handle, 100, 100, h, 0, 0, FFrameIdx, 0, DI_NORMAL);

To animate it, you can use a TTimer with this code:

Inc(FFrameIdx);
Invalidate; // or something more suitable

To try this, you can just put the variables in a new VCL application's main form class (below private), put the LoadImage in the form's OnCreate handler and the DrawIconEx in the OnPaint handler.

But if you want to use this in a real app, you'd better create a new custom control with it.

Obviously, you need to replace FFrameIdx with FFrameIdx mod FFrameCount or Inc(FFrameIdx) with FFrameIdx := Succ(FFrameIdx) mod FFrameCount, where FFrameCount is the number of frames in the animated cursor.

You should also set the timer's Interval to match the cursor's frame rate.

Screen recording

Upvotes: 2

Related Questions