在2000中用使用SetLayeredWindowAttributes可以很方便的实现窗口透明,可是有什么方法可以实现窗体上的图片透明或半透明呢?相当于将SetLayeredWindowAttributes用在picture上。

解决方案 »

  1.   


       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     '表示不显示窗体中的透明色 
     
        
      具体例子  程序代码  Module1Public 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函数来指定颜色值  
     
      

  2.   

    AlphaBlend
    [This is preliminary documentation and subject to change.] The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.AlphaBlend(
      HDC hdcDest,
      int nXOriginDest,
      int nYOriginDest,
      int nWidthDest,
      int hHeightDest,
      HDC hdcSrc,
      int nXOriginSrc,
      int nYOriginSrc,
      int nWidthSrc,
      int nHeightSrc,
      BLENDFUNCTION blendFunction
    );
     
    Parameter
    hdcDest 
    Handle to the destination device context. 
    nXOriginDest 
    Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle. 
    nYOriginDest 
    Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle. 
    nWidthDest 
    Specifies the width, in logical units, of the destination rectangle. 
    hHeightDset 
    Handle to the height, in logical units, of the destination rectangle. 
    hdcSrc 
    Handle to the source device context. 
    nXOriginSrc 
    Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle. 
    nYOriginSrc 
    Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle. 
    nWidthSrc 
    Specifies the width, in logical units, of the source rectangle. 
    nHeightSrc 
    Specifies the height, in logical units, of the source rectangle. 
    blendFunction 
    Specifies the alpha blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER. See the BLENDFUNCTION and EMRALPHABLEND structures. 
    Return Values
    If the function succeeds, the return value is TRUE.If the function fails, the return value is FALSE. Windows NT: To get extended error information, callGetLastError. Res
    If the source rectangle and destination rectangle are not the same size, the source bitmap is stretched to match the destination rectangle. If the SetStretchBltMode function is used, the iStretchMode value is BLACKONWHITE and WHITEONBLACK; the iStretchMode value is automatically converted to COLORONCOLOR for this function. The destination coordinates are transformed by using the transformation currently specified for the destination device context. The source coordinates are transformed by using the transformation currently specified for the source device context. An error occurs (and the function returns FALSE) if the source device context identifies an enhanced metafile device context. If destination and source bitmaps do not have the same color format, AlphaBlend converts the source bitmap to match the destination bitmap. AlphaBlend does not support mirroring. If either the width or height of the source or destination is negative, this call will fail. Note: The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparancy value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (indicates that the image is opaque) when you only want to use per-pixel alpha values. QuickInfo
      Windows NT: Requires version 5.0 or later.
      Windows: Requires Windows 98 or later.
      Windows CE: Unsupported.
      Header: Declared in wingdi.h.
      Import Library: Included as a resource in msimg32.dll.
      

  3.   

    利用API函数实现图像淡入淡出效果
        一般传统的实现两个PictureBox之间图像的淡入淡出效果都需要使用大量的API函数并进行复杂的调色板以及
    绘图设备(Device Context)的操作。但是在Win98、Win2000中,微软提供了支持透明图像拷贝的AlphaBlend函数。
    这篇文章就介绍如何通过API函数AlphaBlend实现PictureBox之间图像的淡入淡出效果。AlphaBlend函数的定义在
    msimg32.dll中,一般Win98、Win2000都带了这个库,在编程之前你可以先察看一下该文件是否存在。
        打开VB建立一个新工程。选择菜单 Project | Add Module 添加一个模块到工程中,在其中输入以下代码:Public Type rBlendProps
        tBlendOp As Byte
        tBlendOptions As Byte
        tBlendAmount As Byte
        tAlphaType As Byte
    End TypePublic Declare Function AlphaBlend Lib "msimg32" (ByVal hDestDC As Long, _
            ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
            ByVal nHeight As Long, ByVal hSrcDC As Long, _
            ByVal xSrc As Long, ByVal ySrc As Long, ByVal widthSrc As Long, _
            ByVal heightSrc As Long, ByVal blendFunct As Long) As BooleanPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
            (Destination As Any, Source As Any, ByVal Length As Long)    大家可以看到,AlphaBlend函数的定义同普通的复制函数Bitblt很相似,只是最后的参数blendFunct定义为一个
    rBlendProps结构。那么为什么在函数定义中blendFunct 定义为Long类型呢?因为rBlendProps结构长度是4个字节。
    而Long类型变量的长度也是4个字节,那么我们就可以程序中通过API函数CopyMemory将一个rBlendProps结构拷贝到
    blendFunct 中。    在Form1中添加两个PictureBox控件,其中Picture2为源,Picture1为拷贝目标,将两者的ScaleMode都设置为3-Pixel
    将两者的AutoRedraw属性都设置为True,然后分别添加图像。在加入一个Timer控件以及一个CommandButton控件,然后
    在Form1的代码窗口中添加如下代码:Dim lTime As ByteSub ShowTransparency(cSrc As PictureBox, cDest As PictureBox, _
        ByVal nLevel As Byte)
        Dim LrProps As rBlendProps
        Dim LnBlendPtr As Long
        
        cDest.Cls
        LrProps.tBlendAmount = nLevel
        CopyMemory LnBlendPtr, LrProps, 4
        With cSrc
            AlphaBlend cDest.hDC, 0, 0, .ScaleWidth, .ScaleHeight, _
                .hDC, 0, 0, .ScaleWidth, .ScaleHeight, LnBlendPtr
        End With
        cDest.Refresh
    End SubPrivate Sub Command1_Click()   
        lTime = 0
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Timer()
        lTime = lTime + 1
        ShowTransparency Picture2, Picture1, lTime
        If lTime >= 255 Then
            Timer1.Enabled = False
        End If
        Me.Caption = Str(Int(lTime / 2.55)) + "%"
    End Sub    运行程序,点击Command1,就可以看到Picture2图像拷贝到Picture1上的淡入淡出效果了。
        在结构rBlendProps中,最重要的参数就是tBlendAmount,该值决定了源与目标之间的透明程序。如果为0的话,源完全
    透明,如果为255的话,源完全覆盖目标。
        另外AlphaBlend 函数不只用于两个PictureBox之间的拷贝,而且可以在两个Device Context之间的透明拷贝,也就是
    说,象窗口等控件之间也可以实现透明效果。
      

  4.   

    可是我需要的是picturebox的透明或半透明阿,picturebox底下是窗口,有可能有好几个控件,也就是说并不知道的,怎么实现呢?
      

  5.   

    只有自己算了:)。
    这里有zyl910的一个alpha blend的例子,你可以参考一下。
    http://www.fantasiasoft.net/Zyl910/AlphaDemo.zip