Recent

Author Topic: [Solved] how can i find master parent ?  (Read 1829 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 508
[Solved] how can i find master parent ?
« on: April 13, 2024, 06:31:21 am »
Hi
please guide and help me

how can i find master parent of  "panel3" ?
master parent is "Form1".
« Last Edit: April 14, 2024, 08:40:04 am by majid.ebru »

440bx

  • Hero Member
  • *****
  • Posts: 4199
Re: how can i find master parent ?
« Reply #1 on: April 13, 2024, 06:44:23 am »
Do you need a cross platform solution or is Windows-only ok ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

majid.ebru

  • Hero Member
  • *****
  • Posts: 508
Re: how can i find master parent ?
« Reply #2 on: April 13, 2024, 06:46:56 am »
i use windows , please say in windows

440bx

  • Hero Member
  • *****
  • Posts: 4199
Re: how can i find master parent ?
« Reply #3 on: April 13, 2024, 07:16:23 am »
There are a number of ways of doing it.

You can use GetAncestor, GetParent or GetTopWindow.

Depending on the function you use, you may need to detect that the returned value isn't the desktop window.

The combination GetAncestor with GA_ROOT will likely do the trick for you.  If not, one of the other two will.  (the reason I'm not giving you a definite answer is because I'm not sure how the LCL manages windows but, one of those will do the "trick".)  In a pure Windows API program, I use GetTopWindow without any problems.

ETA:

I forgot to mention GetTopLevelWindow (it's undocumented - too useful to be documented ;) ) you can find an example which includes its definition and how to use it at https://forum.lazarus.freepascal.org/index.php/topic,53469.msg395532.html#msg395532

HTH.


« Last Edit: April 13, 2024, 07:23:02 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

cdbc

  • Hero Member
  • *****
  • Posts: 1165
    • http://www.cdbc.dk
Re: how can i find master parent ?
« Reply #4 on: April 13, 2024, 07:19:34 am »
Hi
How about:
Code: Pascal  [Select][+][-]
  1. GetParentForm(Self); from forms.pp
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

dsiders

  • Hero Member
  • *****
  • Posts: 1109
Re: how can i find master parent ?
« Reply #5 on: April 13, 2024, 07:29:10 am »
Hi
please guide and help me

how can i find master parent of  "panel3" ?
master parent is "Form1".

Code: Pascal  [Select][+][-]
  1. Panel3.GetTopParent.Name
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

majid.ebru

  • Hero Member
  • *****
  • Posts: 508
Re: how can i find master parent ?
« Reply #6 on: April 13, 2024, 09:50:10 am »
Thank you @dsiders

your code worked.

now how can i find Panel1 ?
the penultimate parent of the panel3 ?
« Last Edit: April 13, 2024, 09:52:28 am by majid.ebru »

wp

  • Hero Member
  • *****
  • Posts: 12031
Re: how can i find master parent ?
« Reply #7 on: April 13, 2024, 10:17:27 am »
now how can i find Panel1 ?
the penultimate parent of the panel3 ?

A good exercise in recursive thinking:
Code: Pascal  [Select][+][-]
  1. function PenultimateParentOf(C: TWinControl): TWinControl;
  2. var
  3.   C0: TWinControl;
  4.   P: TWinControl;
  5.   PP: TWinControl;
  6. begin
  7.   C0 := C;
  8.   P := C.Parent;
  9.   if P = nil then
  10.   begin
  11.     Result := nil;
  12.     exit;
  13.   end;
  14.  
  15.   PP := P.Parent;
  16.   while PP <> nil do
  17.   begin
  18.     C := P;
  19.     P := C.Parent;
  20.     PP := P.Parent;
  21.   end;
  22.   if C = C0 then
  23.     Result := nil
  24.   else
  25.     Result := C;
  26. end;
  27.  
  28. procedure TForm1.FormCreate(Sender: TObject);
  29. var
  30.   P: TWinControl;
  31. begin
  32.   P := PenultimateParentOf(Panel4);
  33.   if P = nil then
  34.     Caption := 'no penultimate parent'
  35.   else
  36.     Caption := P.Name;
  37. end;  

paweld

  • Hero Member
  • *****
  • Posts: 1035
Re: how can i find master parent ?
« Reply #8 on: April 13, 2024, 10:31:02 am »
The TWinControl class has a HasParent property:
Code: Pascal  [Select][+][-]
  1. function PenultimateParentOf(C: TWinControl): TWinControl;
  2. begin
  3.   Result := C;
  4.   while Result.HasParent and Result.Parent.HasParent do
  5.     Result := Result.Parent;
  6.   if Result = C then
  7.     Result := nil;
  8. end;
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. var
  12.   P: TWinControl;
  13. begin
  14.   P := PenultimateParentOf(Panel4);
  15.   if P = nil then
  16.     Caption := 'no penultimate parent'
  17.   else
  18.     Caption := P.Name;
  19. end;
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018