6

I create a resource file, Cur.rc. It contains

CUR1   21   "MYCURSOR.ANI"

mycursor.ani is a file that exists. It's a normal .ani animated cursor.

I compile the resource file using

Brcc32 cur.rc  

which results in the file cur.res

In Unit1 of my project (I tried the project's .dpr file as well), I added

{$R cur.res} 

In the FormCreate event,

Screen.Cursors[8]:=LoadCursor(HInstance, 'CUR1');
Screen.Cursor := 8;
// p.s. 8 - for an example, can be 0.... n or a constant

I run the application. The cursor should change to my animated cursor.

This should be simple. I've tried several different cursor files from different sources. But it doesn't work.

It works if you load from a file:

Screen.Cursors [8]:=LoadCursorFromFile('d:\1.ani'); 
Screen.Cursor:=8;

How do I load the animated cursor from a resource? Why doesn't it work like loading a normal cursor?


This won't work. Apparently, there's no way to load an animated cursor from a resource without going to a file first.

This works, though:

// MyCursor.rc
BGCURSOR ANICURSOR "D:\TEMP\Background.ani"

// Unit1.pas
{$R MyCursor.res MyCursor.rc}

procedure TForm1.FormCreate(Sender: TObject);
var
  Res: TResourceStream;
  FileName: string;
  HC: HCURSOR;
const
  BGCursor = 8;  // Can be anything from 0..32767
begin
  Res := TResourceStream.Create(MainInstance, 'BGCURSOR', 'ANICURSOR');
  try
    FileName := ExtractFilePath(ParamStr(0)) + 'BGCursor.ani';
    Res.SaveToFile(FileName);
    HC := LoadCursorFromFile(PChar(FileName));
    Screen.Cursors[BGCursor] := HC;
    Screen.Cursor := BGCursor;
  finally
    DeleteFile(FileName);
    Res.Free;
  end;
end;

I write it after two answers because comments do not wish to be applied.

P.S. Wrote on Delphi 2010, but I think, as on Delphi 7 will go.

The basic question:

Works only for Panel1. Initial code in the complete set, here text, if to whom long:

The full example has tried to make:

1. 3 panels - at form start on everyone are appointed the cursor.
2. Works only on the first or at loading of the cursor from a file
3. All in the complete set (the small program, cursors, etc.) - exe (executed) - too there

Initial code in the complete set, here text, if to whom long:

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

Const
  CurConst = 8; // 8 .. example, 8 is like me :)
  CurConst1 = 7;
  CurConst2 = 6;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Button1: TButton;
    Panel3: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  cur: HCursor;

implementation

{$R *.dfm}
{$R My.res}

procedure TForm1.Button1Click(Sender: TObject);
var 
  s: string;
begin
  s:=IncludeTrailingPathDelimiter(extractfilepath(paramstr(0)))+'sc1.ani';
  showmessage('Temp value: '+ s);
  Screen.Cursors[CurConst]:=LoadCursorFromFile(pchar(s));
  panel1.Cursor:=CurConst;
  panel2.Cursor:=CurConst;
  panel3.Cursor:=CurConst;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // panel 1
  Screen.Cursors[CurConst] := LoadCursor(HInstance,'V_0');
  panel1.Cursor:=CurConst;
  // panel 2
  Screen.Cursors[CurConst1] := LoadCursor(HInstance,'V_1');
  panel2.Cursor := CurConst1;
  // panel 2
  Screen.Cursors[CurConst2] := LoadCursor(HInstance,'V_2');
  panel3.Cursor := CurConst2;
end;

end.

DownLoad (full project code): http://www.sendspace.com/file/jbzrpx (close popup window, look down); mirror: The attention - anything to pay it is not necessary (is downloaded under the slow reference (the size 4 mb)) http://rapidshare.com/files/455515706/TestCursor.zip (no viruses - testing Symantec - 11.6.3000 SEP) In general there an initial code in the complete set - will understand.

3
  • Wow. Invalid ANI file. Weird. I had the same kind of problem once, and solved it the same way. But I had this problem with an animated .GIF.
    – Warren P
    Commented Apr 3, 2011 at 1:04
  • @Warren - I was surprised to find out more than half of the ani files on my disk were invalid (excluding the ones in the 'Windows' folder). I guess that's why the api tolerates them when loading from a file.. Commented Apr 4, 2011 at 14:00
  • Yes, you are right, at me bad English - and I see here, that the question has been set correctly, it very much suits me. Many thanks to all who has corrected the text or correctly it has understood.
    – Gu.
    Commented Jun 29, 2012 at 21:55

4 Answers 4

9

Apparently the resource loader of the api is somewhat pickier than the file loader, or the requirements are a bit different.

I was able to make your cursors work in the full project code download you provided, without even touching the code, thanks to the executable included:

  • Open 'sc1.ani' in a hex editor.
  • Locate the second double word in the file (the first is the signature 'RIFF'). The double word is '62 0F 00 00' which is 3938, this is the file size. Modify it to become '5A 0F 00 00', that is '3930', now it denotes the total number of remaining bytes after the length identifier. Save the ani file.
  • Open the executable in 'Resource Hacker' or compatible resource editor, and replace 'V_1' with the modified file.
  • Save the executable and run.


You can use Greenfish Icon Editor to open your animated cursors and then save again. It seems to produce compatible files.

2
  • 2
    +1. Wow - I never would have thought of checking the ANI file bits and bytes. :) That explains why the OP's didn't work and mine did.
    – Ken White
    Commented Apr 2, 2011 at 15:08
  • Snx, Greenfish Icon Editor - The program has helped, after reopening (through it) and repeated compilation of a file of resources all cursors have started to be loaded normally. First your way (through hex the editor) too works, but through GIE is easier
    – Gu.
    Commented Apr 6, 2011 at 14:43
