How to save the current cursor in a stream (resource or file)?

Does anyone know how to save the cursor (currently used by my application, even if it is configured or animated) to a stream or file, so that I can send it over the network to another computer where the application loads and use it? I just want to clone the cursor from a remote computer.

As I found in this article , most icon functions can also be used for cursors, but I cannot find an easy to translate example. Here is one example of using COM, but I'm not sure that the IPicture interface can also be used for cursors. Here , for example, the discussion of saving an image in a * .cur file is discussed, but I can not find anything suitable for saving and loading cursors into a stream, resource, or something that I can send over the network and upload to the target computer.

PS there is no SaveCursorToFile function, as you might expect .

Thanks for any suggestions.

+3
source share
2 answers

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; 
+2
source

Have a look here: IconsToFile.pas .

It also saves (static) cursors. Can be tested with:

 hIconToFile('C:\Temp\Demo.cur', GetCursor, BitC32); 

Works. You may need to adjust the bit rate. I think it will have problems with animated cursors, but it may be enough to get you started.

+2
source

Source: https://habr.com/ru/post/894463/


All Articles