Launch on specific screen

If I work out what screen an app is running in at the time it closes, how can I ensure that it launches on the same screen next time?
(Customer with two screens complains that my apps always open on screen (0) )

On Windows I would save the top & left values to the registry & read them back on next launch checking that those positions were available now (in case the external monitor had been removed).

Save window position:

call ParentWindow.NSWindowMBS.saveFrameUsingName(WindowTitle)

Load window position:

call ParentWindow.NSWindowMBS.setFrameUsingName(WindowTitle)

Doing the calculations for loading the window position is a major PITA.

1 Like

Interesting that it uses title. I like the idea, but NS functions aren’t cross platform sadly (I realise I didnt say it needed to be)

Top and Left - didnt realise that these continued over two screens - I had the feeling that x,y co-ordinates also needed a screen number to make sense - like 20,40 could exist on two differnt screens.

You can use Wayne’s approach on Windows and the MBS call on macOS.

Just be aware that you’ll run into positioning issues (when only using “top” and “left”) on TargetWindows if the 2 (or 3) monitors have different Scale Factors.
Long story with this Xojo Issues (multi monitor, multi scale factors)…

That’s why I’m using this: jo-tools/monitors: Xojo example project: Monitors (github.com)

Note the right hand side of the example (which is using Windows API):

  • Labels: “IsOnMonitor” and “at relative Monitor Pos”
    • to read the Window position (e.g. before closing)
  • Button "Position on Screen [num] at Pos (x, y) → button [Pos] (on the right hand side, using Windows API)
    • to restore the previously saved position when opening the window
2 Likes