关于用TComboBox模仿QQ选取头像的下拉列表的问题。

大大怪老张 2002-12-09 06:26:13
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”的错误。

请问是什么原因? 谢谢
...全文
86 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizongqi 2002-12-26
  • 打赏
  • 举报
回复
谢谢
大大怪老张 2002-12-20
  • 打赏
  • 举报
回复
我找到原因了。

可能是delphi的image控件的问题或者是加载的图片的文件格式的问题。

在qq的头像里,大概有6—8个图片会出错。
大大怪老张 2002-12-18
  • 打赏
  • 举报
回复
楼上的,你的代码可以看到 20-1.bmp以后吗?

我照着写了,还是不行 :(
xzgyb 2002-12-18
  • 打赏
  • 举报
回复
我的能看到阿
My_first 2002-12-11
  • 打赏
  • 举报
回复
用devexpress组件里的控件。
xzgyb 2002-12-11
  • 打赏
  • 举报
回复
偶也弄了一个,跟findcsdn类似,好像比它的麻烦。
我把qq中的newface目录拷到我的目录下,挺有意思地

unit Unit1;

interface

uses
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.
findcsdn 2002-12-11
  • 打赏
  • 举报
回复
我试了一下,挺好玩的,没有任何错误发生。
有点改进就是AddItem()的方法不能再drawitem方法内调用。
所有的item必须在初始化的时候(比如form.oncreate函数内)调用完成。



unit Unit1;

interface

uses
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 --〉运行。。。
tohappy 2002-12-11
  • 打赏
  • 举报
回复
我有能力帮助你,请看我站http://www.to-happy.com《大事》里的个性设计的更图部分。
nico_0823 2002-12-10
  • 打赏
  • 举报
回复
试试comboboxex控件(在sys32控件面板上)
大大怪老张 2002-12-10
  • 打赏
  • 举报
回复
up
大大怪老张 2002-12-10
  • 打赏
  • 举报
回复
我的d5里没找到comboboxex控件。所以只好用combobox 。

对上面的问题,哪位给个解释呀。。。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