利用Timer实现窗体淡入淡出的效果

'在窗口上添加一个Timer,默认设置
'代码如下:
Option Explicit
Private Declare Function GetWindowLong Lib “user32” Alias “GetWindowLongA” (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib “user32” Alias “SetWindowLongA” (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib “user32” (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Dim StepCount As Long, Toggle As Boolean

Private Sub Lucency(hwnd As Long, B As Long, LWA As Boolean)
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
Call SetWindowLong(hwnd, GWL_EXSTYLE, rtn)
If LWA And B > 255 Or B < 0 Then Exit Sub
Call SetLayeredWindowAttributes(hwnd, IIf(LWA <> True, B, 0), IIf(LWA, B, 0), IIf(LWA, LWA_ALPHA, LWA_COLORKEY))
End Sub

Private Sub Form_Load()
Timer1.Interval = 1
Toggle = True
Call Lucency(Me.hwnd, 0, True)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Cancel = 1
Toggle = False
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
If Toggle Then
If StepCount < 255 Then StepCount = StepCount + 5 Else Timer1.Enabled = False
Else
If StepCount > 0 Then StepCount = StepCount - 5 Else End
End If
Call Lucency(Me.hwnd, StepCount, True)
End Sub

你可能感兴趣的:(VisualBasic编程源码,VB6,窗口)