Use API functions to implement image fade-in and fade-out

Source: Internet
Author: User
Tags transparent image
Using API functions to achieve the image fade-in and fade-out effect <br/> In general, traditional implementation of the fade-in and fade-out effects between two pictureboxes requires a large number of API functions and complex color palette and <br/> device context). However, in WIN98 and win2000, Microsoft provides the alphablend function that supports transparent image copying. <Br/> This article describes how to use the API function alphablend to implement the fade-in and fade-out effects of Images Between pictureboxes. The alphablend function is defined in <br/> msimg32.dll. Generally, WIN98 and win2000 use this library. Before programming, you can check whether the file exists. <Br/> Open VB to create a new project. Select project | add module to add a module to the project, and enter the following code: </P> <p> Public type rblendprops <br/> tblendop as byte <br/> tblendoptions as byte <br/> tblendamount as byte <br/> talphatype as byte <br /> end type </P> <p> Public declare function alphablend lib "msimg32" (byval hdestdc as long, _ <br/> byval X as long, byval y as long, byval nwidth as long, _ <br/> byval nheight as long, byval hsrcdc as long, _ <br/> Val xsrc as long, byval ysrc as long, byval widthsrc as long, _ <br/> byval heightsrc as long, byval blendfunct as long) as Boolean </P> <p> Public declare sub copymemory lib "Kernel32" alias "rtlmovememory" _ <br/> (destination as any, source as any, byval length as long) </P> <p> as you can see, the definition of the alphablend function is similar to that of the normal copy function bitblt, but the final parameter blendfunct is defined as a <br/> rblendprops structure. So why is blendfunct defined as long in function definition? Because the rblendprops structure is 4 bytes long. <Br/> the length of long type variables is also 4 bytes, so we can copy an rblendprops structure to the blendfunct using the API function copymemory. <br/>. </P> <p> Add two picturebox controls in form1, where picture2 is the source and picture1 is the copy target, set scalemode to 3-pixel <br/>, set autoredraw attribute to true, and add images respectively. Add a timer control and a commandbutton control. <br/> In the form1 code window, add the following code: </P> <p> dim ltime as byte </P> <p> sub showtransparency (CSRC as picturebox, cdest as picturebox, _ <br/> byval nlevel as byte) <br/> dim lrprops as rblendprops <br/> dim lnblendptr as long </P> <p> cdest. CLS <br/> lrprops. tblendamount = nlevel <br/> copymemory lnblendptr, lrprops, 4 <br/> with CSRC <br/> alphablend cdest. HDC, 0, 0 ,. scalewidth ,. Scaleheight, _ <br/>. HDC, 0, 0 ,. scalewidth ,. scaleheight, lnblendptr <br/> end with <br/> cdest. refresh <br/> end sub </P> <p> private sub commandementclick () <br/> ltime = 0 <br/> timer1.interval = 100 <br/> timer1.enabled = true <br/> end sub </P> <p> private sub timer1_timer () <br/> ltime = ltime + 1 <br/> showtransparency picture2, picture1, ltime <br/> If ltime & gt ;= 255 then <br/> timer1.enabled = fa LSE <br/> end if <br/> me. caption = STR (INT (ltime/2.55) + "%" <br/> end sub </P> <p> run the program, click command1, you can see the fade-in and fade-out effect of the picture2 image copied to picture1. <Br/> in the structure of rblendprops, the most important parameter is tblendamount, which determines the transparent program between the source and the target. If the value is 0, the source is completely transparent. If the value is 255, the source completely overwrites the target. <Br/> In addition, The alphablend function is not only used for copying between two pictureboxes, but also for transparent copying between two device context, that is, <br/>, controls such as Windows can also be transparent. However, I found a problem in the programming process. I don't know if it is a bug of alphablend. After I finish writing the <br/> program, there is no transparent replication effect. I thought this function didn't work, but when I opened VB again to run the above program, everything was normal. <Br/> I have not found any related bug list on msdn. <Br/> Www.applevb.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.