2013-06-19 19 views
2

我試着將PNG文件添加到TPngImageList(PngComponents for D7 got from http://code.google.com/p/cubicexplorer/downloads/list)。PngComponents:如何將PNG文件添加到TPngImageList

type 
    TImgListCrack = class(TPngImageList); 

function LoadPngIconEx(ImageList: TPngImageList; const fn: string): boolean; 
var 
    Icon: HICON; 
    AImage: TPngObject; 
begin 
    with ImageList do 
    begin 
    BeginUpdate; 
    try 
     AImage:= TPngObject.Create; 
     AImage.LoadFromFile(fn); 
     Icon:= TImgListCrack(ImageList).PngToIcon(AImage); 
     ImageList_AddIcon(Handle, Icon); 
     DestroyIcon(Icon); 
     FreeAndNil(AImage); 
     Result:= true; 
    finally 
     EndUpdate; 
    end; 
    end; 
end; 

結果:圖標未添加,圖片列表仍爲空。如何做到這一點好嗎?

回答

2

未經測試,但不應該只是工作?

ImageList.PngImages.Add.PngImage.LoadFromFile(fn); 
+0

不,它不。沒有屬性TPngImageList中的項目,沒有方法添加w/out參數。 – Prog1020

+1

的確,這是爲了TPngImageCollection。更改了TPngImageList的代碼。 –

+0

U're在正確的軌道上。解決了。看到我的回覆。 – Prog1020

0

通過使用其他PngImageList方法解決。道具PngImages需要功能。

function LoadPngIconEx(ImageList: TPngImageList; const fn: string): boolean; 
var 
    AImage: TPngObject; 
begin 
    if not FileExists(fn) then 
    Result:= false 
    else 
    begin 
    AImage:= TPngObject.Create; 
    try 
     AImage.LoadFromFile(fn); 
     ImageList.PngImages.Add.PngImage:= AImage; 
     Result:= true; 
    finally 
     FreeAndNil(AImage); 
    end; 
    end; 
end; 
+1

只有代碼的答案在Stack Overflow上不是一個好的答案。請添加一些句子來解釋這個代碼是如何回答問題的。原始代碼有什麼問題,爲什麼這個問題解決了? –