Determine number of displays currently connected

Started by gibberish, November 28, 2018, 10:45:48 AM

Previous topic - Next topic

gibberish

From C# code, I have an object of the screen class:

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:

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.


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

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) Select
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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

gibberish

Thanks very much, Tony.

I will be using this code in a subroutine that gets called repeated from the main loop (watching for when the number connected displays changes from 1 to 2, and back again) - do I need to drop anything at the end of each call to the subroutine?

I am guessing that I should move the line [ ObjectClrOption("useany","System.Windows.Forms") ] to the top of the script, before the main loop, and drop [ Screen ], [ Screens ], [ Item ] and [ ScreenItem ] at the bottom of the sub, before the Return statement. Is that correct, or is that not necessary?

td

If you move everything into a user-defined function ( not a user-defined subroutine), you would not need to do anything much of anything as WinBatch will clean everything up for you.  Alternatively, you could move the variable created in the call to ObjectClrNew outside the UDF and pass the variable containing the result to the UDF and you would only need to release that variable when done with it.  If you are using a UDS instead of a UDF, you could assign 0 to the variables once you were done with them.  You would also need to clear any UDS parameter variables containing objects after the last call to the UDS.

The objects in the script do not appear to have a Dispose method so you don't need to worry any about calling that.

That said, for most scripts, you can get by without doing any of the above because the resources consumed are not that significant relative to what is available.  This sort of issue usually only comes into play when you have a complex script that stays alive for a long time or gets called frequently in a short period of time.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

gibberish

Thanks very much for the detailed answer - every point was useful to me.