ディスプレイモニターに関する情報を取得          <TOP>


EnumDisplayDevices ディスプレイに関する情報を取得
EnumDisplaySettingsEx ディスプレイデバイスのいずれかのグラフィックスモードに関する情報を取得
MonitorFromPoint 指定された点を含むディスプレイモニタへのハンドルを取得
GetMonitorInfo ディスプレイモニターに関する情報を取得
 

 
'================================================================
'= ディスプレイモニターに関する情報を取得
'=    (GetMonitorInfo2.bas)
'================================================================
#include "Windows.bi"

#define DISPLAY_DEVICE_ATTACHED_TO_DESKTOP &H1
#define DISPLAY_DEVICE_DISCONNECT &H2000000
#define DISPLAY_DEVICE_MIRRORING_DRIVER &H8
#define DISPLAY_DEVICE_MODESPRUNED &H8000000
#define DISPLAY_DEVICE_MULTI_DRIVER &H2
#define DISPLAY_DEVICE_PRIMARY_DEVICE &H4
#define DISPLAY_DEVICE_REMOTE &H4000000
#define DISPLAY_DEVICE_REMOVABLE &H20
#define DISPLAY_DEVICE_VGA_COMPATIBLE &H10

#define DISPLAY_DEVICE_ACTIVE &H1
#define DISPLAY_DEVICE_ATTACHED &H2

#define CCHDEVICENAME 32                'デバイス名の長さを示す定数
#define CCHFORMNAME 32                  'フォーム名の長さを示す定数

#define ENUM_CURRENT_SETTINGS -1        'ディスプレイ解像度の現在の設定値を求める
#define ENUM_REGISTRY_SETTINGS -2       '指定した表示デバイスのレジストリに格納されている設定を取得

#define MONITOR_DEFAULTTONEAREST &H2    '指定したウィンドウに最も近い位置にあるディスプレイモニタのハンドルが返る
#define MONITOR_DEFAULTTONULL &H0       'NULLが返る
#define MONITOR_DEFAULTTOPRIMARY &H1    'プライマリディスプレイモニタのハンドルが返る

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

Type POINTAPI
    x As Long
    y As Long
End Type

Type DEVMODE
    dmDeviceName       As String * CCHDEVICENAME
    dmSpecVersion      As Integer
    dmDriverVersion    As Integer
    dmSize             As Integer
    dmDriverExtra      As Integer
    dmFields           As Long
    dmPosition         As POINTAPI
    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

Type RECT
    Left   As Long
    Top    As Long
    Right  As Long
    Bottom As Long
End Type

Type MONITORINFO
    cbSize    As Long
    rcMonitor As RECT
    rcWork    As RECT
    dwFlags   As Long
End Type

' ディスプレイに関する情報を取得
Declare Function Api_EnumDisplayDevices& Lib "user32" Alias "EnumDisplayDevicesA" (Unused As Any, ByVal iDevNum&, lpDisplayDevice As DISPLAY_DEVICE, ByVal dwFlags&)

' ディスプレイデバイスのいずれかのグラフィックスモードに関する情報を取得
Declare Function Api_EnumDisplaySettingsEx& Lib "user32" Alias "EnumDisplaySettingsExA" (ByVal lpszDeviceName$, ByVal iModeNum&, ByRef lpDevMode As DEVMODE, ByVal dwFlags&)

' 指定された点を含むディスプレイモニタへのハンドルを取得
Declare Function Api_MonitorFromPoint& Lib "user32" Alias "MonitorFromPoint" (ByVal x&, ByVal y&, ByVal dwFlags&)

' ディスプレイモニターに関する情報を取得
Declare Function Api_GetMonitorInfo& Lib "user32" Alias "GetMonitorInfoA" (ByVal hMonitor&, ByRef lpmi As MONITORINFO)

Var Shared List1 As Object

List1.Attach GetDlgItem("List1") : List1.SetFontSize 12
List1.SetWindowSize 240, 136

