下拉列表框如何正确显示提示提示(100分)

X

xx_wxg

Unregistered / Unconfirmed
GUEST, unregistred user!
鼠标在下拉列表框中移动时如何正确显示出鼠标所指项的内容?
 
你是指过长的时候显示一个hint? 嘿嘿,看看下面这个效果

界面上扔个combobox,所有属性默认
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);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ComboBox1CloseUp(Sender: TObject);
private
{ Private declarations }
_hint:THintWindow;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ComboBox1DrawItem(Control: TWinControl;
Index: Integer;
Rect: TRect;
State: TOwnerDrawState);
var
arect:trect;
tpt:tpoint;
strwidth:integer;
begin
with combobox1do
begin
Canvas.FillRect(rect);
Canvas.TextOut(rect.Left+2,rect.Top,Items[index]);
strwidth:=canvas.TextWidth(items[index]);
if (strwidth<Width-4) or (odComboBoxEdit in state) then
_hint.ReleaseHandle
else
if (odSelected in state) then
begin
tpt:=clienttoscreen(point(rect.Left,rect.Top+height));
arect.Left:=tpt.X;
arect.Top:=tpt.Y+rect.Bottom-rect.Top;
arect.Right:=arect.Left+strwidth+4;
arect.Bottom:=arect.Top+rect.Bottom-rect.Top;
_hint.ActivateHint(arect,items[index])
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow.Create(self);
combobox1.Items.Clear;
for i:=0 to 9do
begin
str:='';
setlength(str,31);
for j:=1 to 30do
str[j]:=inttostr(i)[1];
str[31]:='A';
{测试数据,检测是否显示全最后一个字符}
combobox1.Items.Add(str);
end;
combobox1.Items.Add('短字符');
combobox1.Style:= csOwnerDrawFixed;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
_hint.Free;
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
_hint.ReleaseHandle;
end;

end.
 
就像hint提示,用鼠标选中列表框的某一项然后显示hint提示一样,鼠标在列表框
移动时如何在hint中显示鼠标所指向的那一项的内容,而不是单击那一项。是列表框
非组合框
 
? 是啊......我给的例子不就是这样吗? 不需要单机啊,不过只有超长的时候会显示,如果你想不超长也显示,把strwidth<Width-4) or 这个条件去掉就行了
 
procedure TForm1.ListBox1MouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
var
i: Integer;
begin
i := ListBox1.ItemAtPos(Point(x,y),True);
if i >= 0 then
ListBox1.Hint := ListBox1.Items;
end;
 
两位仁兄的答案都能显示hint,但需要将鼠标移出列表框再移进来才能显示其他的项目提示。只在列表框中上下移动鼠标只显示1次hint就再也不显示了。例如鼠标从列表框的第一项向下移动,这时只显示第一项的提示,即使我把鼠标指向第三项hint也不再出现。这个问题如何解决?苦恼。。。
 
不会啊 ,我那个没有你说的问题
 
这个很复杂的
是不是取得鼠标下面(没有点击)这一行的内容?
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
752
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
745
SUNSTONE的Delphi笔记
S
D
回复
0
查看
873
DelphiTeacher的专栏
D
顶部