Click here to Skip to main content
15,881,281 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 340.8K   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

 
GeneralUm... Pin
Anonymous21-Oct-02 13:24
Anonymous21-Oct-02 13:24 
Generalit can't do anything for a subwindow(control) in a dialog Pin
Anonymous27-Sep-02 16:33
Anonymous27-Sep-02 16:33 
GeneralCan't Fade-Out doing similar Pin
Member 40239791-Aug-02 9:17
Member 40239791-Aug-02 9:17 
GeneralRe: Can't Fade-Out doing similar Pin
Anonymous11-Aug-02 17:11
Anonymous11-Aug-02 17:11 
QuestionHow about moved window? Pin
BtvnLue6-Nov-01 21:51
BtvnLue6-Nov-01 21:51 
GeneralNot quit 100% proof Pin
26-Oct-01 0:19
suss26-Oct-01 0:19 
QuestionUsing this in ActiveX controls? Pin
Daed9-Jul-01 23:52
Daed9-Jul-01 23:52 
QuestionWhy not simply use SetLayeredWindowAttributes? Pin
9-Jul-01 5:21
suss9-Jul-01 5:21 
This two-liner achieves the very same effect:

SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(GetSafeHwnd(), 0, 255 * alpha, LWA_ALPHA);


where alpha is between 0.0 and 1.0
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 
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 

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.