vb中如何使窗体半透明,控件不透明?
如题PrivateDeclareFunctionGetWindowLongLib"user32"Alias"GetWindowLongA"(ByValhwndAsLong...
如题
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 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, 100, LWA_ALPHA '100值可调,0-255之间,越小透明度越高
' SetLayeredWindowAttributes hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY '将窗体上的黑颜色去掉
End Sub
使用LWA_ALPHA,窗体和控件都半透明。使用LWA_COLORKEY,可将指定颜色透明,但不是半透明。有什么方法可将窗体半透明控件不透明吗? 展开
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 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, 100, LWA_ALPHA '100值可调,0-255之间,越小透明度越高
' SetLayeredWindowAttributes hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY '将窗体上的黑颜色去掉
End Sub
使用LWA_ALPHA,窗体和控件都半透明。使用LWA_COLORKEY,可将指定颜色透明,但不是半透明。有什么方法可将窗体半透明控件不透明吗? 展开
6个回答
展开全部
下面代码能能使窗体透明,但控件不透明:
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
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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
VB6里要想让窗体透明可难啦,而且效果不好。
如果控件有自己的hwnd窗体句柄,只要把hwnd窗体句柄设置成指定的控件或者窗体的hwnd,也就是说精确的设定需要半透明的窗体句柄。
如果控件有自己的hwnd窗体句柄,只要把hwnd窗体句柄设置成指定的控件或者窗体的hwnd,也就是说精确的设定需要半透明的窗体句柄。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
半透明窗体
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 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
完全透明窗体
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 LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Sub Form_Load()
Me.BackColor = &HFF0000
Dim rtn As Long
BorderStyler = 0
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, &HFF0000, 0, LWA_COLORKEY
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 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
完全透明窗体
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 LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Sub Form_Load()
Me.BackColor = &HFF0000
Dim rtn As Long
BorderStyler = 0
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, &HFF0000, 0, LWA_COLORKEY
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
const
lwa_colorkey
=
&h1
const
lwa_alpha
=
&h2
const
gwl_exstyle
=
(-20)
const
ws_ex_layered
=
&h80000
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
'set
the
window
style
to
'layered'
ret
=
getwindowlong(me.hwnd,
gwl_exstyle)
ret
=
ret
or
ws_ex_layered
setwindowlong
me.hwnd,
gwl_exstyle,
ret
'set
the
opacity
of
the
layered
window
to
128
'我们可以设置这个数值来控制透明程度
setlayeredwindowattributes
me.hwnd,
0,
128,
lwa_alpha
end
sub
lwa_colorkey
=
&h1
const
lwa_alpha
=
&h2
const
gwl_exstyle
=
(-20)
const
ws_ex_layered
=
&h80000
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
'set
the
window
style
to
'layered'
ret
=
getwindowlong(me.hwnd,
gwl_exstyle)
ret
=
ret
or
ws_ex_layered
setwindowlong
me.hwnd,
gwl_exstyle,
ret
'set
the
opacity
of
the
layered
window
to
128
'我们可以设置这个数值来控制透明程度
setlayeredwindowattributes
me.hwnd,
0,
128,
lwa_alpha
end
sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在最新的visual Studio编译环境下,安装的VBasic的窗口form控件在属性中是可以直接设置控件的不透明度百分比,而且所有参数都有中文说明,在属性栏中对应的英文参数是Opacity,
太老的vb6.0感觉界面还是windows 2000样子一样,新的visual studio 界面很友好啦,集成了vb到VC++一整套的,我也是入门,看起来VS明显界面更友好,而且也有教程可以学习,
太老的vb6.0感觉界面还是windows 2000样子一样,新的visual studio 界面很友好啦,集成了vb到VC++一整套的,我也是入门,看起来VS明显界面更友好,而且也有教程可以学习,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询