Mam taki oto program:
Na formatkę wrzuciłem komponenty:
6 przycisków typu TButton i po jednym ListView, Image i ListImege
Image1: TImage;
ImageList1: TImageList;
ListView1: TListView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
W onCreate Formy ustawiam komponenty (robię to tylko po to żeby
wszyscy mieli taki sam rozkład komponentów na formie) takim oto kodem:
procedure TForm1.FormCreate(Sender: TObject);
var ListItem : TListItem;
begin
Width := 434;
Height := 395;
ImageList1.Width := 48;
ImageList1.Height := 48;
//ImageList1.Width := 32;
//ImageList1.Height := 32;
Image1.Left := 8;
Image1.Top := 8;
Image1.Width := 105;
Image1.Height := 145;
Button1.Left := 8;
Button1.Top := 159;
Button1.Caption:= 'Metoda1';
Button2.Left := 8;
Button2.Top := 190;
Button2.Caption:= 'Metoda2';
Button3.Left := 8;
Button3.Top := 221;
Button3.Caption:= 'Metoda3';
Button4.Left := 8;
Button4.Top := 252;
Button4.Caption:= 'Metoda4';
Button5.Left := 8;
Button5.Top := 283;
Button5.Caption:= 'Metoda5';
Button6.Left := 8;
Button6.Top := 314;
Button6.Caption:= 'Metoda6';
with ListView1 do
begin
Clear;
Left := 119;
Top := 8;
Width := 299;
Height := 326;
ListItem := Items.Add;
ListItem.Caption := 'Test';
ListItem.ImageIndex := 0;
end;
end;
Powyższa procedura nie jest ważna może jej wcale nie być
Najważniejsze są procedury pod przyciskami i tak pod:
Button1 onClick mamy:
procedure TForm1.Button1Click(Sender: TObject);
var H : HICON;
begin
ListView1.LargeImages := ImageList1;
ImageList1.Clear;
H := Get48hIcon('C:\Windows\notepad.exe',0);
Image1.Picture.Icon.Handle := H;
ImageList1.AddIcon(Image1.Picture.Icon);
Showmessage('Ile ikon1=' + IntToStr(ImageList1.Count));
end;
Button2 onClick mamy:
procedure TForm1.Button2Click(Sender: TObject);
var Icon : TIcon;
LargeIcon : Hicon;
SmallIcon : Hicon;
begin
ListView1.LargeImages := ImageList1;
ImageList1.Clear;
Icon := TIcon.Create;
try
ExtractIconEx(PAnsiChar('C:\Windows\explorer.exe'), 0, LargeIcon,
SmallIcon, 1);
if LargeIcon > 1 then
begin
Icon.Handle := LargeIcon;
ImageList1.AddIcon(Icon);
Image1.Picture.Icon := Icon;
end;
finally
Icon.Free;
Showmessage('Ile ikon2=' + IntToStr(ImageList1.Count));
end;
Button3 onClick mamy:
procedure TForm1.Button3Click(Sender: TObject);
var FileHandle : THandle;
H : HIcon;
Icon : TIcon;
begin
ListView1.LargeImages := ImageList1;
ImageList1.Clear;
FileHandle := LoadLibraryEx(PChar(application.exename),
0,LOAD_LIBRARY_AS_DATAFILE);
Icon := TIcon.create;
try
H := LoadImage(FileHandle,'MAINICON',IMAGE_ICON,48,48,0);
Icon.Handle := H;
if H > 0 then
begin
ImageList1.AddIcon(Icon);
Image1.Picture.Icon := Icon;
end;
finally
Icon.Free;
Showmessage('Ile ikon3=' + IntToStr(ImageList1.Count));
end;
end;
Button4 onClick mamy:
procedure TForm1.Button4Click(Sender: TObject);
procedure ImageListTo32Bit(const ImageList: TImageList);
begin
if Assigned(ImageList) then
begin
// czyscimy ikonki:
ImageList.Clear;
ImageList.Handle := ImageList_Create(48, 48, ILC_COLOR32 or
ILC_MASK, 0, 0);
end;
end;
begin
//wczytuję ikonę do komponentu TImage
Button1Click(Sender);
//Button2Click(Sender);
ImageList1.Clear;
ImageListTo32bit(ImageList1);
ImageList1.AddIcon(Image1.Picture.Icon);
ListView1.LargeImages := ImageList1;
end;
Button5 onClick mamy:
procedure TForm1.Button5Click(Sender: TObject);
var
IL: TImageList;
begin
//wczytuję ikonę do komponentu TImage
Button1Click(Sender);
IL := TImageList.Create(nil);
IL.Handle := ImageList_Create(48, 48, ILC_COLOR32 or ILC_MASK, 0,
IL.AllocBy);
IL.AddIcon(Image1.Picture.Icon);
ListView1.LargeImages := IL;
end;
Button6 onClick mamy:
procedure TForm1.Button6Click(Sender: TObject);
var
Bitmap : TBitmap;
H : HICON;
begin
ListView1.LargeImages := ImageList1;
ImageList1.Clear;
//wczytuję ikonę do komponentu TImage
H := Get48hIcon('C:\Windows\notepad.exe',0);
Image1.Picture.Icon.Handle := H;
//konwertuję na BMP
Bitmap := TBitmap.Create;
try
Bitmap.Width := Image1.Width;
Bitmap.Height := Image1.Height;
Bitmap.Canvas.Draw(0, 0, Image1.Picture.Icon);
ImageList1.Add(Bitmap, nil);
finally
Bitmap.Free;
end;
end;
Powyższe funkcje znalazłem na różnych stronach i forach internetowych
Do prawidłowego działania będzie potrzebny jeszcze plik
IconsToFile.pas znajdujący się tu: http://www.angelfire.com/hi5/delphizeus/icon2file.zip
a opis do pliku tu: http://www.angelfire.com/hi5/delphizeus/saveicons.html
A teraz w czym rzecz.
Otóż chciałbym wczytać do komponentu typu TListImage a pośrednio i do
TListView ikony z dowolnego programu o rozmiarze 48x48
Trochę poszukałem i znalazłem 6 powyższych rozwiązań (6 opracowałem na
podstawie 1 + konwersja ICO na BMP) przy czym rozwiązanie ostatnie działa najlepiej.
Wszystkie działają natomiast gdy zmienię w TImageList wys i szer ikon
na 32
ImageList1.Width := 32;
ImageList1.Height := 32;
Nie mniej jednak potrzebuję w ListView ikony o rozmiarze 48x48 (lub
więcej) i żeby zachowywały się tak jak prawdziwe ikony tzn. miały
przeźroczyste tło
Rozwiązanie 6 pobiera ikonę ale jej tło po przekonwertowaniu nie jest
przeźroczyste.
Czy ktoś wie jak to rozwiązać, ktoś wie jak do TImageList załadować
ikony o rozmiarze 48x48 (lub więcej) i wyświetlić je w TListView ???
Proszę o jakiekolwiek podpowiedzi.
Chcę ikony ładować dynamicznie w czasie działania programu.
Na piechotę działa, sprawdzałem. Wstawiłem do TImageList kilka ikon 48x48, powiązałem z TListView, dodałem kilka pozycji do TListView i ikonki były ładnie wyświetlane
Co jest nie tak z dodawaniem dynamicznym
Proszę o podpowiedź