0
votes

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

1
What keeps you from using OnMouseLeave()? - AmigoJack
You could use a Timer Control and in each event check using GetCursorPos() whether the cursor is inside the TImage or not and show/hide the form acordingly. - MundoPeter
@AmioJack sorry i meant "OnMouseDown". There is no mouse leave or enter for an image - Coding247
@MundoPeter Could you please give me a small example of how I would use the get cursor function to figure that out? Like a small example of such code - Coding247
According to documentation OnMouseEnter and OnMouseLeve events were added to TImage in Delphi XE4. - SilverWarior

1 Answers

1
votes
  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.