Only X coordinate of Window centered

Windows specific forum
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Only X coordinate of Window centered

Post by Jan2004 »

I want to have window x coordinate centered, but 50 pxls y coordinate (50 pxls from top of the screen). My code is below. Is simpler way to achieve it?

Code: Select all

Define Result

If OpenWindow(0, 0, 0, 1200, 700, "PureBasic Window", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
    Result = WindowX(0 ,#PB_Window_FrameCoordinate) 
    ResizeWindow(0, Result, 50, #PB_Ignore, #PB_Ignore ) 

  
  ;

  Repeat
    Event = WaitWindowEvent()

    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf

  Until Quit = 1
  
EndIf

End  
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Only X coordinate of Window centered

Post by jassing »

Jan2004 wrote: Fri Jun 30, 2023 7:51 pm I want to have window x coordinate centered, but 50 pxls y coordinate (50 pxls from top of the screen). My code is below. Is simpler way to achieve it?
I think that's a good way to go. I would create the window with #PB_Window_Invisible, then use HideWindow(..., #False) to show it afte you move it; that way there's no chance of flicker or user seeing the window move.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Only X coordinate of Window centered

Post by RASHAD »

Hi Jan2004
Your code is fine
Next is another way to do it from beginning

Code: Select all

Define Result
ExamineDesktops()
x = DesktopWidth(0)/2 -600
y = 50

If OpenWindow(0, x,y, 1200, 700, "PureBasic Window", #PB_Window_MinimizeGadget)

  Repeat
    Event = WaitWindowEvent()

    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf

  Until Quit = 1
  
EndIf

End  
Egypt my love
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Re: Only X coordinate of Window centered

Post by Jan2004 »

Thanks to my colleagues for the tips. I follow RASHAD. I thought that with high resolution, the top of the window should be further away from the top of the window, so I changed the code (taking into account the suggestions of RASHAD). Here is the new code for most screen resolutions:

Code: Select all

Enumeration 
  #WINDOW_Main 
EndEnumeration 

ExamineDesktops()
x_coord = DesktopWidth(0)/2 -600
y_DesktopHeight = DesktopHeight(0)
                                                                           

If OpenWindow(#WINDOW_Main, 0,0, 1200, 700, "PureBasic Window", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  If y_DesktopHeight > 770 ; for desktop height 800 - 20 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 20, #PB_Ignore, #PB_Ignore) 
  EndIf
  
  If y_DesktopHeight > 810 ; for desktop height 900 and 864 - 50 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 50, #PB_Ignore, #PB_Ignore ) 
  EndIf
; For 768, 720, 600 window will be centered - as in If OpenWindow(#WINDOW_Main, 0,0, 1200, 700, "PureBasic Window", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  Repeat
    Event = WaitWindowEvent()

    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf

  Until Quit = 1
  
EndIf
End
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Only X coordinate of Window centered

Post by RASHAD »

Hi Jan2004
I think you should do the next change to avoid resizing the Window twice

Code: Select all

  If y_DesktopHeight > 770 And y_DesktopHeight < 800; for desktop height 800 - 20 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 20, #PB_Ignore, #PB_Ignore) 
  EndIf
  If y_DesktopHeight >= 800 ; for desktop height 900 and 864 - 50 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 50, #PB_Ignore, #PB_Ignore ) 
  EndIf
Egypt my love
Jan2004
Enthusiast
Enthusiast
Posts: 163
Joined: Fri Jan 07, 2005 7:17 pm

Re: Only X coordinate of Window centered

Post by Jan2004 »

RASHAD: Hi again!
Yes. It is the best solution. The final code is as follows:

Code: Select all

Enumeration 
  #WINDOW_Main 
EndEnumeration 

ExamineDesktops()
x_coord = DesktopWidth(0)/2 -600
y_DesktopHeight = DesktopHeight(0)                                                                        

  
If OpenWindow(#WINDOW_Main, 0,0, 1200, 700, "PureBasic Window", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  
  If y_DesktopHeight < = 770 ; For desktop height 768, 720, 600 - 0 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 0, #PB_Ignore, #PB_Ignore) 
  EndIf
  
  If y_DesktopHeight > 770 And y_DesktopHeight = < 800; for desktop height 800 - 20 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 20, #PB_Ignore, #PB_Ignore) 
  EndIf
  If y_DesktopHeight > 800 ; for desktop height 900 and 864 - 50 pixels from the top
    Result_win = WindowX(#WINDOW_Main ,#PB_Window_FrameCoordinate) 
    ResizeWindow(#WINDOW_Main, x_coord, 50, #PB_Ignore, #PB_Ignore ) 
  EndIf
  
  Repeat
    Event = WaitWindowEvent()

    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf

  Until Quit = 1
  
EndIf

End  
Post Reply