Page 1 of 1

Centering window on screen [RESOLVED]

Posted: Wed Dec 07, 2016 7:46 am
by coder14
Why are windows opened with the screen centered flag not centered vertically on the y axis?

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Centering window on screen

Posted: Wed Dec 07, 2016 1:03 pm
by coder14
Still cannot get it to center vertically. A bug?

Re: Centering window on screen

Posted: Wed Dec 07, 2016 8:04 pm
by fsw
Hi,
using:

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
shows that #PB_Window_ScreenCentered really does something.

Using:

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
CocoaMessage(0,WindowID(0),"center")
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
gives exactly the same result.

Doing a little bit more digging reveals this:
  • The window is placed exactly in the center horizontally and somewhat above center vertically.
    Such a placement carries a certain visual immediacy and importance.
This is the source: https://developer.apple.com/reference/a ... 090-center

So the answer is: it's by Apple's UI design :shock:

If you still want to have your window better "somewhat" centered you have to do it with code...
(quick and dirty...)

Code: Select all

ExamineDesktops()

Procedure TryToCenterTheWindow(window)
  ; not 100% centered on Y axis because of titlebar
  Protected xPos = (DesktopWidth(0) / 2) - (WindowWidth(window) / 2)
  Protected yPos = (DesktopHeight(0) / 2) - (WindowHeight(window) / 2) 
  
  ResizeWindow(window, xPos, yPos, #PB_Ignore, #PB_Ignore)
EndProcedure

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
TryToCenterTheWindow(0)

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
This quick and dirty code doesn't take into account how many monitors are used...

Looking at both solutions Apple's placement really seems to be "nicer" to look at.

As aways: Perception is reality...

:mrgreen:

Re: Centering window on screen

Posted: Sun Dec 18, 2016 11:01 am
by coder14
Thanks fsw! Sorry for the late reply. :oops: