Click here to Skip to main content
15,891,431 members
Articles / Desktop Programming / MFC

Using the AlphaBlend Function

Rate me:
Please Sign up or sign in to vote.
4.43/5 (19 votes)
1 Feb 2000 341.5K   7.4K   49   67
A sample application that uses the AlphaBlend function to produce a transparent splash screen

Sample Image - alphablend.gif

Note: The code in this article is restricted to win98 and win2000.

Do you want a cool splash window? Do you think the general splash window is not good enough for your app? Then this sample source is for you.

My semi-transparent splash window uses the AlphaBlend function. When I found this function, I was so surprised! The function works just like BitBlt for bitmaps that have transparent or semitransparent pixels. Here is the AlphaBlend function's syntax.

C++
BOOL AlphaBlend( HDC hdcDest,
  int nXOriginDest,
  int nYOriginDest,
  int nWidthDest,
  int nHeightDest,
  HDC hdcSrc,
  int nXOriginSrc,
  int nYOriginSrc,
  int nWidthSrc,
  int nHeightSrc,
  BLENDFUNCTION blendFunction
);

The important parameter is the last one, BLENDFUNCTION.

C++
typedef struct _BLENDFUNCTION {
    BYTE     BlendOp;
    BYTE     BlendFlags;
    BYTE     SourceConstantAlpha;
    BYTE     AlphaFormat;
}BLENDFUNCTION;
  • BlendOp must be AC_SRC_OVER
  • BlendFlags must be 0
  • SourceConstantAlpha must be between 0 (transparent) and 255 (semi-transparent)
  • AlphaFormat must be AC_SRC_ALPHA

MSDN states: The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparency 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.

See the sample application for a demonstration.

Just enjoy!!

Ajou University C.C. 4th member.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
Korea (Republic of) Korea (Republic of)
Woo Seok Seo have been a Microsoft MVP for 7 years and have translated several books into Korean. Author of C# Programming for Beginner (DevPress, 2001), he is interested in Debugging techniques and .NET technology. Get in touch with Woo Seok Seo at wooseok.seo@gmail.com

Comments and Discussions

 
QuestionWhy not simply use SetLayeredWindowAttributes? Pin
9-Jul-01 5:21
suss9-Jul-01 5:21 
AnswerRe: Why not simply use SetLayeredWindowAttributes? Pin
21-Jul-01 5:22
suss21-Jul-01 5:22 
GeneralRe: Why not simply use SetLayeredWindowAttributes? Pin
26-Oct-01 0:11
suss26-Oct-01 0:11 
GeneralTRUE color image Pin
1-May-01 22:15
suss1-May-01 22:15 
GeneralRe: TRUE color image Pin
9-Jul-01 5:18
suss9-Jul-01 5:18 
GeneralRe: TRUE color image Pin
21-Jul-01 5:18
suss21-Jul-01 5:18 
QuestionAny way to blend an entire form like win2000? Pin
jdd4828-Sep-00 12:47
jdd4828-Sep-00 12:47 
AnswerRe: Any way to blend an entire form like win2000? Pin
21-Jul-01 5:10
suss21-Jul-01 5:10 
There might be. It'd probably use a good amount of resources, but in theory, Windows would compensate with enough virtual memory to get by. Oh yeah, it'd also take some ingenuity and experimenting. Smile | :)

(By the way, I'm a VB programmer that happens to know a little bit about C, so if I'm confusing you by using VB terminology, let me know; I'd really like to learn better how to program in Visual C++ but haven't found a decent website or instructor yet...)

Here's what I think might work. First, create a standard form, and get rid of the standard Windows border, so you've just basically got a giant canvas. Next, on every resize or move event (for the form, that is), take a screenshot of the area behind the form (I'm not quite sure exactly how you'd go about taking a screenshot of what's *behind* you're form, but that's why I said it'd take some experimenting). Next, take a screenshot of your form at full opacity (again, how you'd do this without actually setting the opacity and taking the screenshot, I don't know--you'd want to hide it from the user; perhaps there's a way to do all this graphics management in a memory buffer?), and blend the two of them, passing the hDC of your borderless "canvas" to the hdcDest parameter of the AlphaBlend function.

Like I said, it'd take a *lot* of ingenuity and experimenting, but if you _really_ want to blend a form, you may want to look in to how you might go about this; it's the only thing I can think of, anyways.

Hope it helps!
Eric (eslindsey@hotmail.com)
QuestionCode not using MFC? Pin
Jon Boyce31-Aug-00 0:05
Jon Boyce31-Aug-00 0:05 
GeneralWorks on WinNT 4.0 Pin
Torsten17-Jul-00 3:12
Torsten17-Jul-00 3:12 
GeneralProblem with GDI32 Pin
sebastien16-May-00 0:07
sebastien16-May-00 0:07 
GeneralRe: Problem with GDI32 Pin
Smile Seo4-Jul-00 8:13
sussSmile Seo4-Jul-00 8:13 
GeneralAnswer: How to run it in any Windows. Pin
Yury Goltsman28-Mar-00 2:40
Yury Goltsman28-Mar-00 2:40 
GeneralRe: Answer: How to run it in any Windows. Pin
Error Coad11-Jul-00 7:03
sussError Coad11-Jul-00 7:03 
GeneralRe: Answer: How to run it in any Windows. Pin
Yury Goltsman11-Jul-00 22:00
Yury Goltsman11-Jul-00 22:00 
GeneralHere's another possibility Pin
21-Jul-01 5:00
suss21-Jul-01 5:00 
GeneralRe: Here's another possibility Pin
Henry P.3-Mar-03 6:03
Henry P.3-Mar-03 6:03 
GeneralWin95 Pin
Alex Butler17-Mar-00 9:14
Alex Butler17-Mar-00 9:14 
GeneralRe: Win95 Pin
21-Jul-01 4:57
suss21-Jul-01 4:57 
QuestionDoes that mean a project w/ this code in it cannot run under 95/NT ? Pin
Christian4-Mar-00 3:02
Christian4-Mar-00 3:02 
AnswerIt might, it depends... Pin
21-Jul-01 4:53
suss21-Jul-01 4:53 
GeneralRe: It might, it depends... Pin
26-Oct-01 0:14
suss26-Oct-01 0:14 
GeneralThat's noted!! Pin
Smile Seo12-Feb-00 23:42
sussSmile Seo12-Feb-00 23:42 
GeneralRe: That's noted!! Pin
Keith Rule13-Feb-00 10:27
professionalKeith Rule13-Feb-00 10:27 
GeneralThis Technique only works on Win98 & Win2k Pin
Keith Rule11-Feb-00 7:37
professionalKeith Rule11-Feb-00 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.