Click here to Skip to main content
15,884,739 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.8K   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

 
AnswerRe: Why not add PNG support? Pin
Rui Lopes4-Apr-01 0:26
Rui Lopes4-Apr-01 0:26 
GeneralRe: Why not add PNG support? Pin
gpmn25-Apr-03 18:37
gpmn25-Apr-03 18:37 
GeneralRe: Why not add PNG support? Pin
gpmn25-Apr-03 18:48
gpmn25-Apr-03 18:48 
GeneralDowngraded the PSP sample image Pin
Rui Lopes3-Apr-01 1:00
Rui Lopes3-Apr-01 1:00 
GeneralRe: Downgraded the PSP sample image Pin
Maximilian Hänel4-Apr-01 8:27
Maximilian Hänel4-Apr-01 8:27 
Generalcan't open psp files Pin
Maximilian Hänel1-Apr-01 9:08
Maximilian Hänel1-Apr-01 9:08 
GeneralRe: can't open psp files Pin
l a u r e n1-Apr-01 10:17
l a u r e n1-Apr-01 10:17 
GeneralRe: can't open psp files Pin
Maximilian Hänel1-Apr-01 10:47
Maximilian Hänel1-Apr-01 10:47 
GeneralRe: can't open psp files Pin
Rui Lopes1-Apr-01 22:57
Rui Lopes1-Apr-01 22:57 
QuestionVery cool, but...PSP? Pin
1-Apr-01 6:28
suss1-Apr-01 6:28 
AnswerRe: Very cool, but...PSP? Pin
Rui Lopes1-Apr-01 23:06
Rui Lopes1-Apr-01 23:06 
GeneralRe: Very cool, but...PSP? Pin
2-Apr-01 12:13
suss2-Apr-01 12:13 
GeneralRe: Very cool, but...PSP? Pin
Rui Lopes4-Apr-01 2:28
Rui Lopes4-Apr-01 2:28 
GeneralRe: Very cool, but...PSP? Pin
21-Apr-01 8:40
suss21-Apr-01 8:40 
GeneralRe: Very cool, but...PSP? Pin
3-Apr-01 20:48
suss3-Apr-01 20:48 
GeneralRe: Very cool, but...PSP? Pin
4-Apr-01 1:52
suss4-Apr-01 1:52 
GeneralA couple of questions.... Pin
Christian Graus31-Mar-01 12:45
protectorChristian Graus31-Mar-01 12:45 
GeneralRe: A couple of questions.... Pin
1-Apr-01 14:53
suss1-Apr-01 14:53 
GeneralRe: A couple of questions.... Pin
Rui Lopes1-Apr-01 23:37
Rui Lopes1-Apr-01 23:37 
GeneralRe: A couple of questions.... Pin
Christian Graus2-Apr-01 2:15
protectorChristian Graus2-Apr-01 2:15 
GeneralRe: A couple of questions.... Pin
Rui Lopes2-Apr-01 9:35
Rui Lopes2-Apr-01 9:35 
GeneralRe: A couple of questions.... Pin
Christian Graus2-Apr-01 12:59
protectorChristian Graus2-Apr-01 12:59 
GeneralPSP reading Pin
Chris Losinger31-Mar-01 9:17
professionalChris Losinger31-Mar-01 9:17 
GeneralRe: PSP reading Pin
Rui Lopes31-Mar-01 9:36
Rui Lopes31-Mar-01 9:36 

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.