2

为了找到显示器的尺寸,我一直在使用:

Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal Index As Long) As Long

进而:

Function getMonitorSize()
  monitorHeight = GetSystemMetrics32(1)
  monitorWidth = GetSystemMetrics32(0)
End Function

这适用于主显示器,但我如何找到外接显示器的尺寸?

4

1 回答 1

5

EnumDisplayDevices告诉您有关具有特定索引的监视器的信息。您可以将索引从 0 增加到任何值,直到函数返回 0,这意味着没有具有该索引的监视器。

然后你打电话EnumDisplaySettings来计算尺寸。

Private Const ENUM_CURRENT_SETTINGS As Long = -1
Private Const DISPLAY_DEVICE_ATTACHED_TO_DESKTOP As Long = &H1
Private Const CCHDEVICENAME As Long = 32
Private Const CCHFORMNAME As Long = 32

Private Type DISPLAY_DEVICE
  cb As Long
  DeviceName As String * CCHDEVICENAME
  DeviceString As String * 128
  StateFlags As Long
  DeviceID As String * 128
  DeviceKey As String * 128
End Type

Private Type DEVMODE
  dmDeviceName As String * CCHDEVICENAME
  dmSpecVersion As Integer
  dmDriverVersion As Integer
  dmSize As Integer
  dmDriverExtra As Integer
  dmFields As Long
  dmOrientation As Integer
  dmPaperSize As Integer
  dmPaperLength As Integer
  dmPaperWidth As Integer
  dmScale As Integer
  dmCopies As Integer
  dmDefaultSource As Integer
  dmPrintQuality As Integer
  dmColor As Integer
  dmDuplex As Integer
  dmYResolution As Integer
  dmTTOption As Integer
  dmCollate As Integer
  dmFormName As String * CCHFORMNAME
  dmLogPixels As Integer
  dmBitsPerPel As Long
  dmPelsWidth As Long
  dmPelsHeight As Long
  dmDisplayFlags As Long
  dmDisplayFrequency As Long
End Type

Private Declare Function EnumDisplayDevices Lib "user32.dll" Alias "EnumDisplayDevicesA" (ByVal lpDevice As String, ByVal iDevNum As Long, ByRef lpDisplayDevice As DISPLAY_DEVICE, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Long, ByRef lpDevMode As DEVMODE) As Long

Dim indAdapter As Long, indDisplay As Long
Dim ddAdapters As DISPLAY_DEVICE, ddDisplays As DISPLAY_DEVICE
ddAdapters.cb = Len(ddAdapters):  ddDisplays.cb = Len(ddDisplays)

indAdapter = 0
Do Until EnumDisplayDevices(vbNullString, indAdapter, ddAdapters, 0) = 0

  If (ddAdapters.StateFlags And DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) = DISPLAY_DEVICE_ATTACHED_TO_DESKTOP Then

      Dim NullCharPos As Long
      NullCharPos = InStr(ddAdapters.DeviceName, vbNullChar)

      Dim CurDeviceName As String

      If NullCharPos > 0 Then
        CurDeviceName = Left$(ddAdapters.DeviceName, NullCharPos - 1)
      Else
        CurDeviceName = ddAdapters.DeviceName
      End If

      Dim dmode As DEVMODE
      dmode.dmSize = Len(dmode)

      EnumDisplaySettings CurDeviceName, ENUM_CURRENT_SETTINGS, dmode

      MsgBox "Width: " & dmode.dmPelsWidth
      MsgBox "Height: " & dmode.dmPelsHeight

  End If

  indAdapter = indAdapter + 1
Loop
于 2011-10-16T17:18:20.473 回答