Centering a Window

Two ways:

  1. If you’re already reading the width and the height of the screen to resize the window, you can do some more math to center the window in that screen.

[code]Dim tScreen as screen = screen( 0 )

Dim newFrame as new realbasic.rect( 0, 0, tScreen.availableWidth * 0.8, tScreen.availableHeight * 0.8 )

newFrame.left = ( ( tScreen.availableWidth - newFrame.width ) * 0.5 ) + tScreen.availableLeft
newFrame.top = max( ( ( tScreen.availableHeight - newFrame.height ) * 0.5 ) + tScreen.availableTop, tScreen.availableTop )

me.bounds = newFrame[/code]
I use Available so it’s centered in the available space, and clamp it so that it doesn’t go under the menubar. This solution is x-plat

  1. Use a macOS declare to center the window.

declare sub NSWindowCenter lib "AppKit" selector "center" ( NSWindowInstance as integer ) NSWindowCenter( me.handle )