设置提示时创建的MouseMove事件

Ste*_*eve 1 delphi

我在XP机器上使用Delphi 6.

我在stringgrid中使用Onmousemove来获取单元格的内容.然后我使用单元格内容来设置提示.然后我使用Application.ActivateHint来显示提示.但每次更新提示值时,操作系统都会发送另一个MouseMove事件.这会导致提示非常糟糕的闪烁.

我知道鼠标没有移动,但我充斥着MouseMove事件.mousemove导致提示更新导致鼠标移动导致提示更新等.

Rem*_*eau 7

你采取了完全错误的方法.而不是使用OnMouseMove事件手动设置Hint和调用Application.ActivateHint(),让VCL为您处理一切.

使用该TApplication.OnShowHint事件,或者使用StringGrid子类来拦截CM_HINTSHOW消息,以自定义StringGrid的本机提示的行为方式.这两种方法都允许您访问THintInfo记录,这允许您在显示/更新之前自定义当前提示.特别是,该THintInfo.CursorRect成员允许您设置VCL用于跟踪鼠标的矩形,并决定何时/是否需要触发新OnShowHint事件或CM_HINTSHOW消息以更新当前提示,同时鼠标仍在显示的控件内提示.这个更新比什么更清晰,更无缝TApplication.ActivateHint().

例如:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnShowHint := AppShowHint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Application.OnShowHint := nil;
end;

procedure TForm1.AppShowHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);
var
  Col, Row: Longint;
begin
  if HintInfo.HintControl := StringGrid1 then
  begin
    StringGrid1.MouseToCell(HintInfo.CursorPos.X, HintInfo.CursorPos.Y, Col, Row);
    if (Col >= 0) and (Row >= 0) then
    begin
      HintInfo.CursorRect := StringGrid1.CellRect(Col, Row);
      HintInfo.HintStr := StringGrid1.Cells[Col, Row];
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

要么:

private
  OldWndProc: TWndMethod;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OldWndProc := StringGrid1.WindowProc;
  StringGrid1.WindowProc := StringGridWndProc;
end;

procedure TForm1.StringGridWndProc(var Message: TMessage);
var
  HintInfo: PHintInfo;
  Col, Row: Longint;
begin
  if Message.Msg = CM_HINTSHOW then
  begin
    HintInfo := PHintInfo(Message.LParam);
    StringGrid1.MouseToCell(HintInfo.CursorPos.X, HintInfo.CursorPos.Y, Col, Row);
    if (Col >= 0) and (Row >= 0) then
    begin
      HintInfo.CursorRect := StringGrid1.CellRect(Col, Row);
      HintInfo.HintStr := StringGrid1.Cells[Col, Row];
      Exit;
    end;
  end;
  OldWndProc(Message);
end;
Run Code Online (Sandbox Code Playgroud)

如果希望在单元格内的每次鼠标移动时更新提示,只需THintInfo.CursorRect在当前THintInfo.CursorPos位置设置为1x1矩形.如果您希望即使未移动鼠标也会定期更新提示,请将其设置为THintInfo.ReshowTimeout非零间隔(以毫秒为单位).