好久没下雨了,好想雨啊~用VB做了一个下雨的效果,很粗糙,希望能给大家做一点参考~
窗体背景设为&H800000,Autoredraw设为True,Width为9600,Height为7200,ScaleMode设为像素,在窗体上添加一个Timer控件,命名为tmr,Interval设为100,输入如下代码:
Option Explicit
Private Type POINT
x1 As Integer
y1 As Integer
x2 As Integer
y2 As Integer
v1 As Integer
v2 As Integer
c As Long
End Type
Private Const NUM_RAIN = 200
Private Const LONG_RAIN = 15
Private Const WIDTH_RAIN = -3
Private lWidth As Long
Private lLeft As Long
Private pRain() As POINT
Dim i As Integer
Dim lBlue As Long
Dim lOther As Long
Private Sub Form_Click()
Unload Me
End Sub
Private Sub Form_Load()
Show
Line (0, 475)-(640, 480), vbBlue, BF
ReDim pRain(NUM_RAIN) As POINT
lWidth = Me.ScaleWidth + ScaleHeight / LONG_RAIN * Abs(WIDTH_RAIN + IIf(WIDTH_RAIN < 0, -10, 10))
lLeft = -IIf(WIDTH_RAIN < 0, 0, ScaleHeight / LONG_RAIN * Abs(WIDTH_RAIN + IIf(WIDTH_RAIN < 0, -10, 10)))
For i = 0 To NUM_RAIN
With pRain(i)
.x1 = lLeft + Rnd * lWidth
Randomize
.y1 = Rnd * Me.ScaleHeight
.x2 = .x1 + WIDTH_RAIN
.y2 = .y1 + LONG_RAIN
Randomize
.v1 = Rnd * LONG_RAIN
.v1 = IIf(.v1 <= 1, 1, .v1)
Randomize
.v2 = Rnd * (10) + WIDTH_RAIN + IIf(WIDTH_RAIN < 0, -10, 10)
Randomize
lBlue = Rnd * 127 + 128
Randomize
lOther = Rnd * lBlue
.c = CLng("&H" & Hex$(lBlue) & Hex$(lOther) & Hex$(lOther))
End With
Next i
tmr.Enabled = True
End Sub
Private Sub tmr_Timer()
For i = 0 To NUM_RAIN
With pRain(i)
Line (.x1, .y1)-(.x2, .y2), BackColor
Line (0, 475)-(640, 480), vbBlue, BF
If .x2 >= lWidth Or .y2 >= Me.ScaleWidth Or .x2 <= lLeft Or .y2 <= 0 Then
Randomize
.x1 = lLeft + Rnd * lWidth
.y1 = 0
Randomize
.v1 = Rnd * LONG_RAIN
.v1 = IIf(.v1 <= 1, 1, .v1)
Randomize
.v2 = Rnd * (10) + WIDTH_RAIN + IIf(WIDTH_RAIN < 0, -10, 10)
Randomize
lBlue = Rnd * 127 + 128
Randomize
lOther = Rnd * lBlue
.c = CLng("&H" & Hex$(lBlue) & Hex$(lOther) & Hex$(lOther))
Else
.x1 = .x1 + .v2
.y1 = .y1 + .v1
End If
.x2 = .x1 + WIDTH_RAIN
.y2 = .y1 + LONG_RAIN
Line (.x1, .y1)-(.x2, .y2), .c
End With
Next i
End Sub
其中的常数NUM_RAIN代表雨的数量(实际多1),LONG_RAIN代表雨的高度,WIDTH_RAIN代表宽度(单位:像素)。程序的颜色深成部分还有一些错误,大家可以看看改改~
最终通过环境:VB6+Win2000