首页 /编程语言和算法/VB6/VBA/ASP
 VB6 颜色转换问题,VB6和网页颜色互转,子程序 f_changeRRGGBB
2025年3月9日 00:44

VB 里颜色值的存储格式是 BGR(蓝 - 绿 - 红),并非通常的 RGB(红 - 绿 - 蓝)。

Private Function f_changeRRGGBB(ByVal strColorOld As String, Optional strSymbol As String = "&H") As String
    'RRGGBB 变成 BBGGRR
    Dim r$, g$, b$
    
    strColorOld = Right("000000" & strColorOld, 6) '补足六位

    If Left(strColorOld, 1) = "#" Then strColorOld = Mid(strColorOld, 2)    ' 去除可能存在的 # 符号
    If UCase(Left(strColorOld, 2)) = "&H" Then strColorOld = Mid(strColorOld, 3)    ' 去除可能存在的 &H 符号
    
    r = Mid(strColorOld, 1, 2)    ' 提取红、绿、蓝分量
    g = Mid(strColorOld, 3, 2)
    b = Mid(strColorOld, 5, 2)

    f_changeRRGGBB = strSymbol & b & g & r
End Function

Private Sub Command1_Click()
    Debug.Print f_changeRRGGBB("AABBCC")
End Sub

运行结果:

&HCCBBAA


 
全部回复(0)
首页 | 电脑版 |