论坛风格切换
正版合作和侵权请联系 sd173@foxmail.com
 
  • 帖子
  • 日志
  • 用户
  • 版块
  • 群组
帖子
购买邀请后未收到邀请联系sdbeta@qq.com
  • 1551阅读
  • 0回复

用VB如何实现屏幕振动? [复制链接]

上一主题 下一主题
离线gsl27
 

发帖
4831
今日发帖
最后登录
2023-11-18
只看楼主 倒序阅读 使用道具 楼主  发表于: 2008-06-18 16:53:38
用VB如何实现屏幕振动?

  用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,你可以实验一下振动效果了,双击屏幕恢复。