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 LWA_ALPHA = &H2 ':表示把窗体设置成半透明样式
Const LWA_COLORKEY = &H1 ':表示不显示窗体中的透明色
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
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 Sub Form_Load()
Static I As Long
Dim rtn As Long
I = 128
rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE) ':取的窗口原先的样式
rtn = rtn Or WS_EX_LAYERED ':使窗体添加上新的样式WS_EX_LAYERED
SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn ':把新的样式赋给窗体
SetLayeredWindowAttributes Me.hwnd, 0, I, LWA_ALPHA
'把窗体设置成半透明样式 , 第二个参数表示透明程度
'取值范围0 --255, 为0时就是一个全透明的窗体了
Me.Show
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
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT = &H20&
Private Const LWA_ALPHA = &H2&
Option Explicit
//还有种类似的"窗体" 可以隔着它点击 比如那个窗体是在桌面上,右键点击窗体,就是再右击桌面,好多桌面时钟呀~ 天气预报~什么都那样,这是怎么做的?
请参考MSDN关于WS_EX_TRANSPARENT扩展样式的示例:
http://support.microsoft.com/default.aspx?scid=kb;en-us;249341
' --- 代码 ---
Private Sub Form_Load()
Dim lOldStyle As Long
Dim bTrans As Byte ' The level of transparency (0 - 255)
bTrans = 128
lOldStyle = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
SetWindowLong Me.hwnd, GWL_EXSTYLE, lOldStyle Or WS_EX_LAYERED Or WS_EX_TRANSPARENT
SetLayeredWindowAttributes Me.hWnd, 0, bTrans, LWA_ALPHA
End Sub