首先,必需使用win2000。否则,自己实现透明算法,非常麻烦。
然后对需要的窗口调用如下的api函数:
SetLayeredWindowAttributes
The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.BOOL SetLayeredWindowAttributes(
HWND hwnd, // handle to the layered window
COLORREF crKey, // specifies the color key
BYTE bAlpha, // value for the blend function
DWORD dwFlags // action
);
Parameters
hwnd
[in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created.
crKey
[in] Pointer to a COLORREF value that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.
bAlpha
[in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
dwFlags
[in] Specifies an action to take. This parameter can be one or more of the following values. Value Meaning
LWA_COLORKEY Use crKey as the transparency color.
LWA_ALPHA Use bAlpha to determine the opacity of the layered window..
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
Note that once SetLayeredWindowAttributes has been called for a layered window, subsequent UpdateLayeredWindow calls will fail until the layering style bit is cleared and set again.For more information, see Layered Windows.Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.See Also
Windows Overview, Window Functions, Layered Windows, AlphaBlend, CreateWindowEx, SetWindowLong, TransparentBlt, UpdateLayeredWindow, COLORREF, RGB Platform SDK Release: February 2001 Contact Platform SDK Order a Platform SDK CD Online Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
See Also
Windows Overview, Window Functions, Layered Windows, AlphaBlend, CreateWindowEx, SetWindowLong, TransparentBlt, UpdateLayeredWindow, COLORREF, RGB
然后对需要的窗口调用如下的api函数:
SetLayeredWindowAttributes
The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.BOOL SetLayeredWindowAttributes(
HWND hwnd, // handle to the layered window
COLORREF crKey, // specifies the color key
BYTE bAlpha, // value for the blend function
DWORD dwFlags // action
);
Parameters
hwnd
[in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created.
crKey
[in] Pointer to a COLORREF value that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.
bAlpha
[in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
dwFlags
[in] Specifies an action to take. This parameter can be one or more of the following values. Value Meaning
LWA_COLORKEY Use crKey as the transparency color.
LWA_ALPHA Use bAlpha to determine the opacity of the layered window..
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
Note that once SetLayeredWindowAttributes has been called for a layered window, subsequent UpdateLayeredWindow calls will fail until the layering style bit is cleared and set again.For more information, see Layered Windows.Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.See Also
Windows Overview, Window Functions, Layered Windows, AlphaBlend, CreateWindowEx, SetWindowLong, TransparentBlt, UpdateLayeredWindow, COLORREF, RGB Platform SDK Release: February 2001 Contact Platform SDK Order a Platform SDK CD Online Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
See Also
Windows Overview, Window Functions, Layered Windows, AlphaBlend, CreateWindowEx, SetWindowLong, TransparentBlt, UpdateLayeredWindow, COLORREF, RGB
解决方案 »
- vb中如何获得一个过程是否执行完毕?
- 如何使用vb创建excel文件?要求不用建立excel模板的,谢谢
- 看不懂...
- 足彩复式投注的中奖匹配算法问题
- CreateFile这个函数怎 么用,急啊。!!!!!!分不够可再加。
- 关于VB操作word调试中的2个小问题
- 如何在VB中使TEXTBOX只能输入数字,并且数字多于5位
- 如何在activeX控件中获取控件自身的绝对位置???
- VB安装程序在简体中文和繁体中文操作系统的问题!!
- 我编写了一个文档阅读器,怎样才能在打开文本文件时自动用文档阅读器装入该文档
- shellexecute谁会用?!谢谢谢谢
- 各位大仙,就是如何在visual basic6中设置一个文件夹为共享文件夹,如果有Api那么用什么函数???????,小弟先谢了
请问你有自己是窗口透明的算法么?最好是VB的
Long, lpRECT As RECT) As LongPrivate Declare Function GetClientRect Lib "user32" (ByVal hWnd As _
Long, lpRECT As RECT) As LongPrivate Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As _
Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal _
nCombineMode As Long) As LongPrivate Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, _
ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As LongPrivate Declare Function ScreenToClient Lib "user32" (ByVal hWnd As _
Long, lpPoint As POINTAPI) As LongPrivate Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As _
Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As LongConst RGN_AND = 1
Const RGN_COPY = 5
Const RGN_DIFF = 4
Const RGN_OR = 2
Const RGN_XOR = 3Private 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
Sub DataSamp()
Dim ad As Database
Dim aserch As QueryDef
End SubPublic Sub MakeTransparent(frm As Form)
Dim rctClient As RECT, rctFrame As RECT
Dim hClient As Long, hFrame As Long
'获得窗口矩形区域
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
SetWindowRgn frm.hWnd, hFrame, True
End SubPrivate Sub Form_Click()
MakeTransparent Me
End Sub
我看到的有两种方法,但都不理想。你可以试一试。
一:
为 了 便 于 观 察 , 先 在 窗 口 上 添 加 两 个 按 钮 , Command1和 Command2。 加 上 如 下 代 码 :
Private Sub Command1_Click()
Print "Hello"
End Sub
Private Sub Command2_Click()
End
End Sub
这 两 个 按 钮 一 个 用 于 终 止 程 序 运 行 , 一 个 用 于 显 示 文 字 。
然 后 , 将 Form的 Border设 为 None。
最 后 , 在 Form的 声 明 部 分 加 上 以 下 代 码 :
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 SetParent Lib "user32" (ByVal hWndChild _
As Long, ByVal hWndNewParent As Long) As Long
Const WS_EX_TRANSPARENT = &H20&
Const GWL_EXSTYLE = (-20)
最 后 , 在 Form中 加 上 如 下 代 码 。
Private Sub Form_Load()
SetWindowLong hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT
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
其中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, &HFF0000, 0, LWA_COLORKEY '将扣去窗口中的蓝色
End Sub
中变透明的图片.我见过一个这样的例子 , 但它把透明代码封在DLL中, 可以在Win98中实现
SetLayeredWindowAttributes 在98能用吗? 我的好像不行啊!
你在那里见过实现半透明的DLL呢?请介绍一下!