Option Explicit Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long Private Sub Command1_Click() Dim lngFormBackColor As Long Dim lngR As Long Dim lngG As Long Dim lngB As Long lngFormBackColor = Me.BackColor '如果获取的颜色是小于0的,那么他是系统颜色。必须使用GetSysColor转换 '转换的规则是带入系统颜色的低字节,也就是系统颜色编号,来转换 If lngFormBackColor < 0 Then lngFormBackColor = GetSysColor(lngFormBackColor And &HFF&) lngR = lngFormBackColor Mod &H100 lngG = (lngFormBackColor \ &H100) Mod &H100 lngB = (lngFormBackColor \ &H10000) Mod &H100 Debug.Print "背景色是:&H" & Hex(lngFormBackColor) Debug.Print "转换为RGB后是:RGB(" & CStr(lngR) & "," & CStr(lngG) & "," & CStr(lngB) & ")" End Sub
[其他解释]
Option Explicit Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Sub Command1_Click() Dim lngFormBackColor As Long Dim lngR As Long Dim lngG As Long Dim lngB As Long lngFormBackColor = Me.BackColor lngR = lngFormBackColor Mod &H100 lngG = (lngFormBackColor \ &H100) Mod &H100 lngB = (lngFormBackColor \ &H10000) Mod &H100 Debug.Print "背景色是:&H" & Hex(lngFormBackColor) Debug.Print "转换为RGB后是:RGB(" & CStr(lngR) & "," & CStr(lngG) & "," & CStr(lngB) & ")" End Sub
Private Sub Form_Load() Me.BackColor = &H808090 End Sub
Option Explicit Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Sub Command1_Click() Dim lngFormBackColor As Long Dim lngR As Long Dim lngG As Long Dim lngB As Long lngFormBackColor = Me.BackColor lngR = lngFormBackColor AND &HFF lngG = (lngFormBackColor AND &HFF00) \ 256 lngB = (lngFormBackColor AND &HFF0000) \ 65536 Debug.Print "背景色是:&H" & Hex(lngFormBackColor) Debug.Print "转换为RGB后是:RGB(" & CStr(lngR) & "," & CStr(lngG) & "," & CStr(lngB) & ")" End Sub