1

Use the deceivingly named CreateIconFromResource API function.

var
  res: TCustomMemoryStream;
  cur: HCursor;
begin
  res := TResourceStream.Create(MainInstance, 'BGCURSOR', 'ANICURSOR');
  try
    cur := CreateIconFromResource(res.Memory, res.Size, False, $30000);
    Win32Check(cur <> 0);
    Screen.Cursors[8] := cur;
  finally
    res.Free;
  end;
end;
5
  • @Rob: This doesn't work. It raises an exception: "EOSError: A call to an OS function failed". (I tried this approach too, before I posted my answer.) I used the same resource file contained in my answer, too.
    – Ken White
    Commented Apr 2, 2011 at 2:23
  • @Ken, that's odd. This is the same technique used by the other questions here asking about animated cursors in resources. Maybe the "version" parameter is wrong? I have no idea what it means. Commented Apr 2, 2011 at 2:42
  • @Rob: That's what I thought when I went down that road. I spent about two hours trying to figure this out, because I couldn't believe that loading from a resource didn't work. I tried a couple of different versions, different values for fIcon, etc., and it wouldn't work.
    – Ken White
    Commented Apr 2, 2011 at 3:02
  • mmm...... It is possible an example how you form a file of resources? BGCURSOR in our case = CUR1? ANICURSOR - the standard instruction? Here an example: sendspace.com/file/gyirsq (1 file, 4 kb, scred.ani) In my case - writes: the resource is not found (at compilation)
    – Gu.
    Commented Apr 2, 2011 at 5:47
  • /\ In a question has published additional data
    – Gu.
    Commented Apr 2, 2011 at 7:01
1

This works for me on Berlin:

To be more specific: If you landed here because you are looking for a way to load animated cursors without first having to save it into a file, this worked for me. The only difference is, move the data to the heap and load it from there.

function GetResourceAsAniCursor(const ResName: string; Width, Height: Integer): HCursor;
var
  resInfo: HRSRC;
  resSize: cardinal;
  resHandle: HGLOBAL;
  resData, Data: pchar;
begin
  Result := 0;
  resInfo := FindResource(MainInstance, PWideChar(ResName), PWideChar('ANICURSOR'));
  resHandle := LoadResource(MainInstance, resInfo);
  if resHandle <> 0 then
  begin
    resSize := SizeofResource(MainInstance, resInfo);
    resData := LockResource(resHandle);
    Data := AllocMem(resSize);
    try
      Move(resData^, Data^, resSize);
      Result := CreateIconFromResourceEx(PByte(Data), resSize, False, $00030000, Width, Height, 0);
    finally
      FreeMem(Data);
    end;
  end;
