VB_窗体透明

来源:互联网 发布:ios源码 编辑:程序博客网 时间:2024/05/08 15:41

 我们知道VB没有Delphi的直接可以改变透明度的属性,那么就需要使用API来实现了也~

  1. Private Const LWA_ALPHA = 
  2. Private Const LWA_COLORKEY = 
  3. Private Const GWL_EXSTYLE = -20
  4. Private Const WS_EX_LAYERED = 
  5. ’定义一些常数,用于API函数的使用
  6. Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal Hwnd As LongByVal nIndex As LongAs Long
  7. Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal Hwnd As LongByVal crKey As LongByVal bAlpha As ByteByVal dwFlags As LongAs Long
  8. Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal Hwnd As LongByVal nIndex As LongByVal dwNewLong As LongAs Long
  9. ’hwnd表示一个窗口的句柄,用来控制一个窗口是必备的,每一次窗口载入后的句柄是不同
  10. ’的!
  11. '定义了一个子过程,来调用上面几个API函数
  12. Public Sub setfrm(frm As Form, ByVal limpid As Long' 设置窗体透明度
  13.     Call SetWindowLong(frm.Hwnd, GWL_EXSTYLE, GetWindowLong(frm.Hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
  14.     Call SetLayeredWindowAttributes(frm.Hwnd, 0, limpid, LWA_ALPHA)    'limpid在0--255之间
  15. End Sub
  16. ’以上的代码可以放在窗口代码中,不过最好放在一个模块下。
  17. Private Sub Form_Load()
  18. setfrm Me, 200'设置透明度为200,透明度可以是0~255的任意整数,注意也可以表示成 
  19. 'Call setfrm (me,200)
  20. End Sub

而如果要做到类似千千的淡入淡出的效果,用个时间控件就可以了

  1. Private Const LWA_ALPHA = 
  2. Private Const LWA_COLORKEY = 
  3. Private Const GWL_EXSTYLE = -20
  4. Private Const WS_EX_LAYERED = 
  5. '定义一些常数,用于API函数的使用
  6. Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal Hwnd As LongByVal nIndex As LongAs Long
  7. Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal Hwnd As LongByVal crKey As LongByVal bAlpha As ByteByVal dwFlags As LongAs Long
  8. Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal Hwnd As LongByVal nIndex As LongByVal dwNewLong As LongAs Long
  9. 'hwnd表示一个窗口的句柄,用来控制一个窗口是必备的,每一次窗口载入后的句柄是不同
  10. '的!
  11. '定义了一个子过程,来调用上面几个API函数
  12. Public Sub setfrm(frm As Form, ByVal limpid As Long' 设置窗体透明度
  13.     Call SetWindowLong(frm.Hwnd, GWL_EXSTYLE, GetWindowLong(frm.Hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
  14.     Call SetLayeredWindowAttributes(frm.Hwnd, 0, limpid, LWA_ALPHA)    'limpid在0--255之间
  15. End Sub
  16. '以上的代码可以放在窗口代码中,不过最好放在一个模块下。
  17. Private Sub Form_Load()
  18.  Timer1.Enabled = 1’令时间控件响应事件
  19.  Timer1.Interval = 10’定义间隔时间
  20. End Sub
  21. Private Sub Timer1_Timer()
  22. Static i%
  23. i = i + 2
  24. If i < 253 Then
  25.  setfrm Me, i
  26. Else
  27.  Timer1.Enabled = 0
  28. End If
  29. End Sub'一个十分简单的循环
原创粉丝点击