请问那里有通过指定一个背景颜色让窗体透明的源码(高分)

cuy 2001-09-25 10:55:06
我想给窗体一个背景图
让他把背景色相同的部分透明掉。
有没有这样的源码啊
我的最高分了。
...全文
175 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuiyxy 2001-09-26
  • 打赏
  • 举报
回复
API函数SetLayeredWindowAttributes 在W98下没有
所以不行
SwordGrass 2001-09-25
  • 打赏
  • 举报
回复
http://swordgrass.myrice.com
这里有。
cuy 2001-09-25
  • 打赏
  • 举报
回复
一定给分。
但我没看懂。
请给源程序,谢谢
这段源程序我有啊。
我是说指定一种颜色让它透明。
而且是能应用在给窗体加一个图像背景的情况下!
htu 2001-09-25
  • 打赏
  • 举报
回复
to Jneu(沧海桑田):看不懂啊,透明色在那里呀!
Jneu 2001-09-25
  • 打赏
  • 举报
回复
一定要给分哦!
Jneu 2001-09-25
  • 打赏
  • 举报
回复
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd _
As Long, lpRECT As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd _
As Long, lpRECT As RECT) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn _
As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, _
ByVal nCombineMode As Long) As Long
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As _
Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) _
As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd _
As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As _
Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Const RGN_XOR = 3

Private Type POINTAPI
x As Long
Y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private rctClient As RECT, rctFrame As RECT
Private hClient As Long, hFrame As Long

Public Sub MakeTransparent(frm As Form)
GetFrameClientRgn frm
SetWindowRgn frm.hWnd, hFrame, True
End Sub

Private Sub GetFrameClientRgn(frm As Form)
GetWindowRect frm.hWnd, rctFrame
GetClientRect frm.hWnd, rctClient

'将窗口矩形坐标转换为屏幕坐标
Dim lpTL As POINTAPI, lpBR As POINTAPI
lpTL.x = rctFrame.Left
lpTL.Y = rctFrame.Top
lpBR.x = rctFrame.Right
lpBR.Y = rctFrame.Bottom
ScreenToClient frm.hWnd, lpTL
ScreenToClient frm.hWnd, lpBR
rctFrame.Left = lpTL.x
rctFrame.Top = lpTL.Y
rctFrame.Right = lpBR.x
rctFrame.Bottom = lpBR.Y
rctClient.Left = Abs(rctFrame.Left)
rctClient.Top = Abs(rctFrame.Top)
rctClient.Right = rctClient.Right + Abs(rctFrame.Left)
rctClient.Bottom = rctClient.Bottom + Abs(rctFrame.Top)
rctFrame.Right = rctFrame.Right + Abs(rctFrame.Left)
rctFrame.Bottom = rctFrame.Bottom + Abs(rctFrame.Top)
rctFrame.Top = 0
rctFrame.Left = 0

hClient = CreateRectRgn(rctClient.Left, rctClient.Top, _
rctClient.Right, rctClient.Bottom)
hFrame = CreateRectRgn(rctFrame.Left, rctFrame.Top, _
rctFrame.Right, rctFrame.Bottom)

CombineRgn hFrame, hClient, hFrame, RGN_XOR
End Sub

Private Sub Form_Resize()
MakeTransparent Me
End Sub

mishow 2001-09-25
  • 打赏
  • 举报
回复
好说,下载LYFTOOLS控件,有这个属性
cuy 2001-09-25
  • 打赏
  • 举报
回复
呵第二个方法已经在2000下测试通过
还请问大家有没有在98下实现类似方案的方法呢??
谢谢了!
cuy 2001-09-25
  • 打赏
  • 举报
回复
这个为什么不能用在动态的给设置背景图呢
cuy 2001-09-25
  • 打赏
  • 举报
回复
这个办法虽然不错。
但只能在2000下不好用啊。
能不能把什么东西打包进去在98下实现??
cuiyxy 2001-09-25
  • 打赏
  • 举报
回复
========在win2000下有效===========

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

'代码二: 形状不规则的窗体
'Private Sub Form_Load()
' Dim rtn As Long
' BorderStyler = 0
' rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
' rtn = rtn Or WS_EX_LAYERED
' SetWindowLong hwnd, GWL_EXSTYLE, rtn
' SetLayeredWindowAttributes hwnd, &H80C0FF, 0, LWA_COLORKEY '将扣去窗口中的蓝色
'End Sub
'

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