在VB6.0中,如何让一个窗体实现淡入淡出效果?

如题,淡入淡出就是慢慢出来、慢慢消失。
2025-02-26 09:49:54
推荐回答(2个)
回答1:

'首先在窗体上添加一个Timer控件,设置好Timer控件的interval值(假设为3000)
'然后复制下面的语句:
Private Declare Function AnimateWindow Lib "user32" (ByVal hwnd As Long, _
ByVal dwTime As Long, ByVal dwFlags As Long) As Long

Private Sub Form_Load()
Me.BorderStyle = 1
End Sub

Private Sub Timer1_Timer()
'使窗口淡化退出,持续时间3秒
AnimateWindow Me.hwnd, 3000, &H80000 + &H10000
Unload Me
End Sub

回答2:

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

Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_ALPHA = &H2
'Const LWA_COLORKEY = &H1
Public i As Integer
Private Sub Form_Load()
Timer1.Interval = 10
Timer2.Interval = 10
Timer1.Enabled = True
Timer2.Enabled = False
i = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
Cancel = 1
i = 255
Timer2.Enabled = True
End Sub

Private Sub Timer1_Timer()
i = i + 5

SetWindowLong Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA '150为透明度(0-255)
If i = 255 Then Timer1.Enabled = False
End Sub

Private Sub Timer2_Timer()
i = i - 5

SetWindowLong Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA '150为透明度(0-255)
If i = 0 Then Timer1.Enabled = False: End
End Sub