问题
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.
回答1:
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.
- VBScript and WMI to capture display information
- PowerShell and WMI to capture display information
- C# to capture display information
- System Information using VBA and WMI
来源:https://stackoverflow.com/questions/32617615/how-to-get-monitor-size