Screen size

Just starting out? Need help? Post your questions and find answers here.
k3pto
User
User
Posts: 87
Joined: Sat Jan 17, 2015 5:24 pm

Screen size

Post by k3pto »

I have a two part question:
1) how can I determine the size of my screen
2) how can I prevent my window from being dragged off the screen
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Screen size

Post by kpeters58 »

1) Check the 'Desktop' chapter in the PB Help.
2) Windows only or platform independent?
PB 5.73 on Windows 10 & OS X High Sierra
k3pto
User
User
Posts: 87
Joined: Sat Jan 17, 2015 5:24 pm

Re: Screen size

Post by k3pto »

Hello kpeters58
Thank you, I have been through a lot of the manual but obviously not this chapter. :?
Right now it is Windows only.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Screen size

Post by RASHAD »

Hi

Code: Select all

Global DesktopWidth,  DesktopHeight ,ww,hh

ExamineDesktops()
DesktopWidth  = DesktopWidth(0)
DesktopHeight = DesktopHeight(0)

Procedure winCB(hWnd, uMsg, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
  Select uMsg
    Case #WM_SIZING
      ww = WindowWidth(0)
      hh = WindowHeight(0)
      
    Case #WM_MOVING      
      *winRect.Rect = lparam      
      If *winRect\left <= 0 
        *winRect\left = 0 
        *winRect\right = *winRect\left + ww       
      EndIf 
      If *winRect\Top < 0  
        *winRect\Top = 0 
        *winRect\bottom = *winRect\Top + hh                
      EndIf 
      If *winRect\right >= DesktopWidth  
        *winRect\left = DesktopWidth -  ww
        *winRect\right = *winRect\left + ww
      EndIf 
      If *winRect\bottom >= DesktopHeight  
        *winRect\Top = DesktopHeight  -  hh 
        *winRect\bottom = *winRect\top + hh 
      EndIf 
      
  EndSelect
  
  ProcedureReturn result 
EndProcedure 


OpenWindow(0, 0,0,400,300, "Restrict moving", #PB_Window_SystemMenu | #PB_Window_SizeGadget|#PB_Window_ScreenCentered) 
ww = 400
hh = 300 

SetWindowCallback(@winCB()) 

Repeat  
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect 
  
Until Quit = 1

End 

It could be cross platform

Code: Select all

Global DesktopWidth,  DesktopHeight ,ww,hh

ExamineDesktops()
DesktopWidth  = DesktopWidth(0)
DesktopHeight = DesktopHeight(0)

Procedure moveCB()
  xl = -8
  yt = 0
  xr = DesktopWidth - WindowWidth(0) - 8
  yb = DesktopHeight - WindowHeight(0)  
  If WindowX(0) < 0
    ResizeWindow(0,xl,#PB_Ignore,#PB_Ignore,#PB_Ignore)
  EndIf
  If WindowY(0) < 1
    ResizeWindow(0,#PB_Ignore,yt,#PB_Ignore,#PB_Ignore)
  EndIf
  If (WindowX(0) + WindowWidth(0)) > DesktopWidth-1
    ResizeWindow(0,xr,#PB_Ignore,#PB_Ignore,#PB_Ignore)
  EndIf
    If (WindowY(0) + WindowHeight(0)) > DesktopHeight-1
    ResizeWindow(0,#PB_Ignore,yb,#PB_Ignore,#PB_Ignore)
  EndIf
EndProcedure 


OpenWindow(0, 0,0,400,300, "Restrict moving", #PB_Window_SystemMenu | #PB_Window_SizeGadget|#PB_Window_ScreenCentered) 
ww = 400
hh = 300 

BindEvent(#PB_Event_MoveWindow,@moveCB())

Repeat  
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect 
  
Until Quit = 1

End 
Egypt my love
Post Reply