1 Jcvjuc

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

' ' ' ' ' ' ' ' ' ' ' ' ' ' '

'

------------------------------------------- In The Name Of The Most High Snow Simulator 1.0 - The program choose n particle initially in the screen and their properties such as Vx,Vy and Color. then animate them in each timer interval. By: Vincent - Junior Programming All comments are welcome Email: icen.inside@gmail.com Cilegon - 2010 --------------------------------------------

DefLng A-Z 'define Long type as default declaration of variables. Option Explicit 'API Declaretions: Private Declare Function GetPixel Lib "gdi32" (ByVal hdc, ByVal x, ByVal y) As L ong Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Lon g, ByVal y As Long, ByVal crColor As Long) As Long Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Lo ng, ByVal y As Long, ByVal crColor As Long) As Long Private Declare Function GetNearestColor Lib "gdi32" (ByVal hdc As Long, ByVal c rColor As Long) As Long Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal x1 As L ong, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long '-- Device Context functions Private Declare Function GetDC Lib "user32" (ByVal Hwnd As Long) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As L ong Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal n Width As Long, ByVal crColor As Long) As Long Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObj ect As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal Hwnd As Long, ByVal hdc A s Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Lon g Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As L ong, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As L ong, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

'dwRop in BitBlt (and StretchBlt): Private Const SRCPAINT = &HEE0086 Private Const SRCERASE = &H440328 Private Const SRCAND = &H8800C6 Private Const SRCCOPY = &HCC0020 Private Const SRCINVERT = &H660046

'dest 'dest 'dest 'dest 'dest

= = = = =

source source source source source

OR dest AND (NOT dest ) AND dest XOR dest

'Custom Declarations: Dim nSnow As Long Dim VxMinSnow As Single, VxMaxSnow As Single, VyMinSnow As Single, VyMaxSnow As Single Dim VxAddMin As Single, VxAddMax As Single, VyAddMin As Single, VyAddMax As Sing le Dim WidthWindowSnow, HeightWindowSnow Dim xSnow() As Single, ySnow() As Single, VxSnow() As Single, VySnow() As Single Dim ColPrevSnow(), ColSnow() As Long Dim hdcSnow As Long, HwndSnow As Long Dim StopSnow As Integer, DontClearParticles As Boolean Dim IsInAnimateSnow As Boolean, IsInCmdFall As Boolean Private Sub CommandFallSnow_Click() If IsInCmdFall Then Exit Sub IsInCmdFall = True Timer1.Enabled = False StopSnow = False ClearSnowParticles DontClearParticles = False 'the value True means that don't clear particles in ' ClearSnowParticles' sub nSnow = Val(TextNParticle.Text) If nSnow < 0 Then nSnow = 250 Dim i SetInitialSnowPositions 'this loop is to reach to steady state motion! For i = 1 To 50 AnimateSnow False DoEvents Next If -1 = True Then 'this loop cause to see begin of falling and particles do not appear instantly For i = 1 To nSnow ySnow(i) = ySnow(i) - (HeightWindowSnow + 1) * Sgn(VySnow(i)) Next End If AnimateSnow Timer1.Enabled = True IsInCmdFall = False End Sub Sub SetInitialSnowPositions(Optional DrawInitialParticles As Boolean = False)

ReDim xSnow(nSnow), ySnow(nSnow), VxSnow(nSnow), VySnow(nSnow), ColPrevSnow(nSno w), ColSnow(nSnow) Dim w, h, hdc, i, x, y, c hdc = hdcSnow w = WidthWindowSnow: h = HeightWindowSnow 'set a x,y position , velocity(x,y) and a color for each particle: For i = 1 To nSnow x = Rnd * w: y = Rnd * h xSnow(i) = x: ySnow(i) = y If Rnd < 0.3 Then c = &HFFFFFF ' &HFFEFEF Else c = 150 + Rnd * (260 - 150): If c > 255 Then c = 255 c = GetRealNearestColor(hdc, RGB(c, c, c)) End If ColSnow(i) = c ColPrevSnow(i) = GetPixel(hdc, x, y) VxSnow(i) = VxMinSnow + Rnd * (VxMaxSnow - VxMinSnow) VySnow(i) = VyMinSnow + Rnd * (VyMaxSnow - VyMinSnow) If DrawInitialParticles Then SetPixelV hdc, x, y, c Next End Sub Sub SetSpeed() Dim vx As Single, vy As Single, r As Single 'Vx,yMin,MaxSnow: determine Min,Max value of absolute speed 'VxMinSnow = -1.5: VxMaxSnow = 1.5 'VyMinSnow = -1: VyMaxSnow = 2 'Vx,yAddMin,Max: determine Min,Max of rate of change in speed (i.e. acceleration ) VxAddMin = -0.1: VxAddMax = 0.1 VyAddMin = -0.1: VyAddMax = 0.1 vx = HScroll1(1).Value / 2: vy = HScroll1(2).Value / 2 r = HScroll1(3).Value / 4 VxMinSnow = vx - r / 2: VxMaxSnow = vx + r / 2 VyMinSnow = vy - r / 2: VyMaxSnow = vy + r / 2 End Sub Sub AnimateSnow(Optional DrawParticles As Boolean = -1) If IsInAnimateSnow Then Exit Sub IsInAnimateSnow = True Dim w, h, hdc, i, x As Single, y As Single, vx As Single, vy As Single, c Dim j, jx, c2 hdc = hdcSnow w = WidthWindowSnow: h = HeightWindowSnow If DrawParticles Then 'clear old pixels: For i = nSnow To 1 Step -1

