To get the number of physical monitors

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

To get the number of physical monitors

Post by Grovkillen » Mon Jul 06, 2020 9:06 am

To get the number of physical monitors:

Code: Select all

LibFunc>user32,GetSystemMetrics,number,80
MDL>number
https://docs.microsoft.com/en-us/window ... temmetrics
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: To get the number of physical monitors

Post by Grovkillen » Fri Oct 01, 2021 8:00 am

I manage to get the number of screens and the "virtual screen" which is the rectangle which all displays are placed within. I now only need to know where the each display's viewing rectangle within this virtual screen.

Code: Select all

  LibFunc>user32,GetSystemMetrics,VIRTUAL_SCREEN_x,76
  LibFunc>user32,GetSystemMetrics,VIRTUAL_SCREEN_y,77
  LibFunc>user32,GetSystemMetrics,VIRTUAL_SCREEN_w,78
  LibFunc>user32,GetSystemMetrics,VIRTUAL_SCREEN_h,79
  LibFunc>user32,GetSystemMetrics,MONITOR_count,80
  MDL>%MONITOR_count%%CRLF%%VIRTUAL_SCREEN_x%%CRLF%%VIRTUAL_SCREEN_y%%CRLF%%VIRTUAL_SCREEN_w%%CRLF%%VIRTUAL_SCREEN_h%
This one seems to have it but I have yet to found out how.
https://docs.microsoft.com/en-us/window ... onitorinfo
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: To get the number of physical monitors

Post by Grovkillen » Fri Mar 17, 2023 6:39 am

This is how Windows is seeing the virtual screen.

https://learn.microsoft.com/en-us/windo ... ual-screen

But today we only know the max and min (purple arrows) related to the 0,0 point (green arrow) which is the top left corner of the primary display. In order to know the visible areas + which display a visible area is attached to I would need to know these points shown in the image below (red arrows):

Image

I have yet to stumble over an API call to retrieve this. If anyone knows please step into the light.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: To get the number of physical monitors

Post by Grovkillen » Thu Apr 06, 2023 4:38 am

God darn it! The problem was solved already!? viewtopic.php?f=2&t=10372&p=44256

:)

Code: Select all

RunProgram>cmd /c PowerShell -Command " & {Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens}" | clip
Gives me the output:

Code: Select all

BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1920,Height=1200}
DeviceName   : \\.\DISPLAY4
Primary      : True
WorkingArea  : {X=0,Y=0,Width=1920,Height=1160}

BitsPerPixel : 32
Bounds       : {X=-1920,Y=920,Width=1536,Height=960}
DeviceName   : \\.\DISPLAY1
Primary      : False
WorkingArea  : {X=-1920,Y=920,Width=1536,Height=920}

BitsPerPixel : 32
Bounds       : {X=1920,Y=0,Width=1920,Height=1200}
DeviceName   : \\.\DISPLAY5
Primary      : False
WorkingArea  : {X=1920,Y=0,Width=1920,Height=1160}
Which is exactly what I want. I will incorporate this into my script at some time. Thanks @jbas!
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: To get the number of physical monitors

Post by Grovkillen » Wed Dec 13, 2023 11:47 am

A touch up on the code:

Code: Select all

  Let>RP_CAPTURESTDOUT=1
  Let>POWERSHELL_COMMAND=Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens | ConvertTo-Json
  RunProgram>cmd /c PowerShell -Command " & {%POWERSHELL_COMMAND%}"
  JSONParse>RP_STDOUT,$[*].DeviceName,TEMP_device_name
  JSONParse>RP_STDOUT,$[*].Primary,TEMP_is_primary
  JSONParse>RP_STDOUT,$[*].Bounds.X,TEMP_x_position
  JSONParse>RP_STDOUT,$[*].Bounds.Y,TEMP_y_position
  JSONParse>RP_STDOUT,$[*].Bounds.Width,TEMP_x_resolution
  JSONParse>RP_STDOUT,$[*].Bounds.Height,TEMP_y_resolution
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts