有一个带有一些长项目的ListBox。这些长项超出了ListBox的右边缘,这里有一个想法,当鼠标悬停在它们上面时显示这些项目的提示。
我找到了一个例子:(来自http://delphi.about.com/cs/adptips2001/a/bltip0201_4.htm)
procedure TForm1.ListBox1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
var lstIndex : Integer ;
begin
with ListBox1 do
begin
lstIndex:=SendMessage(Handle, LB_ITEMFROMPOINT, 0, MakeLParam(x,y)) ;
if (lstIndex >= 0) and (lstIndex <= Items.Count) then
Hint := Items[lstIndex]
else
Hint := ''
end;
end;
它有效,但每次我想查看另一个项目的提示时,我必须将鼠标从ListBox移开,然后指向另一个项目以查看其提示。有没有办法在不将鼠标移离ListBox边框的情况下查看每个项目的提示?
答案 0 :(得分:11)
var fOldIndex: integer = -1;
procedure TForm1.ListBox1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
var lstIndex : Integer ;
begin
with ListBox1 do
begin
lstIndex:=SendMessage(Handle, LB_ITEMFROMPOINT, 0, MakeLParam(x,y)) ;
// this should do the trick..
if fOldIndex <> lstIndex then
Application.CancelHint;
fOldIndex := lstIndex;
if (lstIndex >= 0) and (lstIndex <= Items.Count) then
Hint := Items[lstIndex]
else
Hint := ''
end;
end;