info on your multi monitor setup

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

info on your multi monitor setup

Post by blueznl »

just a little hack for those dealing with multiple monitors

Code: Select all


#SM_XVIRTUALSCREEN        = 76        ; x-coordinate of left upper corner of total display area
#SM_YVIRTUALSCREEN        = 77        ; same for y
#SM_CXVIRTUALSCREEN       = 78        ; width
#SM_CYVIRTUALSCREEN       = 79        ; height
#SM_CMONITORS             = 80        ; number of monitors
#SM_SAMEDISPLAYFORMAT     = 81        ; identical sizing of all screens

#MONITOR_DEFAULTTONULL    = 0
#MONITOR_DEFAULTTOPRIMARY = 1
#MONITOR_DEFAULTTONEAREST = 2
                
Debug "nr of screens:"
Debug GetSystemMetrics_(#SM_CMONITORS)
Debug "same format or not:"
Debug GetSystemMetrics_(#SM_SAMEDISPLAYFORMAT)
Debug "desktop size"
Debug GetSystemMetrics_(#SM_CXVIRTUALSCREEN)
Debug GetSystemMetrics_(#SM_CYVIRTUALSCREEN)
NewList monitor_hnd.l()

Procedure cb_test(WindowID, Message, WPARAM, LPARAM) 
  Result = #PB_ProcessPureBasicEvents 
  ; 
  AddElement(monitor_hnd())
  monitor_hnd() = WindowID
  ; 
  ProcedureReturn Result 
EndProcedure 

Debug "enum"
Debug EnumDisplayMonitors_(0,0,@cb_test(),0)

Debug "handle of primary monitor:"
primary_hnd.l = MonitorFromPoint_(0,0,#MONITOR_DEFAULTTOPRIMARY)
Debug primary_hnd

Structure MONITORINFO
  Size.l
  m_x1.l
  m_y1.l
  m_x2.l
  m_y2.l
  w_x1.l
  w_y1.l
  w_x2.l
  w_y2.l
  flags.l
EndStructure

info.MONITORINFO

info\Size = SizeOf(MONITORINFO)
info\flags = 0

n.l = 0
ResetList(monitor_hnd())
While NextElement(monitor_hnd())
  INC n
  Debug "monitor, handle, origin and size:"
  Debug n
  Debug monitor_hnd()
  GetMonitorInfo_(monitor_hnd(),@info)
  Debug info\m_x1
  Debug info\m_y1
  Debug info\m_x2 - info\m_x1
  Debug info\m_y2 - info\m_y1
Wend  
   
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: info on your multi monitor setup

Post by IdeasVacuum »

A re-vamp to work with PB4.61:

Code: Select all

#SM_XVIRTUALSCREEN        = 76        ; x-coordinate of left upper corner of total display area
#SM_YVIRTUALSCREEN        = 77        ; same for y
#SM_CXVIRTUALSCREEN       = 78        ; width
#SM_CYVIRTUALSCREEN       = 79        ; height
#SM_CMONITORS             = 80        ; number of monitors
#SM_SAMEDISPLAYFORMAT     = 81        ; identical sizing of all screens

#MONITOR_DEFAULTTONULL    = 0
#MONITOR_DEFAULTTOPRIMARY = 1
#MONITOR_DEFAULTTONEAREST = 2

;MONITORINFOEX with added string for device name has MS bug
;https://connect.microsoft.com/VisualStudio/feedback/details
;/545217/windows-7-getmonitorinfo-returns-wrong-information

Structure MONITORINFO 
cbSize.i 
rcMonitor.RECT
rcWorkArea.RECT
flags.i
EndStructure

info.MONITORINFO

  info\cbSize = SizeOf(MONITORINFO)
   info\flags = 0
iMonitorNum.i = 0

NewList monitor_hnd.i()

Procedure cb_test(WinID, Message, WPARAM, LPARAM)
;------------------------------------------------
Shared monitor_hnd()

Protected iResult.i = #PB_ProcessPureBasicEvents
Protected iScreensSame.i = 0
Protected iPrimary_hnd.i
Protected ptMonitor.POINT

     ptMonitor\x = 0
     ptMonitor\y = 0
     
      AddElement(monitor_hnd())
     monitor_hnd() = WinID
     
     ProcedureReturn iResult

EndProcedure

EnumDisplayMonitors_(0,0,@cb_test(),0)

Debug "Number of screens: " + Str(GetSystemMetrics_(#SM_CMONITORS))
 
       iScreensSame = GetSystemMetrics_(#SM_SAMEDISPLAYFORMAT)
Select iScreensSame

           Case #True : Debug "Screens are same format"
          Case #False : Debug "Screens are not same format"
EndSelect

Debug "Virtual Desktop Size Width: " + Str(GetSystemMetrics_(#SM_CXVIRTUALSCREEN)) + " Height: " + Str(GetSystemMetrics_(#SM_CYVIRTUALSCREEN))

;Bug: MonitorFromPoint can deliver the wrong Monitor
;when using the #MONITOR_DEFAULTTONEAREST flag

iPrimary_hnd = MonitorFromPoint_(ptMonitor,#MONITOR_DEFAULTTOPRIMARY)

Debug "Handle of Primary Monitor: " + Str(iPrimary_hnd)
Debug "===================================================="

FirstElement(monitor_hnd())

ForEach monitor_hnd()

         iMonitorNum + 1
         GetMonitorInfo_(monitor_hnd(),@info)
         Debug ""
         Debug "Monitor: " + Str(iMonitorNum)
         Debug "Handle: " + Str(monitor_hnd())
         Debug "Origin: x" + Str(info\rcMonitor\left) + " y" + Str(info\rcMonitor\top)
         Debug "Screen Width: " + Str(info\rcMonitor\right - info\rcMonitor\left)
         Debug "Screen Height: " + Str(info\rcMonitor\bottom - info\rcMonitor\top)
         Debug "Work Area Width: " + Str(info\rcWorkArea\right - info\rcWorkArea\left)
         Debug "Work Area Height: " + Str(info\rcWorkArea\bottom - info\rcWorkArea\top)
         Debug "Task Bar Height: " + Str((info\rcMonitor\bottom - info\rcMonitor\top) - (info\rcWorkArea\bottom - info\rcWorkArea\top))
         Debug "===================================================="

Next 
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
happer66
User
User
Posts: 33
Joined: Tue Jan 12, 2010 12:10 pm
Location: Sweden

Re: info on your multi monitor setup

Post by happer66 »

iirc, structures with DWORD doesn't change size depending on the architecture of the OS. So you'll have to change the integers into longs for it to work correctly for both 32-bit and 64-bit.

Code: Select all

#SM_XVIRTUALSCREEN        = 76        ; x-coordinate of left upper corner of total display area
#SM_YVIRTUALSCREEN        = 77        ; same for y
#SM_CXVIRTUALSCREEN       = 78        ; width
#SM_CYVIRTUALSCREEN       = 79        ; height
#SM_CMONITORS             = 80        ; number of monitors
#SM_SAMEDISPLAYFORMAT     = 81        ; identical sizing of all screens

#MONITOR_DEFAULTTONULL    = 0
#MONITOR_DEFAULTTOPRIMARY = 1
#MONITOR_DEFAULTTONEAREST = 2

;MONITORINFOEX with added string for device name has MS bug
;https://connect.microsoft.com/VisualStudio/feedback/details
;/545217/windows-7-getmonitorinfo-returns-wrong-information

Structure MONITORINFO
  cbSize.l
  rcMonitor.RECT
  rcWorkArea.RECT
  flags.l
EndStructure

info.MONITORINFO

info\cbSize = SizeOf(MONITORINFO)
info\flags = 0
iMonitorNum = 0

NewList monitor_hnd.i()

Procedure cb_test(WinID, Message, WPARAM, LPARAM)
  ;------------------------------------------------
  Shared monitor_hnd()
  
  Protected iResult = #PB_ProcessPureBasicEvents
  Protected iScreensSame
  Protected iPrimary_hnd
  Protected ptMonitor.POINT
  
  ptMonitor\x = 0
  ptMonitor\y = 0
  
  AddElement(monitor_hnd())
  monitor_hnd() = WinID
  
  ProcedureReturn iResult
  
EndProcedure

EnumDisplayMonitors_(0,0,@cb_test(),0)

Debug "Number of screens: " + Str(GetSystemMetrics_(#SM_CMONITORS))

iScreensSame = GetSystemMetrics_(#SM_SAMEDISPLAYFORMAT)
Select iScreensSame
    
  Case #True : Debug "Screens are same format"
  Case #False : Debug "Screens are not same format"
EndSelect

Debug "Virtual Desktop Size Width: " + Str(GetSystemMetrics_(#SM_CXVIRTUALSCREEN)) + " Height: " + Str(GetSystemMetrics_(#SM_CYVIRTUALSCREEN))

;Bug: MonitorFromPoint can deliver the wrong Monitor
;when using the #MONITOR_DEFAULTTONEAREST flag

iPrimary_hnd = MonitorFromPoint_(ptMonitor,#MONITOR_DEFAULTTOPRIMARY)

Debug "Handle of Primary Monitor: " + Str(iPrimary_hnd)
Debug "===================================================="

FirstElement(monitor_hnd())

ForEach monitor_hnd()
  
  iMonitorNum + 1
  GetMonitorInfo_(monitor_hnd(),@info)
  Debug ""
  Debug "Monitor: " + Str(iMonitorNum)
  Debug "Handle: " + Str(monitor_hnd())
  Debug "Origin: x" + Str(info\rcMonitor\left) + " y" + Str(info\rcMonitor\top)
  Debug "Screen Width: " + Str(info\rcMonitor\right - info\rcMonitor\left)
  Debug "Screen Height: " + Str(info\rcMonitor\bottom - info\rcMonitor\top)
  Debug "Work Area Width: " + Str(info\rcWorkArea\right - info\rcWorkArea\left)
  Debug "Work Area Height: " + Str(info\rcWorkArea\bottom - info\rcWorkArea\top)
  Debug "Task Bar Height: " + Str((info\rcMonitor\bottom - info\rcMonitor\top) - (info\rcWorkArea\bottom - info\rcWorkArea\top))
  Debug "===================================================="
  
Next
Image If your code isn't clean atleast make sure it's pure!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: info on your multi monitor setup

Post by IdeasVacuum »

Hi happer66
...have not experienced any problems on either 32bit or 64bit. Did you hit an issue that prompted you to post?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
happer66
User
User
Posts: 33
Joined: Tue Jan 12, 2010 12:10 pm
Location: Sweden

Re: info on your multi monitor setup

Post by happer66 »

Hi IdeasVacuum, when using the 64-bit compiler:
Number of screens: 2
Screens are same format
Virtual Desktop Size Width: 4480 Height: 1440
Handle of Primary Monitor: 2164749
====================================================

Monitor: 1
Handle: 2164749
Origin: x0 y0
Screen Width: 0
Screen Height: 0
Work Area Width: 0
Work Area Height: 0
Task Bar Height: 0
====================================================

Monitor: 2
Handle: 41552431
Origin: x0 y0
Screen Width: 0
Screen Height: 0
Work Area Width: 0
Work Area Height: 0
Task Bar Height: 0
====================================================
Same code but just modified the MONITORINFO struct:
Number of screens: 2
Screens are same format
Virtual Desktop Size Width: 4480 Height: 1440
Handle of Primary Monitor: 2164749
====================================================

Monitor: 1
Handle: 2164749
Origin: x0 y0
Screen Width: 2560
Screen Height: 1440
Work Area Width: 2560
Work Area Height: 1358
Task Bar Height: 82
====================================================

Monitor: 2
Handle: 41552431
Origin: x2560 y0
Screen Width: 1920
Screen Height: 1080
Work Area Width: 1920
Work Area Height: 1080
Task Bar Height: 0
====================================================
If you're using the 32-bit compiler it will still run correctly on both x86/x64. I might have been a bit unclear, sorry.
Image If your code isn't clean atleast make sure it's pure!
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: info on your multi monitor setup

Post by IdeasVacuum »

Ah, sorry, should have realized that's what you meant :shock:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply