Option Explicit #If VBA7 Then Private Declare PtrSafe Function CreateDCW Lib "gdi32" (ByVal pwszDriver As LongPtr, ByVal pwszDevice As LongPtr, ByVal pszPort As LongPtr, ByVal pdm As LongPtr) As LongPtr Private Declare PtrSafe Function DeleteDC Lib "gdi32" (ByVal hDC As LongPtr) As Long Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hDC As LongPtr, ByVal nXPos As Long, ByVal nYPos As Long) As OLE_COLOR #Else Private Declare Function CreateDCW Lib "gdi32" (ByVal pwszDriver As OLE_HANDLE, ByVal pwszDevice As OLE_HANDLE, ByVal pszPort As OLE_HANDLE, ByVal pdm As OLE_HANDLE) As OLE_HANDLE Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As OLE_HANDLE) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As OLE_HANDLE, ByVal nXPos As Long, ByVal nYPos As Long) As OLE_COLOR #End If Public Function GetColor(ByVal x As Long, ByVal y As Long) As OLE_COLOR #If VBA7 Then Dim hDC As LongPtr, NullPtr As LongPtr #Else Dim hDC As OLE_HANDLE, NullPtr As LongPtr #End If NullPtr = 0 hDC = CreateDCW(StrPtr("DISPLAY"), NullPtr, NullPtr, NullPtr) GetColor = GetPixel(hDC, x, y) DeleteDC hDC End Function
Option Explicit Private Type COLORREF R As Byte G As Byte B As Byte 'Reserved As Byte End Type Private Type DWORD Value As Long End Type Private Sub ColorTest() Dim c As COLORREF Dim d As DWORD d.Value = vbRed LSet c = d Debug.Print Hex(d.Value) 'FF Debug.Print Hex(c.R), Hex(c.G), Hex(c.B) 'FF 0 0 d.Value = vbBlue LSet c = d Debug.Print Hex(d.Value) 'FF0000 Debug.Print Hex(c.R), Hex(c.G), Hex(c.B) '0 0 FF d.Value = &HEDA96521 LSet c = d Debug.Print Hex(d.Value) 'EDA96521 Debug.Print Hex(c.R), Hex(c.G), Hex(c.B) '21 65 A9 End Sub