c = ColPrevSnow(i) If c <> -1 Then SetPixelV hdc, xSnow(i), ySnow(i), c Next End If For i = 1 To nSnow x = xSnow(i): y = ySnow(i) vx = VxSnow(i) + VxAddMin + Rnd * (VxAddMax - VxAddMin) vy = VySnow(i) + VyAddMin + Rnd * (VyAddMax - VyAddMin) SetValueInRange vx, VxMinSnow, VxMaxSnow SetValueInRange vy, VyMinSnow, VyMaxSnow VxSnow(i) = vx: VySnow(i) = vy x = x + vx: y = y + vy If Not StopSnow Then 'SetValueInRange y, 0, h, True If y > h And vy >= 0 Then y = 0 Else If y < 0 And vy <= 0 Then y = h End If End If SetValueInRange x, 0, w, True c = GetPixel(hdc, x, y) 'return (-1) if x,y be out of real part of window xSnow(i) = x: ySnow(i) = y ColPrevSnow(i) = c If DrawParticles And c <> -1 Then SetPixelV hdc, x, y, ColSnow(i) End If Next IsInAnimateSnow = False End Sub Sub ClearSnowParticles() 'clear current particles Dim hdc, i, c If DontClearParticles Then Exit Sub 'particles are cleared before and must be c lear again (because hdc may be changed!) hdc = hdcSnow For i = nSnow To 1 Step -1 c = ColPrevSnow(i) If c <> -1 Then SetPixelV hdc, xSnow(i), ySnow(i), c Next End Sub Sub SetValueInRange(v As Variant, ByVal RangeMin As Variant, ByVal RangeMax As V ariant, Optional SwapMaxMin As Boolean = False) If SwapMaxMin Then 'swapMaxMin=True: If v < RangeMin Then v = RangeMax Else If v > RangeMax Then v = RangeMin Else 'default (swapmaxmin=false) If v < RangeMin Then v = RangeMin Else If v > RangeMax Then v = RangeMax End If End Sub

Private Sub CommandLoadPic_Click() On Error Resume Next CD1.ShowOpen If Err Then Err.Clear: Exit Sub ClearSnowParticles DontClearParticles = True Dim w, h With PictureTmp .Picture = LoadPicture(CD1.FileName) w = Picture1.ScaleWidth: h = Picture1.ScaleHeight Picture1.PaintPicture .Picture, 0, 0, w, h, 0, 0, .ScaleWidth, .ScaleHeight End With Option1_Click (0) CommandFallSnow_Click End Sub Private Sub CommandStopSnow_Click() StopSnow = True End Sub Private Sub Form_Load() Picture1.ScaleMode = vbPixels Me.Show: DoEvents Call SetSpeed 'ColSnow = &HFFFFFF 'ColSnow(i) = GetRealNearestColor(hdc, ColSnow(i)) Option1_Click (0) CommandFallSnow_Click End Sub Function GetRealNearestColor(ByVal hdc1 As Long, ByVal Col As Long) As Long Dim c As Long 'to get real color (using getnearestcolor function may have some problems! c = GetPixel(hdc1, 1, 1) If c <> -1 Then GetRealNearestColor = SetPixel(hdc1, 1, 1, Col) SetPixelV hdc1, 1, 1, c Else GetRealNearestColor = Col 'faild to test End If End Function Private Sub HScroll1_Change(Index As Integer) If Index = 0 Then 'timer Timer1.Interval = HScroll1(0).Value Else 'wind,weight or random Call SetSpeed End If End Sub Private Sub HScroll1_Scroll(Index As Integer) HScroll1_Change Index End Sub Private Sub Label5_Click() MsgBox "Snow Simulator (First Release)" + vbCrLf + vbCrLf + "anggit-coder@" + Ch r(89) + "kalimantan-cyber" + ".c" + Chr(111) + "m" + vbCrLf + "By:anjrit404" + S pace(20) + "Brebes - 2013" + Space(4) + vbCrLf, , "About..."

End Sub Private Sub Option1_Click(Index As Integer) If Option1(Index).Value = 0 Then Option1(Index).Value = 1 Dim obj(3) As Object, Cobj As Object Set obj(1) = Picture1: Set obj(2) = Dir1: Set obj(3) = Me DeleteUsedSnowDC DontClearParticles = True If Index <> 1 And Dir1.Enabled = False Then Dir1.Enabled = True Set Cobj = obj(Index + 1) With Cobj If Index = 1 Then .Enabled = False Else WidthWindowSnow = .ScaleWidth - 1: HeightWindowSnow = .ScaleHeight - 1 End If HwndSnow = .Hwnd End With hdcSnow = GetDC(HwndSnow) If Timer1.Enabled Then CommandFallSnow_Click End Sub Private Sub TextNParticle_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0: CommandFallSnow_Click End Sub Private Sub Timer1_Timer() AnimateSnow End Sub Private Sub Form_Unload(Cancel As Integer) DeleteUsedSnowDC End Sub Sub DeleteUsedSnowDC() If hdcSnow <> 0 Then ClearSnowParticles ReleaseDC HwndSnow, hdcSnow End If End Sub

You might also like