'================================================================
'=
'================================================================
Declare Function CStrToVBStr(str As String) As String
Function CStrToVBStr(str As String) As String
    Var char As String
    Var i As Long
    Var StrRet As String
    
    For i = 1 To Len(str)
        char = Mid$(str, i, 1)
        If char <> Chr$(0) Then
            StrRet = StrRet & char
        End If
    Next
    
    CStrToVBStr = StrRet
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var txt As String
    Var dm As DEVMODE
    Var ddMon As DISPLAY_DEVICE
    Var dd As DISPLAY_DEVICE
    Var mi As MONITORINFO
    Var dev As Long
    Var id As Long
    Var devMon As Long
    Var hm As Long
    Var buf As String
    Var Ret As Long

    txt = ""
    
    dd.cb = Len(dd)
    dev = 0
    id = 1

    List1.ResetContent
    
    Do While Api_EnumDisplayDevices(ByVal 0, dev, dd, 0) <> 0
        If Not CInt(dd.StateFlags And DISPLAY_DEVICE_MIRRORING_DRIVER) <> 0 Then
            ddMon.cb = Len(ddMon)
            devMon = 0

            Do While Api_EnumDisplayDevices(dd.DeviceName, devMon, ddMon, 0) <> 0
                If CInt(ddMon.StateFlags And DISPLAY_DEVICE_ACTIVE) <> 0 Then Exit Do
                devMon = devMon + 1
            Loop

            If CStrToVBStr(ddMon.DeviceString) = "" Then
                Ret = Api_EnumDisplayDevices(dd.DeviceName, 0, ddMon, 0)
                If CStrToVBStr(ddMon.DeviceString) = "" Then ddMon.DeviceString = "Default Monitor"
            End If
            
            dm.dmSize = Len(dm)
            If Api_EnumDisplaySettingsEx(dd.DeviceName, ENUM_CURRENT_SETTINGS, dm, 0) = 0 Then
                Ret = Api_EnumDisplaySettingsEx(dd.DeviceName, ENUM_REGISTRY_SETTINGS, dm, 0)
            End If

            mi.cbSize = Len(mi)
            If CInt(dd.StateFlags And DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) <> 0 Then
                hm = Api_MonitorFromPoint(dm.dmPosition.x, dm.dmPosition.y, MONITOR_DEFAULTTONULL)
                If hm <> 0 Then
                    Ret = Api_GetMonitorInfo(hm, mi)
                End If
            End If

            List1.AddString "Monitor No." & Str$(id)
            List1.AddString CStrToVBStr(ddMon.DeviceString)
            List1.AddString "(" & CStrToVBStr(dd.DeviceString) & ")"

            If Not CInt(dd.StateFlags And DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) <> 0 Then
                buf = "disabled, "
            Else If CInt(dd.StateFlags And DISPLAY_DEVICE_PRIMARY_DEVICE) <> 0 Then
                buf = "primary, "
            End If

            If CInt(dd.StateFlags And DISPLAY_DEVICE_REMOVABLE) <> 0 Then
                buf = buf & "removable, "
            End If

            If buf <> "" Then
                List1.AddString Left$(buf, Len(buf) - 2)
                buf = ""
            End If

            '幅 x 高さ @ x,y - bpp - リフレッシュレート
            List1.AddString "Width x Height:" & Str$(dm.dmPelsWidth) & " x " & Str$(dm.dmPelsHeight)
            List1.AddString "Position      :" & Str$(dm.dmPosition.x) & "," & Str$(dm.dmPosition.y)
            List1.AddString "Bits per Pixel:" & Str$(dm.dmBitsPerPel) & "bit"
            List1.AddString "Refresh Rate  :" & Str$(dm.dmDisplayFrequency) & "Hz"

            'ワークエリア・モニターハンドル
            If hm <> 0 Then
                List1.AddString "WorkSpace     :" & Str$(mi.rcWork.Left) & "," & Str$(mi.rcWork.Top) & " - " & Str$(mi.rcWork.Right) & "," & Str$(mi.rcWork.Bottom)
                List1.AddString "hMonitor      : &H" & Hex$(hm)
            End If

            'デバイス名
            If ddMon.DeviceName <> "" Then
                List1.AddString CStrToVBStr(ddMon.DeviceName)
            Else
                List1.AddString CStrToVBStr(dd.DeviceName)
            End If
            
            id = id + 1
        End If
        
        dev = dev + 1
    Loop
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End