vb 窗体 透明
发布网友
发布时间:2022-05-20 12:31
我来回答
共3个回答
热心网友
时间:2023-10-27 01:08
有API~==,我把这个封装过一个Class~
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "MakeTrans"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'****************************************
'/// Project:Boxing Transparent Window
'/// File:MakeTrans.cls
'/// Edition:Version 1.0.0 Beta1
'/// Coder:KingsamChen [Son Of Darkness]
'/// Last Modify:2008-1-28
'****************************************
'=========================================Get Windows Extra Style=========================================
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 Const GWL_EXSTYLE = (-20)
'=========================================Get Windows Extra Style=========================================
'=========================================Set Windows Transparence========================================
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 LWA_ALPHA = &H2
'=========================================Set Windows Transparence========================================
Private m_lngOldExStyle As Long
Public Sub SetWindowTransparence(cTransForm As Form, bytTransKey As Byte)
'********************************************************
'/// 函数名: SetWindowTransparence
'/// 输 入: cTransForm(Form) - 要设置透明的窗体
'/// bytTransKey - 透明度 范围:0-255 数值越低,透明度越高
'/// 输 出: -
'/// 功 能: 令指定窗体透明
'********************************************************
Dim lngExStyle As Long, lngFrmHwnd As Long
lngFrmHwnd = cTransForm.hwnd
lngExStyle = GetWindowLong(lngFrmHwnd, GWL_EXSTYLE)
m_lngOldExStyle = lngExStyle
lngExStyle = lngExStyle Or WS_EX_LAYERED '/// 新样式
Call SetWindowLong(lngFrmHwnd, GWL_EXSTYLE, lngExStyle)
Call SetLayeredWindowAttributes(lngFrmHwnd, 0, bytTransKey, LWA_ALPHA) '/// 窗体透明
End Sub
Public Sub Restore(cResForm As Form)
'********************************************************
'/// 函数名: Restore
'/// 输 入: cResForm(Form) - 要设置透明的窗体
'/// 输 出: -
'/// 功 能: 复原窗体样式
'********************************************************
Call SetWindowLong(cResForm.hwnd, GWL_EXSTYLE, m_lngOldExStyle)
End Sub
'****************************************
'/// Project:Boxing Transparent Window
'/// File:frmMain.frm
'/// Edition:Version 1.0.0 Final
'/// Coder:KingsamChen [Son Of Darkness]
'/// Last Modify:2008-1-28
'****************************************
Dim cTrans As MakeTrans
Private Sub Form_Click()
Call cTrans.Restore(Me)
End Sub
Private Sub Form_Load()
Set cTrans = New MakeTrans
Call cTrans.SetWindowTransparence(Me, 128)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set cTrans = Nothing
End Sub
热心网友
时间:2023-10-27 01:09
没有小点么?你看看 窗体 的属性面板
热心网友
时间:2023-10-27 01:09
试试这个吧。
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const WS_EX_TRANSPARENT As Long = &H20&
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 Sub Form_Load()
Dim Ret As Long
Ret = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
Ret = Ret Or WS_EX_LAYERED Or WS_EX_TRANSPARENT
SetWindowLong Me.hWnd, GWL_EXSTYLE, Ret
SetLayeredWindowAttributes Me.hWnd, 0, 200, LWA_ALPHA
End Sub
呵呵,再试试这个:
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
'其中hwnd是透明窗体的句柄,crKey为颜色值,bAlpha是透明度,取值范围是[0,255],dwFlags是透明方式,可以取两个值:当取值为LWA_ALPHA时,crKey参数无效,bAlpha参数有效;当取值为LWA_COLORKEY时,bAlpha参数有效而窗体中的所有颜色为crKey的地方将变为透明--这个功能很有用:我们不必再为建立不规则形状的窗体而调用一大堆区域分析、创建、合并函数了,只需指定透明处的颜色值即可,哈哈哈哈!请看具体代码。
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
'代码: 一个半透明窗体
Private Sub Form_Load()
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, 0, 200, LWA_ALPHA
End Sub