Window Alpha-Channel Transparency

By: mykle hoban

Abstract: How to use new features of the Windows 2000 SDK to set the transparency (alpha-channel) level of your form

Question

How do I use the window transparency (alpha channel) feature that is part of the Windows 2000 SDK?

Answer

Transparency is a fun and neat thing to do to windows to spice up your application a little bit. Before Win2k, it was a hassle to even attempt to set some sort of alpha-channeling in windows, but now that feature is included with the SDK. You can do it using what is called Layered Windows. The function that we use which is new to the Win2k SDK is SetLayeredWindowAttributes, it is defined on MSDN as:

BOOL SetLayeredWindowAttributes(
   HWND hwnd,           // handle to the layered window
   COLORREF crKey,      // specifies the color key
   BYTE bAlpha,         // value for the blend function
   DWORD dwFlags        // action
);
We do not need to worry about the crKey argument, only hwnd, dwFlags, and bAlpha. bAlpha is a value between 0 and 255, 0 being completely transparent, and 255 being completely opaque. In our example, we will use a slider bar to set the Alpha level. Note: This function only works with layered windows, so we must use SetWindowLong to add the WS_EX_LAYERED style to our form.


trans_unit.h
//---------------------------------------------------------------------------
#ifndef trans_unitH
#define trans_unitH
//---------------------------------------------------------------------------
#include <winuser.h>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TTrackBar *TrackBar1;
  TButton *Button1;
  void __fastcall TrackBar1Change(TObject *Sender);
private:	// User declarations
  int alpha;
  void __fastcall SetAlpha(int alph);
public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
  __property int Alpha = {read=alpha,write=SetAlpha};
  //this property makes setting transparency easier
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


trans_unit.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "trans_unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  SetWindowLong(Handle,GWL_EXSTYLE,  //make our form a layered window
          GetWindowLong(Handle,GWL_EXSTYLE)|WS_EX_LAYERED);
  Alpha = 255;  //make sure our form starts out opaque
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
  Alpha = TrackBar1->Position;  //set the transparency according to the slider
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetAlpha(int alph)
{
  alpha = alph;
  SetLayeredWindowAttributes(Handle,NULL,alpha,LWA_ALPHA);
  //set the alpha level of our form
}


Server Response from: ETNASC03