这还用控件? 自己画不就行了,用不到imagelist
listview+hint就够用了
下面代码是我在实际项目中用的一段 ,你自己改改hint里的draw就行了
TDData=record {我定义了个结构放在item的data里,你把那个图片部分换成你自己的字段就行了}
ID,
Memo,
Picture, {图片}
Music:string;
end;
PDData=^TDData;
procedure OnHintTime(var msg:tmessage);message WM_TIMER;
var
_hint:THintWindow;
HintItem:TListItem;
create部分:
begin
_hint:=THintWindow.Create(self);
_hint.DoubleBuffered:=true;
end;
procedure Tfrm_reslist.LVMouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
var
item:tlistitem;
begin
item:=tlistview(sender).GetItemAt(x,y);
if not (assigned(item) and assigned(item.Data)) then
begin
KillTimer(Handle, 1);
_hint.ReleaseHandle;
HintItem:=nil;
exit;
end;
if (item<>HintItem) then
begin
HintItem:=item;
_hint.ReleaseHandle;
KillTimer(Handle, 1);
SetTimer(Handle, 1, 300, nil);
TListView(Sender).Refresh;
end;
end;
procedure Tfrm_reslist.LVCustomDrawItem(Sender: TCustomListView;
Item: TListItem;
State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
if (HintItem=item) and (not (cdsSelected in State)) then
Sender.Canvas.Brush.Color:=$00E0E0E0
else
Sender.Canvas.Brush.Color:=clWindow;
end;
procedure Tfrm_reslist.OnHintTime(var msg:tmessage);
const
PicWidth=200;
PicHeight=150;
var
pic:tpicture;
Dbmp,Sbmp:tbitmap;
th,tw,tmpi:integer;
_rect,tmprect:trect;
tmppt:tpoint;
HintData
DData;
str,
picname:string;
begin
KillTimer(Handle, 1);
if assigned(HintItem.Data) then
begin
HintData:=HintItem.Data;
Dbmp:=tbitmap.Create;
Sbmp:=tbitmap.Create;
try
with lv.ClientRectdo
begin
tmppt:=clienttoscreen(point(Left+50,HintItem.Position.Y+50));
_rect.Left:=tmppt.X;
_rect.Top:=tmppt.Y;
_rect.Right:=_rect.Left+Right-Left-100;
end;
Dbmp.Width:=PicWidth;
Dbmp.Height:=PicHeight;
picname:=RESPath+HintData.Picture;
{这部分换成你自己纪录图片位置的字段就行了}
if fileexists(picname) then
begin
pic:=tpicture.Create;
try
pic.LoadFromFile(picname);
Sbmp.Assign(pic.Graphic);
SetStretchBltMode(dbmp.Canvas.Handle,HalfTone);
stretchBlt(Dbmp.Canvas.Handle,0,0,PicWidth,PicHeight,
Sbmp.Canvas.Handle,0,0,Sbmp.Width,Sbmp.Height,SRCCOPY);
finally
pic.Free;
end;
end
else
with dbmp.Canvasdo
{无图片}
begin
Brush.Color:=clgray;
FillRect(rect(0,0,PicWidth,PicHeight));
str:='无资源图片';
th:=TextHeight(str);
tw:=TextWidth(str);
TextOut((PicWidth-tw) div 2,(PicHeight-th) div 2,str);
end;
{下面是显示文字,不要可以直接删掉}
tw:=_rect.Right-_rect.Left-PicWidth-15;
{文字输出宽度}
tmprect:=rect(PicWidth+10,5,PicWidth+10+tw,0);
txtstrs.Text:=HintData.Memo;
if txtstrs.Count>20 then
begin
tmpstrs.Clear;
for tmpi:=0 to 19do
tmpstrs.Add(txtstrs.Strings[tmpi]);
str:=tmpstrs.Text;
end
else
str:=txtstrs.Text;
tmpi:=Length(str);
DrawText(_hint.Canvas.Handle,pchar(str),tmpi,tmprect,
DT_WORDBREAK or DT_LEFT or DT_CALCRECT);
th:=tmprect.Bottom-tmprect.Top;;
if th>PicHeight then
_rect.Bottom:=_rect.Top+th+10
else
_rect.Bottom:=_rect.Top+PicHeight+10;
_hint.ActivateHint(_rect,'');
_hint.Refresh;
_hint.Canvas.Draw(5,5,dbmp);
SetBkMode(_hint.Canvas.Handle,Transparent);
DrawText(_hint.Canvas.Handle,pchar(str),tmpi,tmprect,
DT_WORDBREAK or DT_LEFT);
finally
Sbmp.Free;
Dbmp.Free;
end;
end;
end;