I have a situation where I need to get the global coordinates of a control. In the past (yesterday) I would just call TrueWindow to get the information on the top level enclosing window. That functionality seems to be missing in 2021r3. The following is what works in 2021r2.
Public Function GlobalLeft(extends r as rectControl) As Double
// For the control referenced, figure out the Global left by walking up the containment hierarchy.
Dim retValue As Double = r.TrueWindow.Left + r.Left
Dim p As RectControl = r.Parent
While p <> Nil
retValue = retValue + p.Left
p = p.Parent
Wend
Return retValue
End Function
Public Function GlobalLeft(extends r as DesktopUIControl) As Double
// For the control referenced, figure out the Global left by walking up the containment hierarchy.
Dim retValue As Double = r.Window.Left + r.Left
Dim p As Object = r.Parent
Dim container as DesktopContainer
While p <> r.Window
if p isa DesktopUIControl then
p = DesktopUIControl( p ).Parent
elseif p isa DesktopContainer then
container = DesktopContainer( p )
retValue = retValue + container.Left
p = container.Parent
end if
Wend
Return retValue
End Function
Parent can give you instances of DesktopUIControl, DesktopContainer, or DesktopWindow. DesktopContainer does inherit from DesktopWindow, so you could just check for DesktopUIControl or DesktopWindow, but I typically like to have a container object for containers.
The code from Dale works with GroupBox too. (Adds the GroupBox left and this is wrong)
Your code see the DesktopGroupBox as a DesktopUIControl and it doesn’t add the left for that control. I don’t know what other Desktop Controls work like that and will need to check for them too. (This is correct)
According to the docs, Parent will be the container if placed on a container, the control that fully contains the control, or the window, whichever is the most local. My tests just yesterday seem to confirm that, though I didn’t test a control on a control on a container, I’d imagine it works.
Not really, you no longer have to account for that offset and can pass over any Object as Parent that isn’t DesktopWindow when calculating position offsets as I show above. It makes conversion complicated, but now the coordinates of controls is always relative to their nearest DesktopWindow (or DesktopContainer).
After testing some more, the original code add the “left” for GroupBox even when the Button “left” is not related to GroupBox. I think that is not what is expected.
I don’t understand what you’re saying, but the function I provided will return the left value of the control relative to the display zero-point in 2021R3, which seems to be what the OP wanted.
What I’m saying is that OP code will not give the correct GlobalLeft if the control is inside a GroupBox, because it will add the GroupBox.Left value and should not add that.
Edit: thank you for explaining. I had to test again and see that the OP code could give different results in some situations (controls inside Rectangle, GroupBox). I updated my post to reflect that.