XOJO 2021r1 / Windows 10
I am using 2 monitors and am looking for a way to detect on which monitor my app is launched.
Is there a way to do this in Xojo ?
I know about ‘screen’ that makes it possible to detect the number of screens and the available heigth, width …, but I don’t know how to detect on which screen my app is launched.
Iterate each Screen and test if the Window.Bounds are within the Screen’s Available* properties. In Xojo, additional screens have Top/Left properties as if they were all one continuous screen, so you just need to figure out which rectangle the Window is in.
Resurrecting on this old thread because I currently have to find out a window’s screen myself and currently, being equipped with just my MacBook and not at home, I do not have a multi monitor setting to test.
I did find rather complex approaches, and yes, @MarkusR was right: Rect makes things a lot easier. So if someone with more than one monitor could be so kind as to test this method?
It starts with two extension methods to support the missing property for Screen and Window:
Public Function Rect(extends s as Screen) As Rect
return new rect(s.Left, s.Top, s.Width, s.Height)
End Function
Public Function Rect(extends s as Window) As Rect
return new rect(s.Left, s.Top, s.Width, s.Height)
End Function
And the “real” method, taking into account that a window can occupy more than one screen. So it returns an array of screens, sorted descending by the intersected area of window and screen – the first item will by the screen with the biggest intersected area.
Public Function Screens(extends w as Window) As Screen()
var screens(), result() as Screen
var areas() as Double
var q as integer
for q = ScreenCount -1 downto 0
var sc as screen = Screen(q)
if w.rect.Intersects(sc.rect) then
screens.Append sc
areas.Append w.Rect.Intersection(sc.rect).size.area
end if
next q
areas.Sortwith(screens)
for q = screens.LastIndex downto 0
result.Append screens(q)
next q
return result
End Function
Do you need to know all screens where a window is on?
To know which is the “main screen” for a window, I usually just take the point at the center of the window and check on which screen it is.
Sometimes it might be necessary for me to know all touched screens.
Besides: What will your approach report if you move the window to the bottom of the screen so that its center is below the latter?
Correct, although I get the original question differently.
Right, this can lead to two things:
1: another screen is below the one that contains the window’s title bar. In such cases, the code alone works.
2: there’s no screen below. You get a y coordinate out and no corresponding to any screen. You then have to snap the coordinate to the closest screen.
But this wouldn’t normally happen when the window opens, as the question implies.