Delphi在combobox下拉框里显示图片


本文整理自网络,侵删。

 
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;
//该代码片段来自于: http://www.sharejs.com/codes/delphi/8755

相关阅读 >>

Delphi中隐藏程序进程

Delphi base32 的加密和解密

Delphi中组件panel、splitter、groupbox、按钮组件(checkbox...)、计时器、滚动条、多选卡

Delphi 复制动态数组

Delphi 调用外部 dll 中的函数(2. 晚绑定)

Delphi 调用exitwindows 关闭系统

Delphi system 提供的编译期函数

Delphi10.3构造一个json数据的第二种方法,并格式化输出

Delphi 简单的日志记录

Delphi xe5调用外部扫描程序――谷歌 zxing

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...