Solved

VBScript - Get resolution of Primary display only

Posted on 2012-03-19
14
2,470 Views
Last Modified: 2012-03-22
Experts,

I have the following code that sucessfully grabs monitor resolutions...  I can't find anywhere on the interwebs that shows how to filter this WMI query to only the primary display.


        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")

        For Each objItem in colItems
            *Do Crap*
        Next

Open in new window


Would anybody happen to know of any references that could assist here?  Thanks.
0
Comment
Question by:usslindstrom
  • 8
  • 6
14 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 37740623
Hi, what about this:
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor WHERE DeviceID='DesktopMonitor1'")

        For Each objItem in colItems
            WScript.Echo objItem.ScreenHeight & vbCrLf & objItem.ScreenWidth
        Next

Open in new window


That should work on a desktop system....not sure if it works for laptops.....

Rob.
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37740727
Thank you for the idea Rob...  But I'm not sure if that's going to work for desktops either...

In testing, I have 3 displays on a machine, Monitor #3 (in this case) is the primary display, if I define "DesktopMonitor1" in that WHERE clause, I'd imagine it would grab only the left most monitor.  Is that the correct assumption?
TrippleMon.png
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37740753
I'm not sure, but I wouldn't think so.  When you view the graphics properties of a multi-display system, you can move the configuration around such that monitor two is on the left, so in that instance, hopefully DesktopMonitor1 doesn't change physical monitors.

Rob.
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37740837
Understood.

I borrowed your WHERE clause and put it into the WMI.  Unfortunately, it is returning the value of the left most monitor (who's resolution is 1280 x 1024).  The monitor I'm hoping to get ahold of is actually 1920x1080.

        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor WHERE DeviceID='DesktopMonitor1'")
        For Each objItem in colItems
            intHorizontal = objItem.ScreenWidth
            intVertical = objItem.ScreenHeight
            msgbox intHorizontal & " " & intVertical
        Next

Open in new window

Resolution.png
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37740841
Strange thing is, I just ran the script with the WHERE clasue being "DesktopMonitor3" - and the msgbox doesn't even show up.

I'm guessing that some properties of monitors aren't queriable via WMI?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37741335
OK, so just to see what's happening on all three, run this and see what you get.

Regards,

Rob.

        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")

        For Each objItem in colItems
            WScript.Echo "DeviceID: " & objItem.DeviceID & VbCrLf & "ScreenHeight: " & objItem.ScreenHeight & vbCrLf & "ScreenWidth: " & objItem.ScreenWidth
        Next

Open in new window

0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37745908
I ran the WScript.Echo, and it's leading me to believe that's what I'm trying to do isn't entirely possible.

The script returns echo values for DesktopMonitor1 at 1280x1024, and DesktopMonitor2 at the same resolution.

Not a peep for the 3rd monitor at all.  This really may not be possible, as the physical layout of this (at least on this test unit) is that Monitor's 1 & 2 are off the same video card, while the 3rd display is running off the onboard controller.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 65

Expert Comment

by:RobSampson
ID: 37750197
Wow, that's an odd one.  I'll have a look around at some other classes (or maybe even the registry) at some point tomorrow. I won't have a chance today.

Rob.
0
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 37750829
I've been looking at the different classes too, and I don't think what I'm trying to do is going to be native for VBScript.  Every thing I tried seems to only get the monitors that are plugged into the video card, and nothing from the onboard.

BUUUT - Lucky for me, the VB is actually in use within a HTA script.  Where VB aparently isn't able to show me what's needed, HTML actually looks like it'll work perfect, and I tested it accross a few different units with multiple monitors.

So far so good.

The HTML code is actully just:

screen.width
screen.height


I converted those to variables, and the script works like I was hoping.

Much appreciated on the assistance.  You got me thinking about the problem and how to overcome it.  - Thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37751225
Interesting solution!  Thanks for the grade anyhow.  One thought though, if you have that in a HTA, does it scale depending on which screen the HTA is actually being viewed on?  So if you put
MsgBox screen.height & " x " & screen.width

in the code for a button, and move the HTA around from screen to screen, and click the button, does it reflect the different resolutions?  If so, then great!  That's a good workaround!

Regards,

Rob.
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37751668
Yes sir on all counts.  :)

The HTA dynamically sets the window size as well.

        'Retreive Screen Resolution
            MaxWidth = Screen.Width
            MaxHeight = Screen.Height

            BannerWidth = "560"

            Set objItem = Document.GetElementByID("BackDrop")
            BannerHeight = objItem.OffsetHeight

            BannerPos BannerWidth, BannerHeight, MaxWidth, MaxHeight

    Sub BannerPos(BannerWidth, BannerHeight, MaxWidth, MaxHeight)
        FooterHeight = "55"
        OffsetShift = "3"

        BannerWidth = Int(BannerWidth)
        BannerHeight = Int(BannerHeight) + (FooterHeight * 2)
        MaxWidth = Int(MaxWidth)
        MaxHeight = Int(MaxHeight)

        If BannerHeight > MaxHeight Then
            BannerHeight = MaxHeight - (FooterHeight * 2)
            intTop = FooterHeight
        Else
            intTop = (MaxHeight - BannerHeight) / 2 + OffsetShift
        End If
        intLeft = (MaxWidth - BannerWidth) / 2
        window.moveTo intLeft, intTop
        window.resizeTo BannerWidth,BannerHeight
      End Sub

Open in new window

0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37751681
Side note - I'm forcing it to be center on screen though.  (This is for a logon banner).
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 37752328
Oh - I finally got what you were referring to.

Now THAT would be amazing!  - Where you drag the little HTA around between screens and it sized accordingly.

No.  My little script ins't anywhere that complex at all.  It's just a dynamic HTA that I feed the elements from a command line in quoted strings.  Based on how much stuff gets plastered in the div elements - the window sizes accordingly and centers itself on the main screen of the user.

So, to put it in perspective, we have a VB logon script that runs and assigns drive letters / home pages based on group memberships, etc.  The script used to just spit out a msgbox for the user, displaying all of the important bits.  - Msgbox's are BORING - so I thought to draft up a solution that could take what VBScript output over to an HTML environment so we could put our logo on the thing, etc.

It took a bit, a lot of hits here at EE, "The Scripting Guys" from Microsoft, etc to get a working version, but now it's all absolutely PERFECT.  :)

Of course, nothing as intense as you just suggested, but at least for our environment it'll work great.  :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37754854
Sure, sounds good.  Moving it around and having it dynamically size shouldn't be a problem anyway, you wouldn't expect too many people to have it change screens.

I've used HTAs to provide logon information as well, although, perhaps when you have time, you could wrap all of your logon script processing inside the HTA itself (adding it to the Window_OnLoad procedure), so that you can display the information in a scrolling text area as it happens.  This would keep things central, and also avoid the need for command line passing to the HTA from an external VBS.

Anyway, as noted before, thanks for the grade, even though I didn't offer you a direct solution.

Regards,

Rob.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now