[Delphi (Object Pascal)] Delphi在combobox下拉框里显示图片 →→→→→进入此内容的聊天室

来自 , 2020-10-29, 写在 Delphi (Object Pascal), 查看 139 次.
URL http://www.code666.cn/view/aecad423
  1. // http://www.sharejs.com
  2. var
  3.   Bitmap1, Bitmap2, Bitmap3, Bitmap4: TBitmap;
  4.  
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. begin
  7.   ComboBox1.Style := csOwnerDrawVariable;
  8.  
  9.   //Set height of one item
  10.   ComboBox1.ItemHeight := 20;
  11.  
  12.   //load all bitmaps
  13.  
  14.   Bitmap1 := TBitmap.Create;
  15.   Bitmap1.LoadFromFile('chem16.bmp');
  16.  
  17.   Bitmap2 := TBitmap.Create;
  18.   Bitmap2.LoadFromFile('chip16.bmp');
  19.  
  20.   Bitmap3 := TBitmap.Create;
  21.   Bitmap3.LoadFromFile('factry16.bmp');
  22.  
  23.   Bitmap4 := TBitmap.Create;
  24.   Bitmap4.LoadFromFile('skylin16.bmp');
  25.  
  26.   //assign bitmaps with item objects - caption of the items: Bitmap 1 - Bitmap 4
  27.  
  28.   ComboBox1.Items.AddObject('Bitmap 1', Bitmap1);
  29.   ComboBox1.Items.AddObject('Bitmap 2', Bitmap2);
  30.   ComboBox1.Items.AddObject('Bitmap 3', Bitmap3);
  31.   ComboBox1.Items.AddObject('Bitmap 4', Bitmap4);
  32. end;
  33.  
  34. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  35.   Rect: TRect; State: TOwnerDrawState);
  36. var
  37.   Bitmap: TBitmap;
  38.   Offset: Integer;
  39. begin
  40.   offset := 0;
  41.   with ComboBox1.Canvas do
  42.   begin
  43.     FillRect(Rect);
  44.     Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);
  45.     if Bitmap  nil then
  46.     begin
  47.       //copy bitmap to combobox coordinate
  48.       BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  49.         Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  50.         Bitmap.Height), clred);
  51.       Offset := Bitmap.Width + 8;
  52.     end;
  53.     //Set a caption to each item
  54.     TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index]);
  55.   end;
  56. end;
  57.  
  58. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  59. begin
  60.   //free all bitmap objects
  61.   bitmap1.Free;
  62.   bitmap2.Free;
  63.   bitmap3.Free;
  64.   bitmap4.Free;
  65. end;
  66. //delphi/8755

回复 "Delphi在combobox下拉框里显示图片"

这儿你可以回复上面这条便签

captcha