在Delphi中用Loadcursor()得到的遊標只有黑白兩色,怎麼在程式中得到彩色遊標呢?筆者嘗試製作了以下程序:
方法一用Loadcursorfromfile()從外部調入圖示作為遊標
Loadcursorfromfile()函數可以讀*CUR,*ICO,*ANI為後綴的檔案作為遊標,其中ICO為彩色圖示格式(可用Image Editor製作),ANI為動畫遊標格式。以下為開啟一圖示作為遊標的示範程式段,當遊標移動到測試區域內遊標會變成選定的圖案;
{設:opendialog1:Topendialog;Bitbtn1:Tbitbtn}
PRocedure TForm1.BitBtn1Click(Sender:TObject);
var tt:pchar;size:integer;s:string;
begin
if opendialog1.Execute then
begin
size:=length(opendialog1.filename);
getmem(tt,size);
s:=opendialog1.filename;
strpcopy(tt,s);
screen.cursors[2]:=loadcursorfromfile(tt);
bf.cursor:=2;
freemem(tt,size);
end;
end;
方法二從資源檔案載入彩色遊標
用方法一傳送程式時必須包含*CUR文件,因而從資源文件載入彩色遊標是更可行的方法。用圖示存放彩色遊標,使用時將圖示存入臨時文件,用Loadcursorfromfile()從臨時文件讀出彩色遊標。
程式段:
procedure ZloadfromResourse(screenindex:integer;name:Pchar);
var td:ticon;
begin
try
td:=ticon.Create;
td.Handle:=LoadIcon(Hinstance,name);
td.SaveToFile(′temp.cur′);
screen.Cursors[screenindex]:=loadcursorfromfile(′temp.cur′);
deletefile(′temp.cur′);
finally
td.free;
end;
end;
此程式將名字為name的圖示變為序號為screenindex的遊標;
例:
ZloadfromResourse(2,′myicon′);
Form1.cursor:=2;