In this article we will see how to use new AlphaBlend API which is only available in win 2k and later , Win 98 and later. In this example we have function called DoAlphablend and this function takes 3 arguments : source picturebox, destination picture box and AlphaVal. Alphavalue determine the transparancy of image.
Step-By-Step Example
- Create a standard exe project - Add 2 picture box and 2 timers on the form1 - Assign picture property of both picture box and make sure both imagess are different. - Copy and Paste the following code into form1 code window. |
Click here to copy the following block | Const AC_SRC_OVER = &H0
Private Type BLENDFUNCTION BlendOp As Byte BlendFlags As Byte SourceConstantAlpha As Byte AlphaFormat As Byte End Type
Private Declare Function AlphaBlend Lib "msimg32.dll" _ (ByVal hdc As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal hdc As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal lInt As Long, _ ByVal BLENDFUNCT As Long) As Long Private Declare Sub RtlMoveMemory Lib "kernel32.dll" _ (Destination As Any, _ Source As Any, _ ByVal Length As Long)
Dim BlendVal As Integer Dim BF As BLENDFUNCTION, lBF As Long
Dim tempPic1 As New StdPicture, tempPic2 As New StdPicture
Private Sub Form_Load() Picture1.AutoRedraw = True Picture2.AutoRedraw = True Picture1.ScaleMode = vbPixels Picture2.ScaleMode = vbPixels BlendVal = 1 Set tempPic1 = Picture1.Picture Set tempPic2 = Picture2.Picture Timer1.Interval = 200 Timer2.Interval = 200 Timer1.Enabled = True Timer2.Enabled = False End Sub
Public Sub DoAlphablend(SrcPicBox As PictureBox, DestPicBox As PictureBox, AlphaVal As Integer) With BF .BlendOp = AC_SRC_OVER .BlendFlags = 0 .SourceConstantAlpha = AlphaVal .AlphaFormat = 0 End With RtlMoveMemory lBF, BF, 4 AlphaBlend DestPicBox.hdc, 0, 0, DestPicBox.ScaleWidth, DestPicBox.ScaleHeight, SrcPicBox.hdc, 0, 0, SrcPicBox.ScaleWidth, SrcPicBox.ScaleHeight, lBF End Sub Private Sub Timer1_Timer() Picture1.Refresh Picture2.Refresh
BlendVal = BlendVal + 5 If BlendVal >= 155 Then flag = True Timer1.Enabled = False Picture2.Picture = tempPic1 Timer2.Enabled = True BlendVal = 1 End If DoAlphablend Picture2, Picture1, BlendVal
Me.Caption = CStr(BlendVal) End Sub Private Sub Timer2_Timer() Picture1.Refresh Picture2.Refresh
BlendVal = BlendVal + 5 If BlendVal >= 155 Then BlendVal = 1 Timer1.Enabled = True Timer2.Enabled = False Picture2.Picture = tempPic2 End If DoAlphablend Picture2, Picture1, BlendVal
Me.Caption = CStr(BlendVal) End Sub |
- Now press F5 to run the project - Observe that how image becomes semi-transperent. |
|