Calculate the remaining space to the edge of the screen?

Hello Xojo boys and girls,
I have a child window that should be displayed next to the main window of my app. On the right if there is space otherwise on the left. How to calculate the remaining space relative to the edge of the screen?

You can use the following to get the dimensions of the screen (assuming you only have 1, if you have more than one you will need a function to figure out which screen your window is on):

Var MainDisplay as DesktopDisplay = DesktopDisplay.DisplayAt(0)
RemainingSpace = MainDisplay.Width - (MainWindow.Left + MainWindow.Width)

For more than one screen you would have to loop around DisplayAt( number ) to figure which your window is on. Then use that number to find the display, as above (instead of 0).

1 Like

Thanks you. It’s Ok. But so, out of curiosity, I would like to know how to determine on which screen the app (or rather its main window) is displayed Screen(0) or Screen(1) or other.

It is possible for a machine with multiple monitors to have a single window occupy parts of 2+ monitors, so you have to decide how you want to determine “which monitor” it is on. Do you use the top left? Center point? Highest % of window size on each monitor? Etc. So YMMV may vary based on use case. But here is a starting point from a forum thread in 2017:

Public Function WhichScreen(x as integer, y as integer) as integer 
  for i as integer = 0 to ScreenCount 
    if x >= Screen(i).left and x <= screen(i).width + Screen(i).left and _
       y >= screen(i).top and y <= screen(i).height + screen(i).top then 
      return i 
    end if 
End Function

Edit: code indentation

Yes, that’s what I was going to provide. Except that ScreenCount should be ScreenCount - 1 (ie the screen count could be 2, which would be 0 and 1).

The coordinate space is continuous so one monitor may run from 0 to 999 and another next to it 1000 to 1999. The monitors can be stacked or arranged in just about any way. including almost diagonal (although there has to be some overlap). The main monitor is screen 0. If screen 1 is to it’s left or above it you will get negative coordinates for it.

The API2 version of that function is as follows:

Public Function WhichScreen(x As Integer, y As Integer) As Integer
  Var oDisplay As DesktopDisplay
  For iDisplay As Integer = 0 To DesktopDisplay.LastDisplayIndex
    oDisplay = DesktopDisplay.DisplayAt( iDisplay )
    If x >= oDisplay.Left And x <= oDisplay.Width + oDisplay.Left And _
      y >= oDisplay.Top And y <= oDisplay.Height + oDisplay.Top Then
         Return iDisplay
    End If
  Return -1
End Function

As for which point on your window to use to pass into the function. I tend to use the centre of the window as it is very easy for the edges to stray off the screen edge. If you pass top left and it is off the side of all displays then the function will return nothing, which defaults to 0, which can lead to odd results. The return -1 would allow the calling function to detect this and act accordingly.