【【怎么在VB中让窗体透明?在XP下吧。要具体的说明】】『『是不是用api函数??』』

yuxiao99999 2003-06-25 02:38:39
以前在网易论坛里可以按名称查帖子,这里不可以吧?所以我的问题也许被别人问人问烂了,但还望高手告之。(网易的查出的结果都 是一些垃圾回复,说用api函数,又不说清,还望你能具体的讲解?)谢谢了!
...全文
90 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
搬板砖 2003-07-02
  • 打赏
  • 举报
回复
XP下有用::

函数SetLayeredWindowAttributes
  使用这个函数,可以轻松的实现半透明窗体。按照微软的要求,透明窗体窗体在创建时应使用WS_EX_LAYERED参数(用CreateWindowEx),或者在创建后设置该参数(用SetWindowLong),我选用后者。全部函数、常量声明如下:

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 '使窗体添加上新的样式WS_EX_LAYERED
  SetWindowLong hwnd, GWL_EXSTYLE, rtn '把新的样式赋给窗体
  SetLayeredWindowAttributes hwnd, 0, 200, LWA_ALPHA
End Sub
yefm 2003-06-26
  • 打赏
  • 举报
回复
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

  '具体可以使用的常量及其用法

  Const LWA_ALPHA=&H2 '表示把窗体设置成半透明样式
  Const LWA_COLORKEY=&H1 '表示不显示窗体中的透明色


  具体例子

  程序代码

  Module1

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


  Form1

Private Sub Form_Load()
Dim rtn As Long

  rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE) '取的窗口原先的样式
rtn = rtn Or WS_EX_LAYERED '使窗体添加上新的样式WS_EX_LAYERED
SetWindowLong Me.hWnd, GWL_EXSTYLE, rtn '把新的样式赋给窗体

  SetLayeredWindowAttributes me.hwnd, 0, 192, LWA_ALPHA

    '把窗体设置成半透明样式,第二个参数表示透明程度
    '取值范围0--255,为0时就是一个全透明的窗体了

  End Sub


  第二种使用方法

  SetLayeredWindowAttributes Me.hWnd, &H0, 0, LWA_COLORKEY
    '表明不显示窗体中的透明色
    '而第二个参数表示透明色为黑色,并且你可以用RGB函数来指定颜色值
叶帆 2003-06-26
  • 打赏
  • 举报
回复
【声明】
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的地方将变为透明

【其它】
'系统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
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1

'形状不规则的窗体
Private Sub Command1_Click()
Dim rtn As Long
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 Sub Command2_Click()
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
Text1 = VScroll1.Value
SetLayeredWindowAttributes hwnd, 0, 150, LWA_ALPHA
End Sub

PrettyMurphy 2003-06-25
  • 打赏
  • 举报
回复
2000/XP下有一个专门的API不但可以支持透明,而且可以支持半透明。巨爽。你查一下吧。这方面资料好多的。

对了,那个API在98下不能用。
gpo2002 2003-06-25
  • 打赏
  • 举报
回复
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
);


Return Values
If the function succeeds, the return value is nonzero.

Requirements
Windows NT/2000/XP: Included in Windows 2000 and later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
gpo2002 2003-06-25
  • 打赏
  • 举报
回复
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private 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 Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
gpo2002 2003-06-25
  • 打赏
  • 举报
回复
'设置窗体透明度(Win2000)
Public Sub SetTransparency(ByVal hwnd As Long, ByVal iiPercent As Integer)

Dim lReturn As Long

lReturn = GetWindowLong(hwnd, GWL_EXSTYLE)
lReturn = lReturn Or WS_EX_LAYERED

SetWindowLong hwnd, GWL_EXSTYLE, lReturn

If iiPercent >= 0 And iiPercent <= 100 Then
iiPercent = CInt((iiPercent / 100) * 255)
SetLayeredWindowAttributes hwnd, 0, iiPercent, LWA_ALPHA
End If

End Sub
yuxiao99999 2003-06-25
  • 打赏
  • 举报
回复
我在XP下用不行呀。
bydisplay 2003-06-25
  • 打赏
  • 举报
回复
在2000下就可以了,是API函数,
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Sub Command1_Click()
End
End Sub
'透明窗体
Private Sub Form_Load()
Dim hBitmap
Me.AutoRedraw = True
hBitmap = CreateCompatibleBitmap(Me.hdc, 0, 0)
SelectObject Me.hdc, hBitmap
Me.Refresh
End Sub


angeltoday 2003-06-25
  • 打赏
  • 举报
回复
到一些VB编程网站找个源码看看就知道了

1,486

社区成员

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

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