Click here to Skip to main content
15,885,216 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

 
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 
Great Article, you got my 5. I've been toying around with this a bit and I'd like to build an ActiveX control that is alphablended against it's owner.
I've tried setting the WS_EX_LAYERED style for the control's window and using UpdateLayeredWindow (which returns the same error it returns if you haven't set WS_EX_LAYERED) and I've tried building a windowsless control and using alphablends in OnDraw(), neither have worked. Does anyone know how to do this or have ideas on what else to try?

Eric Hansen

ehansen@pmsi.cc
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 
GeneralRe: Unicode compilation Pin
Groda25-Apr-03 7:45
Groda25-Apr-03 7:45 
GeneralControls on the Dialog Pin
azmodius30-Aug-02 20:14
azmodius30-Aug-02 20:14 
GeneralRe: Controls on the Dialog Pin
hbb198127-Sep-03 23:15
hbb198127-Sep-03 23:15 
GeneralRe: Controls on the Dialog Pin
Rui Lopes28-Sep-03 3:41
Rui Lopes28-Sep-03 3:41 
GeneralPCX in 2 bit Pin
nileshwk8-Aug-02 5:11
nileshwk8-Aug-02 5:11 
GeneralRe: PCX in 2 bit Pin
Rui Lopes23-Aug-02 14:53
Rui Lopes23-Aug-02 14:53 
GeneralPremultiply Pin
Sibilant22-Jul-02 20:21
Sibilant22-Jul-02 20:21 
GeneralRe: Premultiply Pin
Rui Lopes23-Jul-02 0:18
Rui Lopes23-Jul-02 0:18 
GeneralRe: Premultiply Pin
Sibilant23-Jul-02 6:05
Sibilant23-Jul-02 6:05 
QuestionWin98/ME..? Pin
Mario M.15-Feb-02 6:28
Mario M.15-Feb-02 6:28 
AnswerRe: Win98/ME..? Pin
Rui Lopes15-Feb-02 9:02
Rui Lopes15-Feb-02 9:02 
GeneralPlatfrom SDK problem Pin
Janssens Johan10-Oct-01 13:47
Janssens Johan10-Oct-01 13:47 
GeneralRe: Platfrom SDK problem Pin
20-Jun-02 23:20
suss20-Jun-02 23:20 

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.