关于窗体透明的问题

sgzhxfsg 2010-10-24 09:27:53
以下是一个窗体透明的代码

'透明度调整
Option Explicit

'Transparancy API's
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 Declare Function UpdateLayeredWindow Lib "user32" (ByVal hWnd As Long, ByVal hdcDst As Long, pptDst As Any, psize As Any, ByVal hdcSrc As Long, pptSrc As Any, crKey As Long, ByVal pblend As Long, ByVal dwFlags As Long) As Long
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 Const GWL_EXSTYLE = (-20)
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
Private Const ULW_COLORKEY = &H1
Private Const ULW_ALPHA = &H2
Private Const ULW_OPAQUE = &H4
Private Const WS_EX_LAYERED = &H80000


Public Function isTransparent(ByVal hWnd As Long) As Boolean
On Error Resume Next
Dim Msg As Long
Msg = GetWindowLong(hWnd, GWL_EXSTYLE)
If (Msg And WS_EX_LAYERED) = WS_EX_LAYERED Then
isTransparent = True
Else
isTransparent = False
End If
If Err Then
isTransparent = False
End If
End Function

Public Function MakeTransparent(ByVal hWnd As Long, ByVal Perc As Integer) As Long
Dim Msg As Long
On Error Resume Next

'Perc = 200
If Perc < 0 Or Perc > 255 Then
MakeTransparent = 1
Else
Msg = GetWindowLong(hWnd, GWL_EXSTYLE)
Msg = Msg Or WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, Msg
SetLayeredWindowAttributes hWnd, 0, Perc, LWA_ALPHA
MakeTransparent = 0
End If
If Err Then
MakeTransparent = 2
End If
End Function

Public Function MakeOpaque(ByVal hWnd As Long) As Long
Dim Msg As Long
On Error Resume Next
Msg = GetWindowLong(hWnd, GWL_EXSTYLE)
Msg = Msg And Not WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, Msg
SetLayeredWindowAttributes hWnd, 0, 0, LWA_ALPHA
MakeOpaque = 0
If Err Then
MakeOpaque = 2
End If
End Function



使用
MakeTransparent A0A0.hWnd, B0A0 使窗体透明
A0A0为窗体名
B0A0为透明度

我在程序运行后执行了这个函数
窗体得到了透明
但我又想在窗体得到了透明后
点击一个按钮后窗体不透明
我试过了把透明度设置为255
窗体看上去的确不透明了
但程序还是执行了透明操作
只不过透明度为255
我想让窗体回到刚刚开始的时候
没有执行透明函数以前的状态
有没有办法呢?
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgzhxfsg 2010-10-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lxq19851204 的回复:]

有结果就可以结帖了
[/Quote]

唉,我也想结啊,但是不能给自己分啊,呼,乱找个人结了
redshiliu 2010-10-25
  • 打赏
  • 举报
回复
自己写的一个控件,也许对你有帮助。
http://download.csdn.net/source/2781902
lxq19851204 2010-10-24
  • 打赏
  • 举报
回复
有结果就可以结帖了
sgzhxfsg 2010-10-24
  • 打赏
  • 举报
回复
我刚做了一个实验
用了这个
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const WS_EX_LAYERED As Long = &H80000
Public Const GWL_EXSTYLE As Long = -20

SetWindowLong A0A0.hWnd, GWL_EXSTYLE, GetWindowLong(A0A0.hWnd, GWL_EXSTYLE) And Not WS_EX_LAYERED

有我想要的效果了
Kill2010 2010-10-24
  • 打赏
  • 举报
回复
问题是什么?
sgzhxfsg 2010-10-24
  • 打赏
  • 举报
回复
我不想重新加载窗体,因为窗体上有一些内容变了,像用户在text1里输入了内容等等

1,486

社区成员

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

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