【问题标题】:Display a form while mouse hovers over a Timage component in Delphi鼠标悬停在 Delphi 中的 Timage 组件上时显示表单
【发布时间】:2021-11-02 22:19:18
【问题描述】:

我只想在光标悬停在 TImage 组件上时显示表单,就像提示一样。我可以使用“OnMouseMove”事件来显示表单,但是我不确定鼠标离开图像后如何隐藏表单。我该怎么做?

提前致谢

【问题讨论】:

  • 是什么让你无法使用OnMouseLeave()
  • 您可以使用计时器控件,并在每个事件中使用 GetCursorPos() 检查光标是否在 TImage 内,并相应地显示/隐藏表单。
  • @AmioJack 对不起,我的意思是“OnMouseDown”。没有鼠标离开或进入图像
  • @MundoPeter 你能给我一个小例子,说明我将如何使用 get cursor 函数来解决这个问题吗?像这样的代码的一个小例子
  • 根据文档 OnMouseEnterOnMouseLeve 事件已添加到 Delphi XE4 中的 TImage

标签: delphi delphi-7


【解决方案1】:
  1. 在您的表单中添加一个名为 Timer1 的 TTimer 控件(可能来自系统选项卡控件)。

  2. 将 Timer1 Inteval 属性设置为 100,这意味着它将每 100 毫秒检查一次光标位置(每秒 10 次)。

  3. 将 Timer1 Enabled 属性设置为 True。

  4. 在Timer1的OnTimer事件中添加如下代码:

procedure TForm1.Timer1Timer(Sender: TObject);
var
  oCursorPos: TPoint;
  oImagePos: TPoint;
  bInside: boolean;
begin
  //Get position of Cursor in Screen Coordinates
  GetCursorPos(oCursorPos);

  //Convert coordinates of Image1 from Client to Screen Coordinates
  oImagePos := Self.ClientToScreen(Point(Image1.Left, Image1.Top));

  bInside := (oCursorPos.x >= oImagePos.x) and (oCursorPos.x <= oImagePos.x + Image1.Width) and
             (oCursorPos.y >= oImagePos.y) and (oCursorPos.y <= oImagePos.y + Image1.Height);

  if bInside then
  begin
    //Cursor is over Image1 -> insert code to show the secondary form

  end
  else
  begin
    //Cursor is not over Image1 -> insert code to hide the secondary form

  end;

end;

就是这样。

【讨论】:

  • 可以使用 Delphi 内置的 Mouse.CursorPos 来代替 WindowsAPI 的“GetCursorPos”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多