was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

31 Visitors Online


 
...Bitmaps in eine TComboBox zeichnen?
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Tip ausdrucken ]  

Tip Bewertung (3):  
     


// This code works also with a TListBox


var
  
Bitmap1, Bitmap2, Bitmap3, Bitmap4: TBitmap;

procedure TForm1.FormCreate(Sender: TObject);
begin
  
ComboBox1.Style := csOwnerDrawVariable;

  //Set height of one item
  
ComboBox1.ItemHeight := 20;

  //load all bitmaps

  
Bitmap1 := TBitmap.Create;
  Bitmap1.LoadFromFile('chem16.bmp');

  Bitmap2 := TBitmap.Create;
  Bitmap2.LoadFromFile('chip16.bmp');

  Bitmap3 := TBitmap.Create;
  Bitmap3.LoadFromFile('factry16.bmp');

  Bitmap4 := TBitmap.Create;
  Bitmap4.LoadFromFile('skylin16.bmp');

  //assign bitmaps with item objects - caption of the items: Bitmap 1 - Bitmap 4

  
ComboBox1.Items.AddObject('Bitmap 1', Bitmap1);
  ComboBox1.Items.AddObject('Bitmap 2', Bitmap2);
  ComboBox1.Items.AddObject('Bitmap 3', Bitmap3);
  ComboBox1.Items.AddObject('Bitmap 4', Bitmap4);
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  
Bitmap: TBitmap;
  Offset: Integer;
begin
  
offset := 0;
  with ComboBox1.Canvas do
  begin
    
FillRect(Rect);
    Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);
    if Bitmap <> nil then
    begin
      
//copy bitmap to combobox coordinate
      
BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
        Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
        Bitmap.Height), clred);
      Offset := Bitmap.Width + 8;
    end;
    //Set a caption to each item
    
TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index]);
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  
//free all bitmap objects
  
bitmap1.Free;
  bitmap2.Free;
  bitmap3.Free;
  bitmap4.Free;
end;


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners