|
文/云飞 也许你有过这样的经历:好友寄来一封信,附件里有个小程序,你不假思索地打开了。这时,意想不到的事情发生了,整个屏幕抖动起来,怎么也控制不住。你顿时头大如斗,脑中一片空白,瘫倒在电脑旁......
用VB来实现屏幕振动,非常容易做到。如果你有兴趣,就跟我来吧!
编程思路
将整个屏幕抓到一个窗体内,将另外一个窗体的背景置为黑色作为背景,随机不断改变窗体的位置就产生了振动效果。
界面设计
首先创建两个窗体Form1和Form2,将Autoredraw属性设为True,Borderstyle属性设为None(即无边框),其中Form2作为背景,再将其BackColor属性设为黑色。在Form1上放置一定时器控件Timer1,将其Interval值设为100。
程序代码
Option Explicit
Dim screenhwnd, screendc, rc
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 '以下API函数可以通过VB自带的API文本浏览器复制而来。
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Sub Form_DblClick()
End '双击窗体结束程序
End Sub
Private Sub Form_Load()
Randomize
Form2.Width = Screen.Width '调整窗体的大小、位置使之覆盖整个屏幕
Form2.Height = Screen.Height + 100
Form2.Top = 0
Form2.Left = 0
Form1.Top = 0
Form1.Left = 0
Form1.Width = Screen.Width
Form1.Height = Screen.Height + 100
screenhwnd = GetDesktopWindow() '取得桌面窗口的句柄
screendc = GetDC(screenhwnd)
rc = BitBlt(Form1.hdc, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, screendc, 0, 0, &&HCC0020) '把整个屏幕抓到窗体中
rc = ReleaseDC(screenhwnd, screendc)
Form2.Show
Form1.Show
Form1.ZOrder '使窗体居于顶层
End Sub
Private Sub Timer1_Timer()
'随机改变窗体的位置
Form1.Top = Form1.Top + Rnd * 100
Form1.Left = Form1.Left + Rnd * 100
Form1.Top = Form1.Top - Rnd * 100
Form1.Left = Form1.Left - Rnd * 100
End Sub
OK,你可以实验一下振动效果了,双击屏幕恢复。
|