'源代码
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
'GetDC()功能是获取指定窗体的设备场景的句柄(hDC),用参数0则可以获取整个屏幕的场景句柄
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
'GetPixel用于取得场景(这里是整个屏幕)中某点的颜色值
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
'SetPixel用于设置场景(这里是整个屏幕)中某点的颜色值
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
'释放由GetDC()获取的设备场景句柄,否则可能造成系统锁死
Private Declare Function InvalidateRect&Lib "user32" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long)
'好咐春清理窗口雪花
Private Type POINTAPI '定义简裂坐标点结构
x As Long
y As Long
End Type
Private Type RECT '定义“区域”数据结构,但实际上并没有用到,因为仅需在函数InvalidateRect中传递一个空的RECT参数
left As Long
top As Long
right As Long
bottom As Long
End Type
Dim rect1 As RECT
Private Const ScrnWidth = 1024 '屏幕宽度(单位:像素)
Private Const ScrnHight = 768 '屏幕高度(单位:像素)
Private Const SnowCol = &HFEFFFE '雪花颜色
Private Const SnowColDown = &HFFFFFF '积雪颜色
Private Const SnowColDuck = &HFFDDDD '深色积雪颜色
Private Const SnowNum = 500 '同一时间飘动的雪花数量
Dim hDC1 As Long '存储桌面窗口设备句柄
Dim pData(SnowNum) As POINTAPI '存储每个雪花的位置信息
Dim pColor(SnowNum) As Long '存储画出雪花前屏幕原来的颜色
Dim Vx As Integer '雪花总体水平飘行速度
Dim Vy As Integer '雪花总体垂直下落速度
Dim PVx As Integer '单个雪花实际水平飘行速度
Dim PVy As Integer '单个雪花实际垂直飘行速友耐度
'初始化雪花位置
Private Sub InitP(i As Integer)
pData(i).x = Rnd() * ScrnWidth
pData(i).y = Rnd() * 2
pColor(i) = GetPixel(hDC1, pData(i).x, pData(i).y) '取得屏幕原来的颜色值
End Sub
'取得某一点与周围点的对比度,确定是否在此位置堆积雪花
Private Function GetContrast(i As Integer) As Long
Dim ColorCmp As Long '存储用作对比的点的颜色值
Dim tempR As Long '存储CorlorCmp的红色部分,下同
Dim tempG As Long
Dim tempB As Long
Dim Slope As Integer '存储雪花飘落方向:Vx/Vy
'计算雪花飘落方向
If PVy <>0 Then
Slope = PVx / PVy
Else
Slope = 2
End If
'根据雪花飘落方向决定取哪一点作对比点,
'若PVx/PVy在-1到1之间,即Slope=0,就取正下面的象素点
'若PVx/PVy>1,取右下方的点,PVx/PVy<-1则取左下方
If Slope = 0 Then
ColorCmp = GetPixel(hDC1, pData(i).x, pData(i).y + 1)
Else
If Slope >1 Then
ColorCmp = GetPixel(hDC1, pData(i).x + 1, pData(i).y + 1)
Else
ColorCmp = GetPixel(hDC1, pData(i).x - 1, pData(i).y + 1)
End If
End If
'确定当前位置没有与另一个雪花重叠,否则返回0,用于防止由于不同雪花重叠造成雪花乱堆
If ColorCmp = SnowCol Then
GetContrast = 0
Exit Function
End If
'分别获取ColorCmp与对比点的蓝、绿、红部分的差值
tempB = Abs((ColorCmp And &HFF0000) - (pColor(i) And &HFF0000)) / &H10000
tempG = Abs((ColorCmp And &HFF00&) - (pColor(i) And &HFF00&)) / &H100&
tempR = Abs((ColorCmp And &HFF&) - (pColor(i) And &HFF&))
'返回对比度值
GetContrast = (tempR + tempG + tempB) / 3
End Function
'画出一帧,即重画所有雪花位置一次
Private Sub DrawP()
Dim i As Integer
For i = 0 To SnowNum
'防止雪花重叠造成干扰
If pColor(i) <>SnowCol Then
'还原上一个位置的颜色
SetPixel hDC1, pData(i).x, pData(i).y, pColor(i)
End If
'设置新的位置,i Mod 3用于将雪花分为三类采用不同速度,以便形成层次感
PVx = Rnd() * 2 - 1 + Vx * (i Mod 3)
PVy = Vy * (i Mod 3 + 1)
pData(i).x = pData(i).x + PVx
pData(i).y = pData(i).y + PVy
'取得新位置原始颜色值,用于下一步雪花飘过时恢复此处颜色
pColor(i) = GetPixel(hDC1, pData(i).x, pData(i).y)
'如果获取颜色失败,表明雪花已飘出屏幕,重新初始化
If pColor(i) = -1 Then
InitP i
Else
'否则若雪花没有重叠
If pColor(i) <>SnowCol Then
'若对比度较小(即不能堆积),就画出雪花
'Rnd()>0.3用于防止某些连续而明显的边界截获所有雪花
If Rnd() >0.3 Or GetContrast(i) <50 Then
SetPixel hDC1, pData(i).x, pData(i).y, SnowCol
'否则表明找到明显的边界,画出堆积的雪,并初始化以便画新的雪花
Else
SetPixel hDC1, pData(i).x, pData(i).y - 1, SnowColDuck
SetPixel hDC1, pData(i).x - 1, pData(i).y, SnowColDuck
SetPixel hDC1, pData(i).x + 1, pData(i).y, SnowColDown
InitP i
End If
End If
End If
Next
End Sub
Private Sub Form_Load()
Dim j As Integer
Me.Caption = "桌面飘雪" '设置窗口标题
'设置计时器,Timer1用于画单帧,Timer2用于风向变化
Timer1.Enabled = True
Timer1.Interval = 10
Timer2.Enabled = True
Timer2.Interval = 2000
Randomize '初始化随机数种子
hDC1 = GetDC(0) '获取桌面窗口设备场景句柄
'初始化整个屏幕
For j = 0 To SnowNum
pData(j).x = Rnd() * ScrnWidth
pData(j).y = Rnd() * ScrnHight
pColor(j) = GetPixel(hDC1, pData(j).x, pData(j).y)
Next
End Sub
Private Sub Form_Unload(Cancel As Integer)
ReleaseDC 0, hDC1 '释放桌面窗口设备句柄
InvalidateRect 0, rect1, 0 '清除所有雪花,恢复桌面
End Sub
Private Sub Timer1_Timer()
DrawP '画出一帧
End Sub
Private Sub Timer2_Timer()
'改变风向
Vx = Rnd() * 4 - 2
Vy = Rnd() + 2
End Sub
'完,最后,需要两个Timer:Timer1、Timer2。
不过你要是没学过vb那就有点困难了……做之前还是先找本关于Vb的书看看吧!
初始察仿化肢则void Init_Data()
{
int i
for( i=0i<POINTi++)
{
Snow[i].x=0
Snow[i].y=0
Snow[i].size=0
}
for( i=0i<MaxXi++)
Map[i]=MaxY
}
显示:
void MakeSnow()
{
int i
if( CurSnow>=POINT ) return
for( i=0Snow[i].sizei++ )
CurSnow++
Snow[i].x=random(MaxX)
Snow[i].y=random(DOWNSPEED)
Snow[i].size=random(MaxSize)+1
}
设定显示坐标
void ShowSnow( int x, int y, int size, int flag )
{
int color=0
if ( flag ) color=15
switch( size )
{
case 1:
putpixel( x, y, color )
break
case 2:
setcolor( color )
line( x-1, y-1, x+1, y+1 )
line( x-1, y+1, x+1, y-1 )
break
case 3:
setcolor( color )
line( x-1, y-1, x+1, y+1 )
line( x-1, y+1, x+1, y-1 )
/*
line( x-2, y-2, x+2, y+2 )
line( x-2, y+2, x+2, y-2 )*/
line( x-2, y, x+2, y )
line( x, y-2, x, y+2 )
break
}
}
雪花移动效果:
void Move( int n, int tox, int toy )
{
int x, y, size, i, j
float person
x=Snow[n].x
y=Snow[n].y
size=Snow[n].size
/* check end */
j=y
if( x<tox )
{
person=(DOWNSPEED *1.0) / ( tox-x )*1.0
for( i=xi<=toxi++ )
{
if( j>=Map[i] )
{
tox=i-size
break
}
j+=(int)( (i-x+1)*person )
}
}
else if( x>tox )
{
person=(DOWNSPEED *1.0) / ( x-tox )*1.0
for( i=xi>=toxi-- )
{
if( j>=Map[i] )
{
tox=i+size
break
}
j+=(int)( (x-i+1)*person )
}
}
if( y+DOWNSPEED>历没棚=Map[tox] )
{
switch( size )
{
case 1:
Map[x]--
break
case 2:
Map[x]-=2
if( x>0 &&Map[x-1]>Map[x] ) Map[x-1]=Map[x]
if( x<MaxX-1 &&Map[x+1]>Map[x] ) Map[x+1]=Map[x]
break
case 3:
Map[x]-=3
if( x>1 &&Map[x-2]>Map[x] ) Map[x-1]=Map[x]
if( x>0 &&Map[x-1]>Map[x] ) Map[x-1]=Map[x]
if( x<MaxX-2 &&Map[x+2]>Map[x] ) Map[x+1]=Map[x]
if( x<MaxX-1 &&Map[x+1]>Map[x] ) Map[x+1]=Map[x]
break
}
CurSnow--
y=Map[x]+size
Snow[n].x=x
Snow[n].y=y
Snow[n].size=0
}
else /* not end */
{
Snow[n].x=tox
Snow[n].y=toy
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)