I am creating a resource file, Cur.rc He contains
CUR1 21 "MYCURSOR.ANI"
mycursor.ani is the file that exists. This is a regular animated .ani cursor.
I will compile the resource file using
Brcc32 cur.rc
which leads to the cur.res file
In Unit1 of my project (I also tried the project .dpr file), I added
{$R cur.res}
In the FormCreate event
Screen.Cursors[8]:=LoadCursor(HInstance, 'CUR1'); Screen.Cursor := 8; // ps 8 - for an example, can be 0.... n or a constant
I am running the application. The cursor should change to my animated cursor.
It should be easy. I tried several different cursor files from different sources. But that does not work.
It works if you download from a file:
Screen.Cursors [8]:=LoadCursorFromFile('d:\1.ani'); Screen.Cursor:=8;
How to load an animated cursor from a resource? Why doesn't it work like loading a regular cursor?
This will not work. There seems to be no way to load an animated cursor from a resource without going to the 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 the comments do not want to be applied.
PS I wrote on Delphi 2010, but I think how it will work on Delphi 7.
The main question:
Only works for Panel1. The source code is in the kit, here is the text, if anyone is long:
A complete example tried to do:
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
The source code is in the kit, here is the text, if anyone is 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 the pop-up window, look down); mirror: Attention - you do not need to pay anything (it is downloaded via a slow link (size is 4 mb)) http://rapidshare.com/files/455515706/TestCursor.zip (no viruses - Symantec testing - 11.6.3000 SEP) In general, the original the code in the kit will understand.