3个回答
展开全部
VB模块代码 控制窗体透明度 'VB 模块 , 可实现窗体透明渐变特效,同时可以直接定义某个窗体的半透明度.
Option Explicit
'API
Public Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "User32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public TransFormFUN As Long
'这里说一下要传递的值, [目标窗体,最小值,渐变到最大值,渐变速度(推荐2),渐变类型是淡入还是淡出,执行完毕后是否显示目标窗体.]
Public Function TransForm(ByVal TFvForm As Form, TFvMin As Byte, TFvMax As Byte, TFvSpeed As Byte, TFvType As Boolean, TFvVisible As Boolean)
On Error Resume Next
Dim TFvI As Integer
TransFormFUN = GetWindowLong(TFvForm.hwnd, GWL_EXSTYLE)
TransFormFUN = TransForm Or WS_EX_LAYERED
SetWindowLong TFvForm.hwnd, GWL_EXSTYLE, TransFormFUN
TFvI = 0
If TFvMax >= 255 Then TFvMax = 255
If TFvMin <= 0 Then TFvMin = 0
TFvForm.Show
Select Case TFvType
Case True
SetLayeredWindowAttributes TFvForm.hwnd, 0, 0, LWA_ALPHA
TFvI = TFvMin
Do Until TFvI = TFvMax
If TFvI >= TFvMax Then Exit Do
TFvI = TFvI + TFvSpeed
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
DoEvents
Loop
Case False
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
TFvI = TFvMax
Do Until TFvI = TFvMin
If TFvI <= TFvMin Then Exit Do
TFvI = TFvI - TFvSpeed
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
DoEvents
Loop
End Select
If TFvVisible = True Then
TFvForm.Visible = True
Exit Function
End If
If TFvVisible = False Then TFvForm.Visible = False
End Function
'直接定义窗体透明度 [目标窗体,透明度 0 - 255]
Public Function SetTFVal(ByVal TFvForm As Form, TFvValue As Byte)
'直接设定窗体的透明度
On Error Resume Next
If TFvValue > 255 Then
SetLayeredWindowAttributes TFvForm.hwnd, 0, 255, LWA_ALPHA
Exit Function
Else
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvValue, LWA_ALPHA
End If
End Function
Option Explicit
'API
Public Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "User32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public TransFormFUN As Long
'这里说一下要传递的值, [目标窗体,最小值,渐变到最大值,渐变速度(推荐2),渐变类型是淡入还是淡出,执行完毕后是否显示目标窗体.]
Public Function TransForm(ByVal TFvForm As Form, TFvMin As Byte, TFvMax As Byte, TFvSpeed As Byte, TFvType As Boolean, TFvVisible As Boolean)
On Error Resume Next
Dim TFvI As Integer
TransFormFUN = GetWindowLong(TFvForm.hwnd, GWL_EXSTYLE)
TransFormFUN = TransForm Or WS_EX_LAYERED
SetWindowLong TFvForm.hwnd, GWL_EXSTYLE, TransFormFUN
TFvI = 0
If TFvMax >= 255 Then TFvMax = 255
If TFvMin <= 0 Then TFvMin = 0
TFvForm.Show
Select Case TFvType
Case True
SetLayeredWindowAttributes TFvForm.hwnd, 0, 0, LWA_ALPHA
TFvI = TFvMin
Do Until TFvI = TFvMax
If TFvI >= TFvMax Then Exit Do
TFvI = TFvI + TFvSpeed
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
DoEvents
Loop
Case False
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
TFvI = TFvMax
Do Until TFvI = TFvMin
If TFvI <= TFvMin Then Exit Do
TFvI = TFvI - TFvSpeed
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvI, LWA_ALPHA
DoEvents
Loop
End Select
If TFvVisible = True Then
TFvForm.Visible = True
Exit Function
End If
If TFvVisible = False Then TFvForm.Visible = False
End Function
'直接定义窗体透明度 [目标窗体,透明度 0 - 255]
Public Function SetTFVal(ByVal TFvForm As Form, TFvValue As Byte)
'直接设定窗体的透明度
On Error Resume Next
If TFvValue > 255 Then
SetLayeredWindowAttributes TFvForm.hwnd, 0, 255, LWA_ALPHA
Exit Function
Else
SetLayeredWindowAttributes TFvForm.hwnd, 0, TFvValue, LWA_ALPHA
End If
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询