要实现的功能:在窗体上放一个CheckListBox控件,写OnMouseMove事件中动态显示鼠标滑动时鼠标位置所在项作为Hint显示出来。现象:同一套代码在Delphi7上运行正常,但使用Delphi2007却没显示出Hint,跟没触发OnMouseMove事件一样,没任何效果,是为何?窗体控件CheckListBox的属性设置都一样(showHint=True),Delphi7代码如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst;type
TForm1 = class(TForm)
CheckListBox1: TCheckListBox;
procedure FormCreate(Sender: TObject);
procedure CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
checkListbox1.Items.Clear;
CheckListBox1.Items.Add('我是一个兵');
CheckListBox1.Items.Add('中华人民共和国中华人民共和国');
CheckListBox1.Items.Add('你好,英雄');
end;procedure TForm1.CheckListBox1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
idx: Integer;
begin
idx := CheckListBox1.ItemAtPos(Point(x, y), True);
if idx > -1 then
begin
CheckListBox1.Hint := CheckListBox1.Items[idx] ;
Application.ActivateHint(Point(x,y));
end;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst;type
TForm1 = class(TForm)
CheckListBox1: TCheckListBox;
procedure FormCreate(Sender: TObject);
procedure CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
checkListbox1.Items.Clear;
CheckListBox1.Items.Add('我是一个兵');
CheckListBox1.Items.Add('中华人民共和国中华人民共和国');
CheckListBox1.Items.Add('你好,英雄');
end;procedure TForm1.CheckListBox1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
idx: Integer;
begin
idx := CheckListBox1.ItemAtPos(Point(x, y), True);
if idx > -1 then
begin
CheckListBox1.Hint := CheckListBox1.Items[idx] ;
Application.ActivateHint(Point(x,y));
end;
end;end.
procedure TForm1.CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
idx: Integer;
begin
idx := CheckListBox1.ItemAtPos(Point(x, y), True);
if idx > -1 then
begin
chklst1.Hint := chklst1.Items[idx];
// Application.ActivateHint(Point(x,y)); 去掉
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
CheckListBox1.Items.Clear;
CheckListBox1.Items.Add('我是一个兵');
CheckListBox1.Items.Add('中华人民共和国中华人民共和国');
CheckListBox1.Items.Add('你好,英雄');
CheckListBox1.ShowHint := True; // 加这行
end;
显示不能连续呀,有缺陷,只能鼠标滑到其它控件上或点击下鼠标左键,才能立刻触发文字出来,能够解决及时显示的问题吗?就是鼠标在滑动的时候,如果移到选项上,要立刻显示出来。想到Application.ActivateHint(Point(x,y));这句代码可以帮忙解决问题,但恰好此代码在delphi2007中不能发挥作用,那么,问题究竟可以怎么解决呢?我的问题出在哪?
var
hintWnd: THintWindow;procedure TForm1.FormCreate(Sender: TObject);
begin
chklst1.Items.Clear;
chklst1.Items.Add('我是一个兵');
chklst1.Items.Add('中华人民共和国中华人民共和国');
chklst1.Items.Add('你好,英雄');
/////////////////////////////////////////////////
Application.CreateForm(HintWindowClass, hintWnd);
end;procedure TForm1.chklst1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
idx: Integer;
pt: TPoint;
rect: TRect;
hintStr: string;
begin
GetCursorPos(pt);
Inc(pt.X, 14);
Inc(pt.Y, 12);
idx := chklst1.ItemAtPos(Point(x, y), True);
if idx > -1 then
begin
hintStr := chklst1.Items[idx];
rect := hintWnd.CalcHintRect(180, hintStr, nil);
OffsetRect(rect, pt.x, pt.y);
hintWnd.Color := clInfoBk;
hintWnd.ActivateHint(rect, hintStr);
end
else
hintWnd.ReleaseHandle;
end;procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
pt: TPoint;
begin
GetCursorPos(pt);
if not PtInRect(chklst1.ClientRect, pt) then
hintWnd.ReleaseHandle;
end;
Application.ActivateHint(Point(x,y));
改成
Application.ActivateHint(TControl(Sender).ClientToScreen(Point(x,y)));
就行了!
非常感谢 sololie 专业撸过 的回复,你给出了正确的思路与完整的答案,让我知道此题的解题思路,方向与认识到相关知识,这对我非常重要!给的分数不足以答谢,望各位见谅。