How to center a window on the screen

Hello All,

I want the windows of my application to always open on the center of computer screen.
Is there a way of getting width and height settings of the monitor to use in centering the windows?

You can get the width and height of the main screen using Screen(0).Width & Screen(0).Height, there are some other useful Screen properties and the ScreenCount() function too, check the Language Reference.

(screen(0).Width - MyWindow.Width) / 2 will center the Window horizontally
(screen(0).Height - MyWindow.Height) / 2 will center the Window vertically

1 Like

Thanks All. That worked.

Hey guys, here’s a better solution that will center the window on the Active, Front Most Screen:

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

taken from
https://forum.xojo.com/52827-centering-a-window/0

[quote=491453:@Jim Underwood]Hey guys, here’s a better solution that will center the window on the Active, Front Most Screen:

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

taken from
https://forum.xojo.com/52827-centering-a-window/0[/quote]

I believe that would be macOS only?

Yes, you are correct. I should have so stated.
But it is the only way I have found to open a Xojo app window in the user’s Active Screen, on a Mac.

[quote=491510:@Jim Underwood]Yes, you are correct. I should have so stated.
But it is the only way I have found to open a Xojo app window in the user’s Active Screen, on a Mac.[/quote]
All good, hopefully it helps out other people on macOS

[quote=28318:@Chris Verberne](screen(0).Width - MyWindow.Width) / 2 will center the Window horizontally
(screen(0).Height - MyWindow.Height) / 2 will center the Window vertically[/quote]

In the case of a a multi-screen setup, it might be a good idea to determine on what screen the window is located.

I think you could iterate through the screens, check the LEFT, TOP, WIDTH and HEIGHT properties. Check these values to see if the TOP and LEFT properties of the window is in the range of the screen. That would be the screen to center on.

Otherwise, a window could be jump to the main-screen instead of the screen where it was.

When using the Window.Bounds property you include the frame and menubar in the dimensions.

[quote=28318:@Chris Verberne](screen(0).Width - MyWindow.Width) / 2 will center the Window horizontally
(screen(0).Height - MyWindow.Height) / 2 will center the Window vertically[/quote]

For the newbies, the whole code will be looking like this (placed under the open event for instance):

// Center Window self.Left = (screen(0).Width - self.Width) / 2 self.top = (screen(0).Height - self.Height) / 2

You can also use

self.Left = (screen(0).AvailableWidth - self.Width) / 2 self.top = (screen(0).AvailableHeight - self.Height) / 2

[quote]AvailableWidth
The available width (in pixels) of the screen, taking into account the taskbar if placed on the left or right of the specified screen.[/quote]

[quote]AvailableHeight
The available height (in pixels) of the specified screen, taking into account the menubar and/or the taskbar (Dock on macOS), if present.[/quote]

https://documentation.xojo.com/Screen

2 Likes

if you’re trying to center the entire window get & set the BOUNDS not left & top
Left and Top refer to the content region - see http://documentation.xojo.com/api/deprecated/window.html#window-left and http://documentation.xojo.com/api/deprecated/window.html#window-top
Bounds includes the frame toolbars etc - see http://documentation.xojo.com/api/deprecated/window.html#window-bounds