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:
In your form add a TTimer control (probably from the System Tab Control), named Timer1.
Set the Timer1 Inteval property to 100, which means it will check the cursor position every 100 milliseconds, (10 times in a second).
Set the Timer1 Enabled Property to True.
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.