viewpoint-particle

Author Topic: Determine number of displays currently connected  (Read 39 times)

gibberish

  • Newbie
  • *
  • Posts: 15
Determine number of displays currently connected
« on: November 28, 2018, 10:45:48 am »
From C# code, I have an object of the screen class:

Code: [Select]
objScreen = ObjectClrNew(`System.Windows.Forms.Screen`)(Thanks Tony for assist with above)

How would I return the number of displays currently connected? I tried this:

Code: [Select]
arrDisplays = objScreen.AllScreens
hoping that a useable array would be created, but that didn't work.

How would I work with the below C# code (or ANY code, I am not attached to below code) to determine number of connected screens?

Getting some info about each connected display (eg. resolution) would also be a huge help.

Code: [Select]
private void button1_Click(object sender, System.EventArgs e)
{
    // For each screen, add the screen properties to a list box.
    foreach (var screen in System.Windows.Forms.Screen.AllScreens)
    {
        listBox1.Items.Add("Device Name: " + screen.DeviceName);
        listBox1.Items.Add("Bounds: " +
            screen.Bounds.ToString());
        listBox1.Items.Add("Type: " +
            screen.GetType().ToString());
        listBox1.Items.Add("Working Area: " +
            screen.WorkingArea.ToString());
        listBox1.Items.Add("Primary Screen: " +
            screen.Primary.ToString());
    }

}

Source:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.screen?view=netframework-4.7.2

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: Determine number of displays currently connected
« Reply #1 on: November 28, 2018, 11:23:19 am »
Not sure what you mean by "that didn't work" as that is a bit too generic to make sense of but if you use WinBatch Foreach syntax, iterating the screen objects works just fine. 

Here is an updated example loosely based on an example originally created by Stan:
Code: Winbatch
ObjectClrOption("useany","System.Windows.Forms")
Screen = ObjectClrNew('System.Windows.Forms.Screen')

PrimScreen = Screen.PrimaryScreen

;; Don't need to use ObjectClrType because
;; class wrapped for you by WinBatch
Message("Device Name", PrimScreen.DeviceName)

Screens = Screen.AllScreens
                                                             
foreach Item in Screens
        ;; Don't need to use ObjectClrType because
        ;; the CLR class allows WinBatch query its
        ;; type information.
        Message("Device Name", Item.DeviceName)

        ;; However, it is more efficient to do so.
        ScreenItem = ObjectClrType("System.Windows.Forms.Screen", Item)
        Message("Device", "Name: ":ScreenItem.DeviceName:@CRLF:"Bytes per pixel: ":ScreenItem.BitsPerPixel)
next
exit
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


gibberish

  • Newbie
  • *
  • Posts: 15
Re: Determine number of displays currently connected
« Reply #2 on: November 28, 2018, 02:49:32 pm »
Thanks very much Tony