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