读书人

怎么使得一张图片能够淡出淡入

发布时间: 2012-02-04 15:43:09 作者: rapoo

如何使得一张图片能够淡出淡入
我在网上搜得的代码是这样的,但是不能运行,大家给看看吧?
一般传统的实现两个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 Type

Public 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 Boolean

Public 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 Byte

Sub 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 Sub

Private 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



[解决办法]
图片复制成多张,依次用图片加工工具制作成渐淡效果,用timer控件显示多张图片就可以达到效果。
或者你用flash直接做一张这种效果的图片加载到VB控件更简单
[解决办法]
http://download.csdn.net/user/sun_zhangkui
你看看这上面的一个淡入淡出,可能有帮助
[解决办法]
很代码很好,我加了淡出效果


再添加两个picturebox控件(picture3, picture4),将两者的ScaleMode都设置为3- Pixel将两者的AutoRedraw属性都设置为True,picture3加入一蒙板图片(纯色图像,纯白或纯黑或纯灰,什么都行),picture4不加任何图片。添加一timer控件,command控件。当然这是在以上作者源代码的基础上做的,所以,以上源代码还得有
Private Sub Command2_Click()
lTime = 255
Timer2.Interval = 50
Timer2.Enabled = True
End Sub

Private Sub Form_Load()
Picture2.Visible = False
Picture3.Visible = False

End Sub

Private Sub Timer2_Timer()
lTime = lTime - 1
ShowTransparency Picture3, Picture2, lTime
If lTime < 1 Then
Timer2.Enabled = False
End If
Me.Caption = Str(Int((255 - lTime) / 2.55)) + "% "
Set Picture4.Picture = Picture2.Image
End Sub

读书人网 >VB

热点推荐