Find out which display is a given Window opened on

Hello:

Does the Xojo Framework provide the capability of knowing which display (in a multi-monitor setup) a given Window is currently displayed on for Mac, Windows, and Linux or should I look into using OS-specific APIs for that via Declare statement?

The framework doesn’t provide a “which screen” function, but it’s relatively simple math that has been done so many times on here that if you ask an AI I’m sure it will regurgitate stolen code you can use.

1 Like

It’s relatively simple. There are two schools of thought to deciding which screen a Windows is visible on, since there can be multiple answers. The simpler is deciding based on the top left corner, and the other is majority overlap. Majority overlap is the technique I use.

The idea is you get your window bounds, get the bounds for each screen, and see which screen has the largest amount of intersection.

Function GetCurrentDisplay (Extends Win As DesktopWindow) As DesktopDisplay
  Var WinBounds As Xojo.Rect = Win.Bounds
  Var LargestScreen As DesktopDisplay
  Var LargestArea As Double
  For ScreenIndex As Integer = 0 To DesktopDisplay.LastDisplayIndex
    Var Screen As DesktopDisplay = DesktopDisplay.DisplayAt(ScreenIndex)
    Var ScreenBounds As New Xojo.Rect(Screen.Left, ScreenTop, Screen.Width, Screen.Height)
    Var Overlap As Xojo.Rect = ScreenBounds.Intersection(WinBounds)
    Var Area As Double = Overlap.Width * Overlap.Height
    If Area > LargestArea Then
      LargestArea = Area
      LargestScreen = Screen
    End If
  Next
  Return LargestScreen
End Function
1 Like