首先,打开VB,新建一个工程,加入一个模块,把我们的“作案工具”声明一下:
Public Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Type POINTAPI
x As Long
y As Long
End Type
声明完了,双击Form1,在“Form_Load”底下写上“Me.hide”,这样,运行时就不会看到窗体了。
然后,添加一个时钟,设置Interval为100,双击它并为它写这些代码:
Private Sub Timer1_Timer()
Dim targetDC As Long
Dim myPoint As POINTAPI
Static oldPoint As POINTAPI
'获取鼠标当前位置。
GetCursorPos myPoint
'如果鼠标没有动,就不画了。
If oldPoint.x = myPoint.x And oldPoint.y = myPoint.y Then Exit Sub
'记录鼠标新的位置,以便以后再判断鼠标有没有动。
oldPoint.x = myPoint.x
oldPoint.y = myPoint.y
'获取屏幕的DC,以便使用SetPixel往上面乱画。
targetDC = GetDC(0)
Dim width As Long, height As Long
'调用随机数,往屏幕上画一个略呈十字形的杂点。
width = Rnd(1) * 100
'在鼠标周围100×100的区域内画。
For x = myPoint.x - Rnd(1) * 100 To myPoint.x + width
height = Rnd(1) * 100
For y = myPoint.y - Rnd(1) * 100 To myPoint.y + height
'有1/7的地区被画了点。
If Int(Rnd(1) * 7) = 1 Then
SetPixel targetDC, x, y, Rnd(1) * &H1000000
End If
Next y
Next x
height = Rnd(1) * 100
For y = myPoint.y - Rnd(1) * 100 To myPoint.y + height
width = Rnd(1) * 100
For x = myPoint.x - Rnd(1) * 100 To myPoint.x + width
If Int(Rnd(1) * 7) = 1 Then
SetPixel targetDC, x, y, Rnd(1) * &H1000000
End If
Next x
Next y
End Sub
运行一下,拖动一下鼠标,怎么样?是不是一塌糊涂?
做完了,感觉怎么样?怎样去和朋友开玩笑,那就是你的事了。
不管怎么说,VB的速度总是不尽人意。我就是因为这个才抛弃了她的(我真是个喜新厌旧的陈世美呀!)。再拿一个Borland C++版的回去吧!还是免安装的“绿色害虫”呢!