VB中自定义一个调色板

这是一个关于在VB中创建和使用自定义调色板的代码示例。通过`CreateColorPal`函数可以将一个对象设置为调色板,`GetPicColor`函数用于从调色板中获取指定位置的颜色值。此外,还包含设置按钮颜色和图片复制的功能。
 
  1. '
  2. '图形颜色类'
  3. Option Explicit
  4. Dim Colors() As Variant
  5. Dim ColorToUse As Long
  6. '
  7. '建立一个调色板.
  8. '函数名: CreateColorPal
  9. '入口参数: ColorPic As Object
  10. '说明:ColorPic 欲设置成调色板的对象名
  11. '作用: 将一个对象设置成一个调色板
  12. Public Sub CreateColorPal(ColorPic As Object)
  13.     Dim I As Long
  14.     ColorPic.AutoRedraw = True
  15.     ColorPic.Scale (0, 0)-(16, 3)
  16.     Colors = Array(16777215, 14737632, 12632319, 12640511, _
  17.                    14745599, 12648384, 16777152, 16761024, _
  18.                    16761087, 192, 16576, 49344, _
  19.                    49152, 12632064, 12582912, 12583104, _
  20.                    12632256, 4210752, 8421631, 8438015, _
  21.                    8454143, 8454016, 16777088, 16744576, _
  22.                    16744703, 128, 16512, 32896, _
  23.                    32768, 8421376, 8388608, 8388736, _
  24.                    8421504, 0, 255, 33023, _
  25.                    65535, 65280, 16776960, 16711680, _
  26.                    16711935, 64, 4210816, 16448, _
  27.                    16384, 4210688, 4194304, 4194368)
  28.     
  29.     For I = 0 To 15
  30.         ColorPic.Line (I, 0)-(I + 1, 1), Colors(I), BF
  31.         ColorPic.Line (I, 1)-(I + 1, 2), Colors(I + 16), BF
  32.         ColorPic.Line (I, 2)-(I + 1, 3), Colors(I + 32), BF
  33.         If I > 0 Then
  34.             ColorPic.Line (I, 0)-(I, 3)
  35.         End If
  36.     Next I
  37.     ColorPic.Line (0, 1)-(16, 1)
  38.     ColorPic.Line (0, 2)-(16, 2)
  39. End Sub
  40. '
  41. '从调色板中取颜色.
  42. '函数名: GetPicColor
  43. '入口参数: ColorPic As Object, x As Single, y As Single
  44. '返回值:该点的颜色值
  45. '说明:ColorPic已设置成调色板的对象名:(X,Y)该点坐标.
  46. '作用: 从调色板中取(x,y)点颜色值
  47. '*注: 请在MouseDown 或 MouseUp事件中使用
  48. Public Function GetPicColor(ColorPic As Object, X As Single, Y As SingleAs Long
  49.     On Error Resume Next
  50.     Dim W As Long, h As Long, c As Long
  51.     
  52.     W = ColorPic.ScaleWidth
  53.     h = ColorPic.ScaleHeight
  54.     If (X <= 0) Or (X >= W) Or (Y <= 0) Or (Y > h) Then
  55.          Exit Function
  56.     End If
  57.     c = Fix(X) + Fix(Y) * 16
  58.     ColorToUse = Colors(c)
  59.     GetPicColor = ColorToUse
  60. End Function
  61. Private Sub Class_Initialize()
  62.     Dim T As New ClsRev
  63.     Call T.GetIniVal
  64.     Set T = Nothing
  65. End Sub
  66. '
  67. '设置按钮颜色.
  68. '函数:SetComFore
  69. '参数:ObjWin 目标窗体名.FontColor 按钮的字体颜色,PTwidth 如果存在图片,设置图片与文件字间距.
  70. '返回值:无
  71. Public Function SetComFore(ObjWin As ObjectOptional FontColor As Long = 0, Optional PTwidth As Long = 0)
  72.        Dim Frm As Form
  73.        Set Frm = ObjWin
  74.        
  75.        With SetFrmCom
  76.             Call .ChComFcolor(Frm, .CjhPicToComm, .CjhFontSize, .CjhPicSize, FontColor, PTwidth)
  77.        End With
  78.        Unload SetFrmCom
  79.        Set SetFrmCom = Nothing
  80. End Function
  81. '
  82. '图片到图片复制..
  83. '函数:PicToPic
  84. '参数:BigWidth 最大宽度.BigHeight 最大高度,SourPic 源图片框,ObjPic 目标图片框.
  85. '返回值:无
  86. Public Function PicToPic(BigWidth As Long, BigHeight As LongByRef SourPic As ObjectByRef ObjPic As Object)
  87.     Dim RName As String
  88.     Dim Pw As Long
  89.     Dim Ph As Long
  90.     Dim T1 As Double
  91.     Dim T2 As Double
  92.     
  93.     Pw = SourPic.Width: Ph = SourPic.Height
  94.     
  95.     If SourPic.Picture <> 0 Then
  96.        ObjPic.Visible = False
  97.        T2 = Pw / Ph
  98.        T1 = BigWidth / BigHeight
  99.        If Pw > BigWidth Or Ph > BigHeight Then
  100.             If T2 > T1 Then
  101.                ObjPic.Width = BigWidth
  102.                ObjPic.Height = BigWidth / T2
  103.             Else
  104.                ObjPic.Width = BigHeight * T2
  105.                ObjPic.Height = BigHeight
  106.             End If
  107.        Else
  108.             ObjPic.Width = Pw
  109.             ObjPic.Height = Ph
  110.        End If
  111.        ObjPic.Picture = SourPic.Picture
  112.        ObjPic.Move (BigWidth - ObjPic.Width) / 2, (BigHeight - ObjPic.Height) / 2
  113.        ObjPic.Visible = True
  114.     End If
  115. End Function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值