end;

Usage:

const 
 crMyAniCursor = 1;

 Screen.Cursors[crMyAniCursor] := GetResourceAsAniCursor('MYANICURSOR', 128, 128);
10
  • @SertacAkyuz could you be please more specific? What is a "broken" cursor?
    – Atys
    Commented Apr 19, 2018 at 8:30
  • Please read the question, then I won't have to be more specific. I wonder how can you post an answer without reading the question. Also read the accepted answer, it will give you a hint of the actual issue and explain what is broken. Commented Apr 19, 2018 at 10:06
  • @SertacAkyuz Maybe you should read the question, try to concentrate on the sentences ending with question mark. Also, read the statements where OP and some others states that animated cursors can't be loaded from resource, only from file. The fact, that his cursor was broken has nothing to do with his question. Either change the question to: How can I load a broken cursor or delete the thread. Then this is the first google hit for Delphi how to load an animated cursor from resource.
    – Atys
    Commented Apr 19, 2018 at 11:06
  • So you think that the accepted answer is a lie. And the OP accepted it without even trying it. And the OP lies in his comment to the accepted answer. Good thing that you are not fooled with all these lies. Go on... Commented Apr 19, 2018 at 11:55
  • 1
    I tested this. Although I don't understand what makes it so, it does seem to be working. I replaced PWideChar('ANICURSOR') with RT_ANICURSOR to be able to work with the original project but I think those links should be dead by now anyway. Commented Apr 19, 2018 at 15:39
0

This won't work. Apparently, there's no way to load an animated cursor from a resource without going to a file first.

This works, though:

// MyCursor.rc
BGCURSOR ANICURSOR "D:\TEMP\Background.ani"

// Unit1.pas
{$R MyCursor.res MyCursor.rc}

procedure TForm1.FormCreate(Sender: TObject);
var
  Res: TResourceStream;
  FileName: string;
  HC: HCURSOR;
const
  BGCursor = 8;  // Can be anything from 0..32767
begin
  Res := TResourceStream.Create(MainInstance, 'BGCURSOR', 'ANICURSOR');
  try
    FileName := ExtractFilePath(ParamStr(0)) + 'BGCursor.ani';
    Res.SaveToFile(FileName);
    HC := LoadCursorFromFile(PChar(FileName));
    Screen.Cursors[BGCursor] := HC;
    Screen.Cursor := BGCursor;
  finally
    DeleteFile(FileName);
    Res.Free;
  end;
end;
5
  • Of course user may need administrator privileges to succeed on UAC enabled computers. Why not use a temporary file? or a file in a user writable directory?
    – jachguate
    Commented Apr 2, 2011 at 1:55
  • @jachguate: Write it wherever you need to - my example was to show how, which was the question. I could just as well have said "Of course, you need a computer to do this. You'll also need a copy of Delphi, and a keyboard." Why complicate the answer with unecessary code, when it wasn't the question? - OK. I'll fix it for you.
    – Ken White
    Commented Apr 2, 2011 at 2:29
  • Wow! {$R MyCursor.res MyCursor.rc} - For question editing - separate thanks. ------------------------------- So unless it is possible? Probably while Delphi badly I know... In the evening I will check up, now there is nothing to execute. I think, that the difference in a time zone a little to us stirs. It is necessary to have a sleep a little... (unreal work this forum with IE 9.. .) Anyhow - already I see other decisions (and in the answer more low, too thanks) than in native inquiry Codegear (ebarcadero) Delphi, Which why that in more in answers - is ground under C ++ :(
    – Gu.
    Commented Apr 2, 2011 at 5:02
  • ок, the dream was gone. Is not present - does not interest from a file: it works always, wrote about it, probably or the editor has not up to the end corrected difficulties of transfer. And resource record on a disk with loading after the cursor - too does not interest By the way, I think that in this case the cursor to keep as RcData better.
    – Gu.
    Commented Apr 2, 2011 at 5:36
  • Excuse, but in the comment it not gjvtoftncz (and answering more low too this text) test of the project, cursors and a question - why one works and another is not present? look UP
    – Gu.
    Commented Apr 2, 2011 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.