VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 半透明窗体(win2000特有API)
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:9245, 日期:2001-11-26
半透明窗体(win2000特有API)  

  Windows2000已经出了一年多了,就先介绍一下Windows2000特有的API吧!! AnimateWindow是一个窗口打开和关闭时产生动画效果的新函数,因为是一个新的函数, 所以在 API Viewer中是找不到的,必需自己定义:

   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函数来指定颜色值