Use the API function to achieve image fade out

zhaozj2021-02-17  47

Use the API function to achieve image fade out

Generally, the fade out effect of the image between the two PictureBox images requires a large number of API functions and performs complex palettes and

Draw the operation of the DEVICE CONTEXT. However, in WIN98, WIN2000, Microsoft provides an Alphablend function that supports transparent image copies.

This article describes how to achieve the fade of image between the image between Picturebox through the API function alphablend. The definition of the alphablend function

In msimg32.dll, general Win98, win2000 has this library, before programming, you can see if the file exists.

Open VB to establish a new project. Select Menu Project | Add Module Add a module to the project where you enter the following code:

Public Type RblendProps

Tblendop as Byte

TblendOptions as byte

Tblendamount as Byte

Talphype 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)

As you can see, the definition of the Alphablend function is similar to the ordinary replication function Bitblt, but the last parameter BlendFunct is defined as one.

RBLENDPROPS structure. So why is BLENDFUNCT defined as a long type in a function definition? Because the RBLENDPROPS structure length is 4 bytes.

The length of the long type variable is also 4 bytes, then we can copy a RBLENDPROPS structure to the API function CopyMemory.

Blendfunct.

Add two PictureBox controls in Form1, where Picture2 is the source, and Picture1 is a copy target, set the SCALEMODE of both to 3-Pixel.

Set the autoreraw attributes of the two to TRUE and add images separately. Add a TIMER control and a CommandButton control, then

Add the following code in the Form1's code window:

Dim Ltime as Byte

Sub ShowTransparency (CSRC AS Picturebox, CDest As Picturebox, _

BYVAL NLVEL 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, LnblendPtrend with

CDest.refresh

End Sub

Private submmand1_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

Run the program, click Command1, you can see the fade of the Picture2 image to copy to Picture1.

In the structure RBLENDPROPS, the most important parameters are TBLENDAMOUNT, which determines the transparent program between the source and the target. If 0, the source is completely

Transparent, if it is 255, the source is fully covered.

In addition, the alphablend function is not only used in the copy between two Picturebox, and can be transparent between the two Device Context, that is

Said, transparent effects can also be achieved between controls such as window and other controls. However, I found a problem during the programming process. I don't know if it is alphablend's bug, that is, I am writing.

After the program, there is no effect of transparent replication. I don't work, but when I open the VB to run the above program, everything is normal.

I didn't find the relevant bug list on MSDN.

转载请注明原文地址:https://www.9cbs.com/read-29659.html

New Post(0)