5

In my project I need to determine the size of the monitor/screen. I can get the screen resolution using following code

   X = fPixelsToTwips(GetSystemMetrics(SM_CXSCREEN), "X") '
   Y = GetSystemMetrics(SM_CYSCREEN)

This gives me the correct screen resolution. But I have a 13.6" laptop screen and my friend has 15.6" laptop screen. Both has same screen resolution 1366*768. But the screen size is different. So how can I determine screen size of monitor? This is very important for my project.

  • If all else fails, you could ask the user for the screen size. I know it's not ideal. zedfoxus might be onto something though. Good luck. – MatthewD Sep 16 '15 at 20:28

1 Answers1

6

You can tap into WMI's WmiMonitorBasicDisplayParams to get some information about your display. I was successfully able to display the diagonal length of both displays with this code using Windows 7.

Option Explicit

Sub Test()

    Dim WMIObject As Object
    Dim WMIResult As Object
    Dim WMIItem As Object

    Set WMIObject = GetObject("winmgmts:\\.\root\WMI")
    Set WMIResult = WMIObject.ExecQuery("Select * From WmiMonitorBasicDisplayParams")

    Dim Diagonal As Double
    Dim Width As Double
    Dim Height As Double
    Dim Counter As Integer
    Counter = 1

    For Each WMIItem In WMIResult

        Width = WMIItem.MaxHorizontalImageSize / 2.54
        Height = WMIItem.MaxVerticalImageSize / 2.54
        Diagonal = Sqr((Height ^ 2) + (Width ^ 2))

        MsgBox "Your monitor # " & Counter & " is approximiately " & Round(Diagonal, 2) & " inches diagonal"
        Counter = Counter + 1

    Next

End Sub

Some other references that may help you.

Community
  • 1
  • 1
zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • @MatthewD it is indeed quite interesting to see all the kinds of information WMI exposes. I didn't know this was possible until trying it myself after some Googling – zedfoxus Sep 16 '15 at 20:28
  • 1
    I went down a different path from my Googling. I will put this info in my "bag of tricks". Thanks. – MatthewD Sep 16 '15 at 20:29
  • 1
    This is amazing. Thanks a lot. –  Sep 16 '15 at 20:53
  • 1
    I have two monitor, is there any way to check on which monitor my project is currently running? –  Sep 16 '15 at 20:54
  • @user1670773 I am sure there is a way to do that, but I don't know the answer. You might benefit from creating a new question so that someone more knowledgeable than me can help out – zedfoxus Sep 16 '15 at 21:03
  • Note that, as noted in the Wmi documentation, the actual values may not be available. – Adrian McCarthy Feb 02 '17 at 22:20