I think DrawIconEx might be useful for this. With it, you can simply draw the entire image of the cursor on a specific canvas. It is also possible to draw the specified animated cursor frame by passing its index to the istepIfAniCur parameter. The following example shows how to save the current cursor in a stream (Button1Click) and load it back and display (Button2Click).
Another question is how to determine if the cursor is animated .
var Stream: TMemoryStream; procedure TForm1.FormCreate(Sender: TObject); begin Stream := TMemoryStream.Create; end; procedure TForm1.FormDestroy(Sender: TObject); begin Stream.Free; end; procedure TForm1.Button1Click(Sender: TObject); var Picture: TPicture; CursorInfo: TCursorInfo; begin Picture := TPicture.Create; CursorInfo.cbSize := SizeOf(CursorInfo); GetCursorInfo(CursorInfo); Picture.Bitmap.Transparent := True; Picture.Bitmap.Width := GetSystemMetrics(SM_CXCURSOR); Picture.Bitmap.Height := GetSystemMetrics(SM_CYCURSOR); DrawIconEx( Picture.Bitmap.Canvas.Handle, // handle to the target canvas 0, // left coordinate 0, // top coordinate CursorInfo.hCursor, // handle to the current cursor 0, // width, 0 for autosize 0, // height, 0 for autosize 0, // animated cursor frame index 0, // flicker-free brush handle DI_NORMAL // flag for drawing image and mask ); Picture.Bitmap.SaveToStream(Stream); Picture.Free; end; procedure TForm1.Button2Click(Sender: TObject); var Picture: TPicture; begin Stream.Position := 0; Picture := TPicture.Create; Picture.Bitmap.Transparent := True; Picture.Bitmap.Width := GetSystemMetrics(SM_CXCURSOR); Picture.Bitmap.Height := GetSystemMetrics(SM_CYCURSOR); Picture.Bitmap.LoadFromStream(Stream); SetBkMode(Canvas.Handle, TRANSPARENT); Canvas.FillRect(Rect(0, 0, 32, 32)); Canvas.Draw(0, 0, Picture.Graphic); Picture.Free; end;
user532231
source share