Home > Enterprise >  Display a form while mouse hovers over a Timage component in Delphi
Display a form while mouse hovers over a Timage component in Delphi

Time:09-17

I want to display a form only while the cursor is hovering over a TImage component just like a hint. I am able to use the "OnMouseMove" event to show the form however I am unsure as to how I could hide the form once the mouse leaves the image. How can I do that?

Thanks in advance

CodePudding user response:

  1. In your form add a TTimer control (probably from the System Tab Control), named Timer1.

  2. Set the Timer1 Inteval property to 100, which means it will check the cursor position every 100 milliseconds, (10 times in a second).

  3. Set the Timer1 Enabled Property to True.

  4. Add The following code to the OnTimer Event of Timer1:

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;

That's all.

  • Related