📄 combo box.txt
字号:
给ListBox、ComboBxox添加图片
给ListBox、ComboBxox添加图片
第一步:
在Form1上分别放置一个ComboBox1: TComboBox,改变ComboBox1的Style属性:ComboBox1.Style := csOwnerDrawVariable;ListBox1 : TListBox,改变ListBox1的Style属性 ListBox1.style := lbOwnerDrawVariable
第二步:
定义一个图片数组,Bmp : Array [0..9] of TBitmap ;同时再设置一个BmpCreate : Boolean;变量,用于判断图片是否已经生成。
第三步:
给ComboBox1.OnDrawItem和ListBox1.OnDrawItem事件添加内容。
第四步:
在程序退出时释放所创建的图片资源,图片是否创建,由变量BmpCreate来判断;
具体程序如下:
procedure TForm1.FormCreate(Sender: TObject);
begin
BmpCreate := False ;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
appPath:string;
max:integer;
begin
appPath:=ExtractFilePath(application.ExeName);
i:=0;max:=0;
if not bmpCreate then
while i<10 do
begin
bmp[i]:=TBitMap.Create;
try
bmp[i].LoadFromFile(appPath+'zhlimages\'+inttostr(i)+'.bmp');
if bmp[i].Height>max then max:=bmp[i].Height;
listbox1.Items.AddObject('图片 '+inttostr(i),bmp[i]);
ComboBox1.Items.AddObject('图片'+inttostr(i),bmp[i]);
inc(i);
except
halt;
end;
end;
Listbox1.ItemHeight:=max; //set item height
Combobox1.ItemHeight:=max;
bmpCreate:=true;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
bitmap:TBitmap;
offset:integer;
begin
offset:=0;
with(control as TListBox).Canvas do
begin
bitmap:=TBitmap(listbox1.Items.Objects[index]);
fillrect(Rect);
if bitmap<>nil then
begin
brushcopy(Bounds(rect.Left+2,rect.Top+2,bitmap.Width,bitmap.Height),
bitmap,bounds(0,0,bitmap.Width,bitmap.Height),clGreen);
offset:=bitmap.Width+10;
end;
textout(rect.Left+offset,rect.Top,listbox1.Items[index]); //display the text
end;
end;
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
bitmap:TBitmap;
offset:integer;
begin
offset:=0;
with (Control as TComboBox ).Canvas do
begin
bitmap:=TBitmap(Combobox1.Items.Objects[index]);
if bitmap<>nil then
begin
brushcopy(Bounds(rect.Left,rect.Top,bitmap.Width,bitmap.Height),
bitmap,bounds(00,0,bitmap.width,bitmap.Height),clGreen);
end;
offset:=bitmap.Width+10;
textout(rect.Left+offset,rect.Top+2,combobox1.Items[index]);
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
I:integer;
begin
if bmpCreate then
for i:=0 to 9 do
begin
bmp[i].Free;
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -