I’ve been a little slow to come around to this topic, until I recently hooked up a second screen (I knew there was some potential issues).
But yes, when the Main (parent) Window of my app is open on a second screen - some Modal Windows that I open will always appear on the “Main” computer screen, and not the screen my app is on. I would prefer of course, that these child Windows of the calling Window appear on the same screen.
https://documentation.xojo.com/api/deprecated/window.html#window-locations says that either ParentWindow
or ParentWindowScreen
should do the trick. And they usually do - providing the new child Window has ImplicitInstance set to True
.
But when ImplicitInstance=False
, the DefaultLocation
setting doesn’t appear to apply. And maybe there is a good reasons for that?
Anyway, because a lot of my Modal Windows have ImplicitInstance=False
(because of custom Constructors
), I thought I would be clever and use the Bounds property and Rect to come up with a more dynamic positioning solution. The idea came from @Norman Palardy excellent article here.
After getting a Rect
object of both the opener (parent) Window and the new (child) Window, and experimenting with child.Top = opener.Top
and child.Left = opener.Left
to make sure I wasn’t going to blow up something (which worked, but not with the desired outcome), I thought my “centering” positioning solution could be something like the following:
Var newWin As New MyModalWindow
Var bChild As Rect = newWin.Bounds
Var bParent As Rect = Self.Bounds
bChild.Center = bParent.Center // me, thinking I was being clever (nope)
newWin.Bounds = bChild
newWin.ShowModalWithIn(Self)
The above resulted in my new Modal Window still not on the second screen, but also not where it was before. The Modal moved, but not where I expected it.
I then tried like the following:
...
bChild.VerticalCenter = bParent.VerticalCenter
bChild.HorizontalCenter = bParent.HorizontalCenter
...
Which also didn’t give me what I expected, but it did give me a clue in the IDE Debugger because I saw that the newly assigned bChild.VerticalCenter
was always half of the value of bParent.VerticalCenter
(same with HorizontalCenter
). So I tried:
...
bChild.VerticalCenter = bParent.VerticalCenter * 2
bChild.HorizontalCenter = bParent.HorizontalCenter * 2
...
And it worked!
But what I don’t understand is, why?
Is there some math/geometry here I’m just not getting? Or is this a bug?
What do you folks think? Any feedback on my solution would be appreciated, like - dude, this was covered in the first chapter of …
Here is a link to an example project to demonstrate: https://basic.fyi/screenshots/ModalOpeningOnSecondScreen.xojo_binary_project See App.CenteredWindowGet()
Using Xojo 2019r3.1 (API 2.0)
Tested on: macOS 10.15.3 Catalina & Windows 10 Pro 1909
Note: My solution appears to even work when my screens are arranged like the following (tried on both macOS & Windows):