VB代码注释 哪位高人帮帮我吧
Private Const MERGEPAINT = &HBB0226
Private Const SRCAND = &H8800C6
Private Const SRCCOPY = &HCC0020
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, 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
' Variables for positioning the ball.
Private Const BallR = 10 ' Radius.
Private Const BallD = 2 * BallR + 1 ' Diameter.
Private CurX As Single ' Position.
Private CurY As Single
Private OldX As Single
Private OldY As Single
Private VelX As Single ' Velocity.
Private VelY As Single
Private Xmax As Single ' Edge of canvas.
Private Ymax As Single
' Draw the ball at (CurX, CurY).
Private Sub DrawBall()
' Fix the part of the image that was covered.
BitBlt picCanvas.hDC, _
OldX - BallR, OldY - BallR, BallD, BallD, _
picHidden.hDC, OldX - BallR, OldY - BallR, SRCCOPY
OldX = CurX
OldY = CurY
' Redraw the ball.
picCanvas.Circle (CurX, CurY), BallR
' Update the display.
picCanvas.Refresh
End Sub
' Initialize values, and draw the initial ball.
Private Sub Form_Load()
' Make the form fit the picture.
Width = (Width - ScaleWidth) + picCanvas.Width
Height = (Height - ScaleHeight) + picCanvas.Height
Xmax = picCanvas.ScaleWidth - BallR
Ymax = picCanvas.ScaleHeight - BallR
' Set initial position and velocity.
Randomize
CurX = Int((Xmax - BallR + 1) * Rnd + BallR)
CurY = Int((Ymax - BallR + 1) * Rnd + BallR)
OldX = CurX
OldY = CurY
VelX = Int((10 - 5 + 1) * Rnd + 5)
VelY = Int((10 - 5 + 1) * Rnd + 5)
DrawBall
End Sub
Private Sub picCanvas_Click()
End Sub
' Move the ball.
Private Sub tmrBounce_Timer()
CurX = CurX + VelX
If (CurX > Xmax) Then
CurX = Xmax
VelX = -VelX
' Beep
ElseIf (CurX < BallR) Then
CurX = BallR
VelX = -VelX
' Beep
End If
CurY = CurY + VelY
If (CurY > Ymax) Then
CurY = Ymax
VelY = -VelY
' Beep
ElseIf (CurY < BallR) Then
CurY = BallR
VelY = -VelY
' Beep
End If
DrawBall
End Sub 展开
Option Explicit
'声明在变量使用之前必须先定义
Private Const MERGEPAINT = &HBB0226
Private Const SRCAND = &H8800C6
'MERGEPAINT与SRCAND在本段代码中没有使用
Private Const SRCCOPY = &HCC0020
'bitblt函数中的一个操作数
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, 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
'此函数用于对指定的区域中的像素进行位块(bit_block)转换,以传送到目标对象区域。
' Variables for positioning the ball.
'以下几个变量及常量的定义用于控制小球的位置、移动等参数
Private Const BallR = 10 ' 半径
Private Const BallD = 2 * BallR + 1 ' 直径
Private CurX As Single ' 圆球所在的当前x坐标
Private CurY As Single ' 圆球所在的当前y坐标
Private OldX As Single ' 上一次绘制圆的x坐标
Private OldY As Single ' 上一次绘制圆的y坐标
Private VelX As Single ' 水平位移距离
Private VelY As Single ' 垂直位移距离
Private Xmax As Single ' 圆心能够到达的最大X坐标
Private Ymax As Single ' 圆心能够到达的最大Y坐标
' DrawBall过程用于绘制圆球
Private Sub DrawBall()
' 抹去原来的小球
BitBlt picCanvas.hDC, _
OldX - BallR, OldY - BallR, BallD, BallD, _
picHidden.hDC, OldX - BallR, OldY - BallR, SRCCOPY
OldX = CurX
OldY = CurY
'把当前圆心的X坐标、Y坐标保存在OldX、OldY中
' 重新在 curX,CurY 坐标 绘制半径为ballR的圆球
picCanvas.Circle (CurX, CurY), BallR
' 刷新图片框
picCanvas.Refresh
End Sub
' 初始化小球的位置等信息,并绘制小球
Private Sub Form_Load()
' 是窗口大小恰好适应图片框的大小
Width = (Width - ScaleWidth) + picCanvas.Width
'计算窗体应该的宽度 =图片框的宽度+边框的宽度
Height = (Height - ScaleHeight) + picCanvas.Height
'计算窗体应该的高度 =图片框的高度+边框的高度
Xmax = picCanvas.ScaleWidth - BallR
'新的圆边界能到达的X坐标
Ymax = picCanvas.ScaleHeight - BallR
'新的圆边界能到达的y坐标
' 初始化圆球的位置和移动方向
Randomize'随机
CurX = Int((Xmax - BallR + 1) * Rnd + BallR)
CurY = Int((Ymax - BallR + 1) * Rnd + BallR)
'随机产生一个在图片框内的圆心坐标的X、Y
OldX = CurX
OldY = CurY
'把当前圆心的X坐标、Y坐标保存在OldX、OldY中
VelX = Int((10 - 5 + 1) * Rnd + 5)
VelY = Int((10 - 5 + 1) * Rnd + 5)
'随机产生VelX和VelY,即每次圆心所移动的距离,也可以说是移动速度
DrawBall
'调用DrawBall过程绘制圆球
End Sub
' 通过Timer移动圆球,从而达到动画移动效果
Private Sub tmrBounce_Timer()
CurX = CurX + VelX
'计算新的小球的X坐标
If (CurX > Xmax) Then
'如果新的x坐标到达图片框的最右侧
CurX = Xmax
'则将x坐标置为图片框的最右侧
VelX = -VelX
'移动的方向改为相反方向,即为向左
' Beep
'取消此处的注释则在小球到达图片框的最右侧时 咚 的 响一声,后面的一样,不再介绍
ElseIf (CurX < BallR) Then
'如果新的x坐标到达图片框的最左侧
CurX = BallR
'移动的方向改为相反方向,即为向右
VelX = -VelX
' Beep
End If
'计算新的小球的Y坐标
CurY = CurY + VelY
'如果新的Y坐标到达图片框的最下侧
If (CurY > Ymax) Then
'则将Y坐标置为图片框的最下侧
CurY = Ymax
'移动的方向改为相反方向,即为向上
VelY = -VelY
' Beep
ElseIf (CurY < BallR) Then
'如果新的Y坐标到达图片框的最上侧
CurY = BallR
'则将Y坐标置为图片框的最上侧
VelY = -VelY
'移动的方向改为相反方向,即为向下
' Beep
End If
'得到新的圆心后重回圆球
DrawBall
End Sub
这段代码使用了API,还用了两个图片框,其实这个程序不需要这么复杂,按照这里做节省许多代码,也不用那么多控件http://hi.baidu.com/kfqjhg/blog/item/bd5dc6034e0d67d8277fb5bd.html
关于BITBlt函数请参考:http://baike.baidu.com/view/1080366.htm
因为按照以上代码远点太小所以更改了圆的半径,运行后结果如图:
参考资料: http://hi.baidu.com/kfqjhg/blog/item/bd5dc6034e0d67d8277fb5bd.html