我在网上找了下面一段代码,在LISTBOX中加入了图片,请问怎么实现像QQ那样头像闪动。
-----------------------------------------------------------------------------------------------------------
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
//ListBox.OnDrawItem事件
var
ImgRect, MemoRect: TRect;
Cnvs: TCanvas;
B: TBitmap;
S, Memo: string;
iPos: Integer;
begin
Cnvs := ListBox1.Canvas;
ImgRect := Rect;
ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域 if odSelected in State then
begin
Cnvs.Brush.Color := clSkyBlue;
Cnvs.Pen.Color := clNavy;
end
else
begin
Cnvs.Brush.Color := ListBox1.Color;
Cnvs.Pen.Color := ListBox1.Color;
end;
Cnvs.Brush.Style := bsSolid;
Cnvs.Rectangle(Rect); Cnvs.Brush.Style := bsClear;
if ImageList.Count > 0 then
begin
B := TBitmap.Create;
try
ImageList.GetBitmap(0, B);
B.PixelFormat := pf24bit;
B.Transparent := True;
InflateRect(ImgRect, -2, -2);
Cnvs.StretchDraw(ImgRect, B);
finally
B.Free;
end;
end; Rect.Left := ImgRect.Right;
MemoRect := Rect;
MemoRect.Top := (Rect.Top + Rect.Bottom) div 2;
Rect.Bottom := MemoRect.Top;
S := ListBox1.Items[Index]; iPos := Pos(#9, S);
Memo := Copy(S, iPos + 1, Length(S)); //提取备注
S := Copy(S, 1, iPos - 1); //提取名称
Cnvs.Font.Assign(ListBox1.Font);
DrawText(Cnvs.Handle, PChar(S), -1,
Rect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字 Cnvs.Font.Color := clGrayText;
DrawText(Cnvs.Handle, PChar(Memo), -1,
MemoRect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawVariable; //自己画每一项,项高度可调
ListBox1.ItemHeight := 30;
ListBox1.Clear;
//名称+#9+备注
ListBox1.Items.Add('yujinfree'#9 + '昨日足迹');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
ListBox1.Items.Add('yujinfree'#9 + '昨日足迹');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
end;
-----------------------------------------------------------------------------------------------------------
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
//ListBox.OnDrawItem事件
var
ImgRect, MemoRect: TRect;
Cnvs: TCanvas;
B: TBitmap;
S, Memo: string;
iPos: Integer;
begin
Cnvs := ListBox1.Canvas;
ImgRect := Rect;
ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域 if odSelected in State then
begin
Cnvs.Brush.Color := clSkyBlue;
Cnvs.Pen.Color := clNavy;
end
else
begin
Cnvs.Brush.Color := ListBox1.Color;
Cnvs.Pen.Color := ListBox1.Color;
end;
Cnvs.Brush.Style := bsSolid;
Cnvs.Rectangle(Rect); Cnvs.Brush.Style := bsClear;
if ImageList.Count > 0 then
begin
B := TBitmap.Create;
try
ImageList.GetBitmap(0, B);
B.PixelFormat := pf24bit;
B.Transparent := True;
InflateRect(ImgRect, -2, -2);
Cnvs.StretchDraw(ImgRect, B);
finally
B.Free;
end;
end; Rect.Left := ImgRect.Right;
MemoRect := Rect;
MemoRect.Top := (Rect.Top + Rect.Bottom) div 2;
Rect.Bottom := MemoRect.Top;
S := ListBox1.Items[Index]; iPos := Pos(#9, S);
Memo := Copy(S, iPos + 1, Length(S)); //提取备注
S := Copy(S, 1, iPos - 1); //提取名称
Cnvs.Font.Assign(ListBox1.Font);
DrawText(Cnvs.Handle, PChar(S), -1,
Rect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字 Cnvs.Font.Color := clGrayText;
DrawText(Cnvs.Handle, PChar(Memo), -1,
MemoRect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawVariable; //自己画每一项,项高度可调
ListBox1.ItemHeight := 30;
ListBox1.Clear;
//名称+#9+备注
ListBox1.Items.Add('yujinfree'#9 + '昨日足迹');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
ListBox1.Items.Add('yujinfree'#9 + '昨日足迹');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
end;
另
你这个代码是直接把图画在listbox的canvas上,而且是靠着listbox的ondrawitem来实现的,qq的闪动是图片上下左右位置的改变,靠listbox的ondrawitem来实现勉强些,只能不断刷新不断重绘,不仅速度慢而且会产生很多问题,想要弄个形似的,还是自己做个控件比较好