• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

用API函数取色后,如何将其分成RGB颜色?

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网
用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



 

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网