标题:发个下雪的代码!
取消只看楼主
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
 问题点数:0 回复次数:3 
发个下雪的代码!
发一个下雪代码
Option Explicit
'in form1  add timer
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function RedrawWindow Lib "user32" (ByVal hWnd As Long, lprcUpdate As Any, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long

Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long


Private Const SNOW_MAX& = 100
Private Const FALL_SPEED& = 3
Private Const COLOR_DIFF = 100
Dim ScreenDC&, ScreenW&, ScreenH&
Dim Snow&(SNOW_MAX, 1), Last&(SNOW_MAX)

Dim mlFrmWidth As Long
Dim mlFrmHeight As Long
Dim lbExit As Boolean

Private Sub Form_Click()
    lbExit = True
End Sub

Private Sub Form_Load()
'    Dim CER As Long
'    CER = CreateEllipticRgn(35, 10, 300, 200)
'    Call SetWindowRgn(Me.hWnd, CER, True)
   
    mlFrmWidth = Width
    mlFrmHeight = Height
    lbExit = False
    Timer1.Interval = 100
    Timer1.Enabled = True

End Sub
Private Sub NewSnow(i&)
    Snow(i, 0) = Rnd * ScreenW
    Snow(i, 1) = 0
    Last(i) = GetPixel(ScreenDC, Snow(i, 0), 0)
End Sub
Private Function ColorDec(Color1&, Color2&) As Long
    Dim R1%, G1%, B1%
    Dim R2%, G2%, B2%
    GetRGB Color1, R1, G1, B1
    GetRGB Color2, R2, G2, B2
    ColorDec = Abs(R1 - R2) + Abs(G1 - G2) + Abs(B1 - B2)
End Function
Private Sub GetRGB(ByVal Color&, ByRef r%, ByRef g%, ByRef b%)
    r = (Color Mod 256)
    b = (Int(Color \ 65536))
    g = ((Color - (b * 65536) - r) \ 256)
End Sub
Private Sub Form_Resize()
    If WindowState <> 1 Then
        Width = mlFrmWidth
        Height = mlFrmHeight
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    lbExit = True
    Erase Snow
    Erase Last
    RedrawWindow ScreenDC, ByVal 0, ByVal 0, &H1
    Set Form1 = Nothing
End Sub
Private Sub Timer1_Timer()
    Dim llCount As Long
    Timer1.Enabled = False
    Dim i  As Long, k As Long
    Dim lPic As Long
    Dim llColor As Long
    ScreenDC = GetWindowDC(0)
    ScreenW = Screen.Width / Screen.TwipsPerPixelX
    ScreenH = Screen.Height / Screen.TwipsPerPixelY
    Randomize
    For i = 0 To SNOW_MAX
        NewSnow i
    Next

    On Error Resume Next
    Do
'        If llCount Mod 20 = 0 Then
'            llColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
'            Label1.ForeColor = llColor
'            Label2.ForeColor = llColor
'            Label3.ForeColor = llColor
'        End If
'        llCount = llCount + 1
        For lPic = 0 To 7
        For i = 0 To SNOW_MAX
        
            SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1) + 1, Last(i)
            SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1), Last(i)
            SetPixel ScreenDC, Snow(i, 0), Snow(i, 1) + 1, Last(i)
            SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1) + 1, Last(i)
            SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1) + 1, Last(i)
            SetPixel ScreenDC, Snow(i, 0), Snow(i, 1), Last(i)
            SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1) - 1, Last(i)
            SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1), Last(i)
            SetPixel ScreenDC, Snow(i, 0), Snow(i, 1) - 1, Last(i)
            
            Snow(i, 0) = Snow(i, 0) + Rnd * FALL_SPEED - FALL_SPEED / 2 '左右随机偏转
            Snow(i, 1) = Snow(i, 1) + Rnd * FALL_SPEED '下落
            If Snow(i, 0) < 0 Or Snow(i, 0) > ScreenW Or Snow(i, 1) > ScreenH Then
                NewSnow i
            Else
                k = Last(i)
                Last(i) = GetPixel(ScreenDC, Snow(i, 0), Snow(i, 1))
               
                SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1) + 1, vbWhite
                SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1), vbWhite
                SetPixel ScreenDC, Snow(i, 0), Snow(i, 1) + 1, vbWhite
                SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1) + 1, vbWhite
                SetPixel ScreenDC, Snow(i, 0) + 1, Snow(i, 1) + 1, vbWhite
                SetPixel ScreenDC, Snow(i, 0), Snow(i, 1), vbWhite
                SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1) - 1, vbWhite
                SetPixel ScreenDC, Snow(i, 0) - 1, Snow(i, 1), vbWhite
                If Rnd * 3 < 1 And ColorDec(k, Last(i)) > COLOR_DIFF Then NewSnow i
            End If
        Next
        If lbExit = True Then Exit Do
        Sleep 20
        DoEvents
        Picture = Picture1(lPic).Picture
        Next
    Loop
    Unload Me
End Sub

下雪.rar (300.15 KB)
搜索更多相关主题的帖子: Long ByVal Lib Declare 
2007-12-19 17:40
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 
就是桌面上下雪的啊! 窗体,可以用来做美化的而已!

2007-12-20 14:31
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 
Private Declare Sub SHChangeNotify Lib "shell32" _
                                                  (ByVal wEventId As Long, _
                                                  ByVal uFlags As Long, _
                                                  ByVal dwItem1 As Long, _
                                                  ByVal dwItem2 As Long)
   
  Const SHCNE_UPDATEIMAGE = &H8000&
  Const SHCNF_FLUSHNOWAIT = &H2000
  Const SHCNF_DWORD = &H3
   
  Private Sub Command1_Click()
          Dim l     As Long
          l = -1
          SHChangeNotify SHCNE_UPDATEIMAGE, SHCNF_DWORD, l, 0
  End Sub


关闭的时候加上这句就可以实现自动刷新了

2007-12-20 14:54
心中有剑
Rank: 2
等 级:新手上路
威 望:5
帖 子:611
专家分:0
注 册:2007-5-18
得分:0 
楼上厉害,学习!
Set w = CreateObject("wscript.shell")
w.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
还是把这个去掉吧,要不有点耍流氓,哈哈

2007-12-20 18:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-193370-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.192954 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved