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.