Poner imágenes en los Items

4580 vistas

Para poder añadir imágenes a los Ãtems de un ComboBox, necesitamos almacenar esas imágenes en "algún lado". Para ello usaremos un TImageList.

Además tendremos que programar el evento DrawItem donde dibujaremos en el Canvas



delphi
  1. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   Rect: TRect; State: TOwnerDrawState);
  3. var
  4.   bTemp: TBitmap;
  5. begin
  6.   bTemp := TBitmap.Create;
  7.   if Index < ImageList1.Count then
  8.     ImageList1.GetBitmap(Index,bTemp);
  9.  
  10.   with (Control as TComboBox) do
  11.   begin
  12.     Canvas.FillRect(Rect);
  13.     Canvas.TextOut(Rect.Left + ImageList1.Height + 2, Rect.Top, Items[Index]);
  14.     Canvas.Draw(Rect.Left, Rect.Top, bTemp);
  15.   end;
  16.   bTemp.Free;
  17. end;



Nota: esto mismo es aplicable a un TListBox