多边形颜色渐变填充C语言,vb6:用渐变填充多边形

下面的代码将绘制一个渐变填充矩形。我从vbcity.com的这个帖子稍微修改了一下。

将其放入模块中:

Public Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long

Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Public Sub gdiDrawGradient( _

ByVal hdc As Long, _

ByRef rct As RECT, _

ByVal lEndColor As Long, _

ByVal lStartColor As Long, _

ByVal bVertical As Boolean)

Dim lStep As Long

Dim lPos As Long, lSize As Long

Dim bRGB(1 To 3) As Integer

Dim bRGBStart(1 To 3) As Integer

Dim dR(1 To 3) As Double

Dim dPos As Double, d As Double

Dim hBr As Long

Dim tR As RECT

LSet tR = rct

If bVertical Then

lSize = (tR.Bottom - tR.Top)

Else

lSize = (tR.Right - tR.Left)

End If

lStep = lSize 255

If (lStep < 3) Then

lStep = 3

End If

bRGB(1) = lStartColor And &HFF&

bRGB(2) = (lStartColor And &HFF00&) &H100&

bRGB(3) = (lStartColor And &HFF0000) &H10000

bRGBStart(1) = bRGB(1): bRGBStart(2) = bRGB(2): bRGBStart(3) = bRGB(3)

dR(1) = (lEndColor And &HFF&) - bRGB(1)

dR(2) = ((lEndColor And &HFF00&) &H100&) - bRGB(2)

dR(3) = ((lEndColor And &HFF0000) &H10000) - bRGB(3)

For lPos = lSize To 0 Step -lStep '

' Draw bar

If bVertical Then

tR.Top = tR.Bottom - lStep

Else

tR.Left = tR.Right - lStep

End If

If tR.Top < rct.Top Then

tR.Top = rct.Top

End If

If tR.Left < rct.Left Then

tR.Left = rct.Left

End If

hBr = CreateSolidBrush((bRGB(3) * &H10000 + bRGB(2) * &H100& + bRGB(1)))

FillRect hdc, tR, hBr

DeleteObject hBr

' Adjust colour '

dPos = ((lSize - lPos) / lSize)

If bVertical Then

tR.Bottom = tR.Top

bRGB(1) = bRGBStart(1) + dR(1) * dPos

bRGB(2) = bRGBStart(2) + dR(2) * dPos

bRGB(3) = bRGBStart(3) + dR(3) * dPos

Else

tR.Right = tR.Left

bRGB(1) = bRGBStart(1) + dR(1) * dPos

bRGB(2) = bRGBStart(2) + dR(2) * dPos

bRGB(3) = bRGBStart(3) + dR(3) * dPos

End If

Next lPos

End Sub

要进行测试,请将此代码添加到表单中:

Private Sub Command1_Click()

Dim r As RECT

r.Left = 10

r.Top = 10

r.Right = 100

r.Bottom = 150

Call gdiDrawGradient(Me.hdc, r, vbRed, vbBlue, True)

End Sub

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值