style设置成ownerdrowfix
为了把高度设置合适,把font中字体的大小设为 小三
strings 只输入一个回车,如果strings中没有内容的话,drawitem事件将无法激发.在drowitem事件里写代码:var bm : TBitmap ;
begin
bm := TBitmap.creat ;
bm.loadfromfile(inttostr(index + 1) + '-1.bmp');{index 从0开始,而我的图片名从 1-1.bmp 开始}
combobox1.items.addobject('',TObject(bm));
combobox1.canvas.draw(Rect.Left,Rect.Top,bm);
bm.free ;
end ;出现如下问题:
1,在单击combobox下拉三角时,弹出“Can't draw canvas”的错误,按确定后再次下拉combobox则不再提示,能够正确显示。
2,当下拉头像到第20个(文件名为20-1。bmp)时,出现“stream read error”的错误。请问是什么原因? 谢谢
为了把高度设置合适,把font中字体的大小设为 小三
strings 只输入一个回车,如果strings中没有内容的话,drawitem事件将无法激发.在drowitem事件里写代码:var bm : TBitmap ;
begin
bm := TBitmap.creat ;
bm.loadfromfile(inttostr(index + 1) + '-1.bmp');{index 从0开始,而我的图片名从 1-1.bmp 开始}
combobox1.items.addobject('',TObject(bm));
combobox1.canvas.draw(Rect.Left,Rect.Top,bm);
bm.free ;
end ;出现如下问题:
1,在单击combobox下拉三角时,弹出“Can't draw canvas”的错误,按确定后再次下拉combobox则不再提示,能够正确显示。
2,当下拉头像到第20个(文件名为20-1。bmp)时,出现“stream read error”的错误。请问是什么原因? 谢谢
有点改进就是AddItem()的方法不能再drawitem方法内调用。
所有的item必须在初始化的时候(比如form.oncreate函数内)调用完成。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
combobox1.canvas.draw(Rect.Left, Rect.Top,
TBitmap(ComboBox1.Items.Objects[Index]));
end;procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
bmp: tbitmap;
begin
ComboBox1.Style := csOwnerDrawFixed;
ComboBox1.DropDownCount := 2;
ComboBox1.ItemHeight := 32;
ComboBox1.Width := 52;
ComboBox1.OnDrawItem := ComboBox1DrawItem;
for I := 1 to 5 do
begin
bmp := Tbitmap.Create;
bmp.Height := 32;
bmp.Width := 32;
bmp.Transparent := true;
bmp.LoadFromFile('d:\7200000' + inttostr(I) + '.bmp');
ComboBox1.AddItem('', Tobject(Bmp));
end;
end;end.新建工程--〉覆盖unit1单元的内容,--〉访如combox --〉运行
我把qq中的newface目录拷到我的目录下,挺有意思地unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
TMainForm = class(TForm)
ComboBox1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
FItemCount: Integer;
public
{ Public declarations }
end;var
MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.FormCreate(Sender: TObject);
var
f: TextFile;
s: string;
i: Integer;
begin
AssignFile(f, ExtractFilePath(ParamStr(0)) + '\newface\face.ini');
ReSet(f);
ReadLn(f, FItemCount);
i := 0;
while Not Eof(f) do
begin
ReadLn(f, s);
if i mod 3 = 0 then
ComboBox1.Items.Add(s);
Inc(i);
end;
CloseFile(f);
end;procedure TMainForm.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
Bmp: TBitmap;
SrcRect: TRect;
begin
Bmp := TBitmap.Create;
try
Bmp.LoadFromFile(ExtractFilePath(ParamStr(0)) + '\newface\' + ComboBox1.Items[Index]);
Bmp.Transparent := True;
Bmp.TransparentColor := clTeal;
ComboBox1.Canvas.FillRect(Rect);
ComboBox1.Canvas.Draw(Rect.Left, Rect.Top, Bmp);
finally
Bmp.Free;
end;
end;end.