Putting bitmaps within ComboBox items  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 25/12/99

Tip accessed 473 times

 


We will use a TImageList to store the images of each Item.

  • Put a TImageList (ImageList1) and a TComboBox (ComboBox1) in your form
  • Put the Style property of ComboBox1 to lbOwnerDrawFixed
  • Put the next code in their OnDrawItem event:


     procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
       Rect: TRect; State: TOwnerDrawState);
     var
        bTemp:TBitmap;
     begin
       bTemp:=TBitmap.Create;
       if Indexthen
       begin
         ImageList1.GetBitmap(Index,bTemp);
       end;
    
       with (Control as TComboBox) do
       begin
         Canvas.FillRect(Rect);
         Canvas.TextOut(Rect.Left+ImageList1.Height+2,Rect.Top,Items[Index]);
         Canvas.Draw(Rect.Left,Rect.Top,bTemp);
       end;
       bTemp.Free;
     end;
    



    Ah!, this tip also is valid for a ListBox...