HOME > 即効テクニック > AccessVBA > Windows環境・オブジェクト > ディスプレイの解像度を取得する(API)

即効テクニック

Windows環境・オブジェクト

ディスプレイの解像度を取得する(API)

(Access 97)
●概要●
MDBを起動しているコンピュータの、ディスプレイ解像度を取得します。

●サンプルコード●
標準モジュールに以下のコードを記述して下さい。

Declare Function GetDesktopWindow Lib "user32" () As Long

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

Type WindowSize
    Height As Long
    Width As Long
End Type

Declare Function GetWindowRect Lib "user32" ( ByVal hwnd As Long, _
                                              lpRect As RECT _
                                            ) As Long

    'ディスプレイの解像度を取得する
    Public Function GetWindowSize() As WindowSize
    Dim Re As RECT
    Dim hwnd As Long
    Dim RetVal As Long

    hwnd = GetDesktopWindow()
    RetVal = GetWindowRect(hwnd, Re)

    GetWindowSize.Height = Re.Bottom - Re.Top
    GetWindowSize.Width = Re.Right - Re.Left
End Function

●動作確認●
標準モジュールに以下のコードを記述し、実行して下さい。

Private Sub test()
    Debug.Print GetWindowSize.Width     '横のドット数
    Debug.Print GetWindowSize.Height    '縦のドット数
End Sub

このテストでは、イミディエイトウィンドウにディスプレイの解像度が表示されます。
ディスプレイの解像度によって、表示するフォームの種類や大きさを変えたい時などに応用できます。