用API函数取色后,如何将其分成RGB颜色?
问题:
用API函数取色后,是一个10进制的数值,如何将其分成RGB颜色?
方法一:
用 HEX 函数将数值转换为 16 进制,然后,每两个切分一下就可以得到 RGB 数值了
Function C10ToRGB_1(lngColor As Long) As String
Dim strR As String
Dim strG As String
Dim strB As String
strR = lngColor Mod 256
strG = lngColor \ 256 Mod 256
strB = lngColor \ 256 \ 256
C10ToRGB_1 = strR & "_" & strG & "_" & strB
End Function
Sub test1()
Debug.Print C10ToRGB_1(33023)
Debug.Print RGB(255, 128, 0)
End Sub
方法二:
【转载】
如果要将VB的颜色转换为COLORREF,需要使用OleTranslateColor函数。例子:
Private Declare Function OleTranslateColor Lib "olepro32.dll" _
(ByVal OLE_COLOR As Long, _
ByVal HPALETTE As Long, _
pccolorref As Long) As Long
Private Sub GetRBGFromOLEColour(ByVal dwOleColour As Long, r As Long, g As Long, b As Long)
'pass a hex colour, return the rgb components
Dim clrref As Long
'translate OLE color to valid color if passed
OleTranslateColor dwOleColour, 0, clrref
b = (clrref \ 65536) And &HFF
g = (clrref \ 256) And &HFF
r = clrref And &HFF
Text1(0).Text = dwOleColour
Text1(1).Text = clrref
End Sub
更完整的例子参考:http://www.mvps.org/vbnet/index.html?code/system/oletranslatecolor.htm
方法三:
用 HEX 函数将数值转换为 16 进制,然后,每两个切分一下就可以得到 RGB 数值了
Sub test1()
Debug.Print C10ToRGB(33023)
Debug.Print RGB(255, 128, 0)
End Sub
Function C10ToRGB(lngA As Long) As String
Dim strR As String
Dim strG As String
Dim strB As String
Dim strHEX As String
strHEX = Right("00000" & Hex(lngA), 6)
'Debug.Print "B" & Mid(strHEX, 1, 2)
'Debug.Print "G" & Mid(strHEX, 3, 2)
'Debug.Print "R" & Mid(strHEX, 5, 2)
strB = C16To10(Mid(strHEX, 1, 2))
strG = C16To10(Mid(strHEX, 3, 2))
strR = C16To10(Mid(strHEX, 5, 2))
C10ToRGB = strR & "," & strG & "," & strB
'Debug.Print C10ToRGB
End Function
'以下函数将 16 进制数值转换为 10 进制数值
Private Function C16To10(strA As String) As Double
Dim a As Double
Dim b As String
Dim c As Double
Dim l As Integer
Dim i As Long
l = Len(strA)
For i = 1 To l
b = Mid(strA, i, 1)
Select Case b
Case "A"
b = 10
Case "B"
b = 11
Case "C"
b = 12
Case "D"
b = 13
Case "E"
b = 14
Case "F"
b = 15
End Select
c = c + b * 16 ^ (l - 1)
l = l - 1
Next
C16To10 = c
'Debug.Print C16To10
End Function
如何取色你可以参考本站
http://access911.net/index.asp?board=4&recordid=71FAB31E16DC