vb中设置窗体透明?
各位高手,在vb中如何设置窗体透明?就是设置它的背景色是透明的。在属性栏的backcolor好像没有透明选择。...
各位高手,在vb中如何设置窗体透明?就是设置它的背景色是透明的。在属性栏的backcolor好像没有透明选择。
展开
3个回答
展开全部
Option Explicit
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Sub Form_Load()
Form1.BorderStyle = 0
Dim Bitmap
Me.AutoRedraw = True
Bitmap = CreateCompatibleBitmap(Me.hdc, 0, 0)
SelectObject Me.hdc, Bitmap '设置窗体透明
End Sub
Private Sub Form_Click()
End '单击窗体任何地方都是退出程序
End Sub
自己写的一段代码。你看一下吧。。我写是完全的。直接没有了标题栏,如果想要标题栏的话。把其中的 Form1.BorderStyle = 0 这句去掉就可以了。
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Sub Form_Load()
Form1.BorderStyle = 0
Dim Bitmap
Me.AutoRedraw = True
Bitmap = CreateCompatibleBitmap(Me.hdc, 0, 0)
SelectObject Me.hdc, Bitmap '设置窗体透明
End Sub
Private Sub Form_Click()
End '单击窗体任何地方都是退出程序
End Sub
自己写的一段代码。你看一下吧。。我写是完全的。直接没有了标题栏,如果想要标题栏的话。把其中的 Form1.BorderStyle = 0 这句去掉就可以了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
原理:
先把窗体背景设置为一种特殊色
然后用SetLayeredWindowAttributes函数扣除那个颜色
代码:
模块:
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 Const LWA_COLORKEY = &H1
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
窗体载入时候代码Form_Load:
Me.BackColor=&H458B34 '设置特殊颜色
SetWindowLong Me.hWnd,GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE)
SetLayeredWindowAttributes me.hwnd, &H458B34, 0, LWA_COLORKEY
楼上火星了 这只是一种
第二种合并区域 利用GDI 不过效率不高
第三种利用GDI+ 可以创建透明通道 不过代码复杂。只要扣色上个代码即可。
==================================
To 楼上:
楼上的代码是模拟没有自动重绘;但是有个问题就是在没有重绘的时候 窗体的背景就是固定的;而且一般这种窗口会支持移动,那么就出现错误了;如果你要不断刷新,那么你的程序的效率就会很低;任何此类的LayeredWindow都是正解;包括全透明或者是半透明或者是部分透明或者是 透明通道;只不过有一些有直接API实现;有一些需要结合一堆API实现。
先把窗体背景设置为一种特殊色
然后用SetLayeredWindowAttributes函数扣除那个颜色
代码:
模块:
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 Const LWA_COLORKEY = &H1
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
窗体载入时候代码Form_Load:
Me.BackColor=&H458B34 '设置特殊颜色
SetWindowLong Me.hWnd,GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE)
SetLayeredWindowAttributes me.hwnd, &H458B34, 0, LWA_COLORKEY
楼上火星了 这只是一种
第二种合并区域 利用GDI 不过效率不高
第三种利用GDI+ 可以创建透明通道 不过代码复杂。只要扣色上个代码即可。
==================================
To 楼上:
楼上的代码是模拟没有自动重绘;但是有个问题就是在没有重绘的时候 窗体的背景就是固定的;而且一般这种窗口会支持移动,那么就出现错误了;如果你要不断刷新,那么你的程序的效率就会很低;任何此类的LayeredWindow都是正解;包括全透明或者是半透明或者是部分透明或者是 透明通道;只不过有一些有直接API实现;有一些需要结合一堆API实现。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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 WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_ALPHA = &H2
Const LWA_COLORKEY = &H1
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()
Dim rtn As Long
rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes Me.hwnd, 0, 150, LWA_ALPHA
End Sub
Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_ALPHA = &H2
Const LWA_COLORKEY = &H1
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()
Dim rtn As Long
rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes Me.hwnd, 0, 150, LWA_ALPHA
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询