下面代码能能使窗体透明,但控件不透明:
创新互联建站作为成都网站建设公司,专注重庆网站建设公司、网站设计,有关企业网站设计方案、改版、费用等问题,行业涉及成都被动防护网等多个领域,已为上千家企业服务,得到了客户的尊重与认可。
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 WS_EX_TRANSPARENT = H20
Private Const LWA_ALPHA = H2
Private Const LWA_COLORKEY = H1
Private Sub Form_Load()
Dim rtn As Long, ctrol As Control
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_TRANSPARENT
SetWindowLong hwnd, GWL_EXSTYLE, rtn
Me.Show
DoEvents
For Each ctrol In Me.Controls
ctrol.Refresh
Next
'SetLayeredWindowAttributes hwnd, 0, 100, LWA_ALPHA '100值可调,0-255之间,越小透明度越高
'SetLayeredWindowAttributes hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY '将窗体上的黑颜色去掉
End Sub
PNG透明时,在窗体上的颜色是这个窗体的BackColor
可以将窗体的TransparencyKey属性设为BackColor
将控件的背景色设为透明即可:
如Label1.BackColor=Color.
Transparent
当然,还有别的方式可使底色透明,你自已琢磨一下。
选择要设置透明度的控件,然后找到该控件的Opacity属性,修改里面的百分比就可以达到透明的效果,输入的值越小,控件就越透明。
PictureBox1.BackColor=Color.Transparent
用PS处理成gif格式的就可以了,vb不支持PNG图片,但是gif是可以的
先把窗体的颜色换成其它的,然后在用SetLayeredWindowAttributes函数设置窗体的背景色为透明,如果觉得窗体的标题栏没透明而不美观的话,可以把窗体的BorderStyle设为0。 代码举例(在窗体代码区中加入):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()
Me.BackColor
=
RGB(210,
210,
210)
'背景透明
Dim
crKey
As
Long,
bAlpha
As
Byte,
dwFlags
As
Long
Call
SetWindLong
dwFlags
=
1
'1
只有
crKey
指定的颜色才透明,忽略
bAlpha
参数
crKey
=
Me.BackColor
'窗体中设为透明的颜色
SetLayeredWindowAttributes
Me.hWnd,
crKey,
bAlpha,
dwFlags
End
Sub Private
Sub
SetWindLong()
'为调用
SetLayeredWindowAttributes
做准备
Dim
ExsTyle
As
Long
Const
WS_EX_Layered
=
H80000,
GWL_ExsTyle
=
-20
ExsTyle
=
GetWindowLong(Me.hWnd,
GWL_ExsTyle)
ExsTyle
=
ExsTyle
Or
WS_EX_Layered
SetWindowLong
Me.hWnd,
GWL_ExsTyle,
ExsTyle
End
Sub