Click here to Skip to main content
15,880,469 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.3K   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

 
QuestionHow can i do this in VB.NET ??? Pin
dj.3agl314-Jun-14 8:29
dj.3agl314-Jun-14 8:29 
I need something just like this, but in VB.NET!
Can someone help me please!
I've been searching for a code like this in VB.Net for a few days, and all projects that i found didn't worked...
Please help me... Frown | :(
GeneralMy vote of 5 Pin
xueweizhong24-May-13 0:03
xueweizhong24-May-13 0:03 
Generalbeginner code... Pin
kilt12-Oct-09 5:39
kilt12-Oct-09 5:39 
GeneralRe: beginner code... Pin
Apuhjee23-Oct-09 4:24
Apuhjee23-Oct-09 4:24 
Generalgreat tutorial ! some questions about this still Pin
nirishere5-Sep-09 0:21
nirishere5-Sep-09 0:21 
QuestionHow about clean WinAPI? Pin
flaviusv12-Oct-08 10:16
flaviusv12-Oct-08 10:16 
AnswerRe: How about clean WinAPI? Pin
David_LoveCpp13-May-10 16:22
David_LoveCpp13-May-10 16:22 
QuestionHow can I draw text on the alpha window? Pin
Greg Ellis24-Aug-08 10:04
Greg Ellis24-Aug-08 10:04 
AnswerRe: How can I draw text on the alpha window? Pin
Greg Ellis27-Aug-08 5:41
Greg Ellis27-Aug-08 5:41 
QuestionHow to use control in Layered windows Pin
pherosiden29-Oct-07 18:29
pherosiden29-Oct-07 18:29 
QuestionUrgent (UNICODE) ! Pin
Mutant X1233-Sep-07 20:29
Mutant X1233-Sep-07 20:29 
GeneralTrouble with headers Pin
Delfistyaosani28-Jun-07 20:52
Delfistyaosani28-Jun-07 20:52 
GeneralRe: Trouble with headers Pin
kr.kim10-May-09 21:35
kr.kim10-May-09 21:35 
GeneralRe: Trouble with headers Pin
Delfistyaosani11-May-09 3:29
Delfistyaosani11-May-09 3:29 
GeneralRe: Trouble with headers Pin
kr.kim11-May-09 7:29
kr.kim11-May-09 7:29 
Generalundeclared identifiers Pin
DV6413-Apr-07 8:09
DV6413-Apr-07 8:09 
GeneralMore Test Images Pin
ninadpachpute12-Feb-07 22:05
ninadpachpute12-Feb-07 22:05 
JokeGR8 STUFF!!! Pin
DieZeL25-Jul-06 23:09
DieZeL25-Jul-06 23:09 
QuestionHow to add control to the dialog? Pin
Kingle Zhuang20-Jul-06 15:47
Kingle Zhuang20-Jul-06 15:47 
GeneralEfficency of layered windows Pin
jens pedersen9-Jun-06 3:40
jens pedersen9-Jun-06 3:40 
QuestionWriting Text Pin
MrBlob31-May-06 14:29
MrBlob31-May-06 14:29 
AnswerRe: Writing Text Pin
MacGadger28-Mar-07 2:02
MacGadger28-Mar-07 2:02 
GeneralRe: Writing Text Pin
Greg Ellis27-Aug-08 9:43
Greg Ellis27-Aug-08 9:43 
QuestionUpdateLayeredWindow Pin
toto777toto9-Apr-06 16:14
toto777toto9-Apr-06 16:14 
AnswerRe: UpdateLayeredWindow Pin
amit dedha28-Sep-06 6:42
amit dedha28-Sep-06 6:42 

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.