Recent

Author Topic: MouseLeave on TPanel  (Read 1353 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
MouseLeave on TPanel
« on: February 04, 2021, 01:07:59 pm »
Hello guys :)

Just wanted to test This :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Panel2MouseLeave(Sender: TObject);
  2. begin
  3.   Panel2.Visible:= False;
  4. end;
  5.  

But the Panel has Some TEdit's in it and when i hover over those the Panel MouseLeave Event gets Triggered to0.
is there any way to connect those object's ?
So the panel gets Invisible when the mouse leaves the Panel, but stay's visible when i hover over a TEdit Field.

the Idea: i wanted to create a small Form/Window, wich appears when i click on a Button. On this Panel i can choose between some Informations.
After i have choosen 1, the form/window will get Invisible again. But when i just leave it without selecting something it should count as a canceld Operation
and the Window/form should close again.

Works Great but like i said, i don't leave the panel but MouseLeave event gets triggered anyway.

Thanks:) and Excuse my English

Handoko

  • Hero Member
  • *****
  • Posts: 5532
  • My goal: build my own game engine using Lazarus
Re: MouseLeave on TPanel
« Reply #1 on: February 04, 2021, 06:21:51 pm »
That's easy, just make it visible again. Tested on Linux only but it should work on Windows too:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Panel1MouseLeave(Sender: TObject);
  2. begin
  3.   Panel1.Visible := False;
  4. end;
  5.  
  6. procedure TForm1.Edit1MouseEnter(Sender: TObject);
  7. begin
  8.   Panel1.Visible := True;
  9. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: MouseLeave on TPanel
« Reply #2 on: February 04, 2021, 07:32:11 pm »
Or, probably better, get the cursor position and check whether it's still inside the panel's bounding rectangle or has gone out.

Let me see if I can find the specific info to write a small example; this kind of thing is not something I use every day :-[

After some testing ...

Ok, here it is. Just an extremely simple example of how to test whether the mouse is still in the panel, whether over the panel itself or a control inside it:

Code: Pascal  [Select][+][-]
  1. {Test system: Laz/FPC 2.0.10/3.2.0 x86_64-linux-gtk2}
  2. procedure TForm1.AnyPanelMouseLeave(Sender: TObject);
  3. var
  4.   mpos: TPoint;
  5.   ThePanel: TPanel;
  6. begin
  7.   if (Sender is TPanel) then begin
  8.     ThePanel := Sender as TPanel;
  9.     mpos := ScreenToClient(Mouse.CursorPos);
  10.     if ThePanel.BoundsRect.Contains(mpos) then
  11.       ThePanel.Caption := 'Still inside ' + ThePanel.Name
  12.     else
  13.       ThePanel.Hide;
  14.   end;
  15. end;

I tested with a small program with two panels, one of which also had a couple edit controls.

ETA:
Forgot to comment on this:
Works Great but like i said, i don't leave the panel but MouseLeave event gets triggered anyway.

Note that from the mouse's point of view you do leave the panel to enter another control.

Think about it like this: Imagine, if you will, a big box over a carpet: if you enter (get into) the box, you leave (your feet are no longer directly on) the carpet, even if the carpet is still there under the box.

That's the general idea of MouseEnter/MouseLeave*. ;)


Not saying that you're a mouse, o.c.; it's just an example! :D
« Last Edit: February 04, 2021, 08:06:48 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018