首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

ActiveHint在Delphi7与Delphi2007间差异

2013-08-01 
ActiveHint在Delphi7与Delphi2007间差别要实现的功能:在窗体上放一个CheckListBox控件,写OnMouseMove事件

ActiveHint在Delphi7与Delphi2007间差别
要实现的功能:在窗体上放一个CheckListBox控件,写OnMouseMove事件中动态显示鼠标滑动时鼠标位置所在项作为Hint显示出来。

现象:同一套代码在Delphi7上运行正常,但使用Delphi2007却没显示出Hint,跟没触发OnMouseMove事件一样,没任何效果,是为何?窗体控件CheckListBox的属性设置都一样(showHint=True),Delphi7代码如下:

unit Unit1;

interface

uses
  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.
[解决办法]


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;


[解决办法]
ActivateHint要求传的是屏幕坐标,所以把
Application.ActivateHint(Point(x,y));
改成
Application.ActivateHint(TControl(Sender).ClientToScreen(Point(x,y)));
就行了!

热点排行