Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC

Per Pixel Alpha Blend

Rate me:
Please Sign up or sign in to vote.
4.73/5 (25 votes)
20 Apr 20012 min read 473.7K   15.2K   120   99
Demonstrates how to create per pixel alpha blending windows

Introduction

In this article, I will give you some source code to create per pixel alpha blending windows.

First, some images:

Nice, isn't it?

For creating a per-pixel window, we are going to use:

  • One 24bit Bitmap with alpha channel
  • The GDI function, UpdateLayeredWindow

24bit Bitmap with Alpha Channel

The most tricky part of creating the bitmap is finding a simple file format that has an embedded alpha channel.

The selected file format for the first incarnation of this article was PSP (Paint Shop Pro™ from Jasc®), but now the recommended file format has been changed to PNG (Portable Network Graphics).

For loading PNG files, I use the paintlib library. This library can also load files from PCX, TGA and TIFF formats.

This demo application does not use paintlib directly; instead it uses a paintlib wrapper DLL, ImgDecoder.Dll.

For loading PSP files, I've created a function called LoadPSPFileToDIB (PSPLoader.h/cpp).

UpdateLayeredWindow

To use this function, we must have a top-level window (in this sample, this is implemented by the class CDummyDialog) with the extended style WS_EX_LAYERED set.

Before we can pass the bitmap to the UpdateLayeredWindow function, we must pre-multiply the rgb channels with the alpha channel. For this, you just have to call the PreMultiplyRGBChannels function (take a look at CPerpixelalphaDlg::DoLoadFile).

From here on, read the source code in CPerpixelalphaDlg::DoUpdateDummyDialog.

Installing & Building

  1. Make sure you have the SDK for Windows® NT 5.0.
  2. Download and extract the source code of this article.
  3. Build and enjoy!

If you want to build the ImgDecoder.Dll, you also have to:

  1. Download paintlib and all the required libraries
  2. Download the ImgDecoder project
  3. Extract the ImgDecoder project to the paintlib library
  4. Build the ImgDecoder project
  5. Copy the resulting ImgDecoder.dll and ImgDecoder.lib to the bin directory of this article's demo application.

License

  1. You must mention my name in your application documentation/help box.
  2. I would appreciate it if you message me on the article forum letting me know that you are using this code.
  3. I cannot be held responsible for any kind of data loss caused directly or indirectly by the use of this code - use it at your own risk.
  4. You cannot sell this code - this is free software.

Don't forget if you have any constructive thoughts, please leave a comment below!

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
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Don't use this! Pin
Thomas Abegglen4-Mar-06 7:08
Thomas Abegglen4-Mar-06 7:08 
GeneralRe: Don't use this! Pin
Rui Lopes4-Mar-06 13:47
Rui Lopes4-Mar-06 13:47 
GeneralRe: Don't use this! Pin
OJ6-Apr-06 4:03
OJ6-Apr-06 4:03 
GeneralRe: Don't use this! Pin
Jiunnyih18-Jul-06 19:04
Jiunnyih18-Jul-06 19:04 
GeneralRe: Don't use this!(Read this if you read the criticism) Pin
John Crenshaw5-Jan-07 6:12
John Crenshaw5-Jan-07 6:12 
GeneralUpdateLayerdWindow not recognised Pin
ananasgroup24-Mar-05 12:37
ananasgroup24-Mar-05 12:37 
GeneralRe: UpdateLayerdWindow not recognised Pin
jongallegraud13-Apr-05 22:23
jongallegraud13-Apr-05 22:23 
GeneralRe: UpdateLayerdWindow not recognised Pin
ananasgroup14-Apr-05 3:29
ananasgroup14-Apr-05 3:29 
Worked for me as well.

Thanks !
Generalplease help Pin
v_ivan12-Dec-04 23:52
v_ivan12-Dec-04 23:52 
GeneralUpdateLayeredWindow Pin
v_ivan27-Aug-04 0:31
v_ivan27-Aug-04 0:31 
GeneralRe: UpdateLayeredWindow Pin
Rui Lopes27-Aug-04 0:48
Rui Lopes27-Aug-04 0:48 
GeneralRe: UpdateLayeredWindow Pin
v_ivan27-Aug-04 1:09
v_ivan27-Aug-04 1:09 
GeneralRe: UpdateLayeredWindow Pin
Rui Lopes27-Aug-04 1:22
Rui Lopes27-Aug-04 1:22 
GeneralRe: UpdateLayeredWindow Pin
v_ivan27-Aug-04 1:55
v_ivan27-Aug-04 1:55 
GeneralRe: UpdateLayeredWindow Pin
Rui Lopes27-Aug-04 2:12
Rui Lopes27-Aug-04 2:12 
GeneralLots of images Pin
artombo14-Jul-04 15:43
artombo14-Jul-04 15:43 
GeneralRe: Lots of images Pin
Rui Lopes15-Jul-04 3:44
Rui Lopes15-Jul-04 3:44 
GeneralRe: Lots of images Pin
artombo15-Jul-04 3:55
artombo15-Jul-04 3:55 
GeneralDo not see any Alpha Blending. Pin
WREY7-Oct-03 10:23
WREY7-Oct-03 10:23 
GeneralPer-Pixel Alphablended ActiveX Controls Pin
Eric Hansen2-Oct-03 14:01
Eric Hansen2-Oct-03 14:01 
GeneralAlpha Blended ActiveX Controls Pin
Eric Hansen2-Oct-03 14:00
Eric Hansen2-Oct-03 14:00 
GeneralHelp - 'AC_SRC_ALPHA' : undeclared identifier Pin
DataDuck18-Jun-03 6:00
DataDuck18-Jun-03 6:00 
GeneralRe: Help - 'AC_SRC_ALPHA' : undeclared identifier Pin
Rui Lopes18-Jun-03 9:47
Rui Lopes18-Jun-03 9:47 
GeneralRe: Help - 'AC_SRC_ALPHA' : undeclared identifier Pin
Anonymous15-Jun-04 3:24
Anonymous15-Jun-04 3:24 
GeneralUnicode compilation Pin
Amit Sindhwani31-Oct-02 3:46
sussAmit Sindhwani31-Oct-02 3:46 

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.