Click here to Skip to main content
15,861,125 members
Articles / Desktop Programming / MFC
Article

CLayeredWindowHelperST v1.0

Rate me:
Please Sign up or sign in to vote.
4.64/5 (9 votes)
17 Feb 20021 min read 93.5K   2.9K   37   10
A wrapper class for transparent windows

Sample Image - CLayeredWindowHelperST.gif

Abstract

CLayeredWindowHelperST
is a wrapper class of all required APIs to add support for transparent (layered)
windows to your applications. This is a scalable class. If running under Windows 2000/XP your windows
will have the selected transparent effect, while if running under Windows 9x/ME/NT will run error-less.

How to integrate CLayeredWindowHelperST in your application

In your project include the following files:

  • LayeredWindowHelperST.h
  • LayeredWindowHelperST.cpp
Create a instance of CLayeredWindowHelperST. This class encapsulates all the required APIs to support
transparent windows and to run error-less if running under a old Windows version that doesn't support this feature.
You need only one instance, so this can be global to the application.
CLayeredWindowHelperST G_Layered;
Modify the style of your window, then set the transparency effect. For dialog-based applications, in your OnInitDialog:
// Call the base-class method
CDialog::OnInitDialog();

// Modify the style
G_Layered.AddLayeredStyle(m_hWnd);

// Set the trasparency effect to 70%
G_Layered.SetTransparentPercentage(m_hWnd, 70);
  -or-
G_Layered.SetLayeredWindowAttributes(m_hWnd, 0, 210, LWA_ALPHA);

Class methods

AddLayeredStyle
Adds the WS_EX_LAYERED style to the specified window.
This style is required to have a transparent effect.

// Parameters:
//     [IN]   Handle to the window and, indirectly, the class to which the window belongs. 
//            Windows 95/98/Me: The AddLayeredStyle function may fail if the window 
//            specified by the hWnd parameter does not belong to the same process 
//            as the calling thread. 
//
// Return value:
//     Non zero
//            Function executed successfully.
//     Zero
//            Function failed. To get extended error information, call ::GetLastError().
//
LONG AddLayeredStyle(HWND hWnd)
SetLayeredWindowAttributes
Sets the opacity and transparency color key of a transparent (layered) window.
// Parameters:
//     [IN]   hWnd
//            Handle to the layered window.
//     [IN]   crKey
//            A COLORREF value that specifies the transparency color key to be used when
//            composing the layered window. All pixels painted by the window in this color will be transparent.
//            To generate a COLORREF, use the RGB() macro.
//     [IN]   bAlpha
//            Alpha value used to describe the opacity of the layered window.
//            When bAlpha is 0, the window is completely transparent.
//            When bAlpha is 255, the window is opaque. 
//     [IN]   dwFlags 
//            Specifies an action to take. This parameter can be one or more of the following values:
//              LWA_COLORKEY  Use crKey as the transparency color.  
//              LWA_ALPHA     Use bAlpha to determine the opacity of the layered window.
//
// Return value:
//     TRUE
//            Function executed successfully.
//     FALSE
//            Function failed. To get extended error information, call ::GetLastError().
//
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
SetTransparentPercentage
Sets the percentage of opacity or transparency of a layered window.
// Parameters:
//     [IN]   hWnd
//            Handle to the layered window.
//     [IN]   byPercentage
//            Percentage (from 0 to 100)
//
// Return value:
//     Non zero
//            Function executed successfully.
//     Zero
//            Function failed. To get extended error information, call ::GetLastError().
//
BOOL SetTransparentPercentage(HWND hWnd, BYTE byPercentage)
GetVersionI
Returns the class version as a short value.
// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()
GetVersionC
Returns the class version as a string value.
// Return value:
//     Pointer to a null-terminated string containing the class version.
//
static LPCTSTR GetVersionC()

Remarks

To compile CLayeredWindowHelperST you need the Platform SDK (August 2001 or newer). This is not
mandatory because at compile-time, if not found, it will be emulated. At run-time you don't need any
additional SDK or libraries installed.

History

  • v1.0 (17/January/2002)
    First release

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

License

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

Comments and Discussions

 
GeneralNice article. Pin
LittleTiger18-Nov-07 15:55
LittleTiger18-Nov-07 15:55 
Thanks a lot.Smile | :)
QuestionCan have 100% opaque window text while 70% transparent window background? [modified] Pin
Alexandru Matei30-May-06 23:25
Alexandru Matei30-May-06 23:25 
General256 colors in Win2000 is not transparent Pin
Shanhaz12-Mar-03 0:01
Shanhaz12-Mar-03 0:01 
GeneralReleased version 1.1 Pin
Davide Calabro18-Feb-02 20:27
Davide Calabro18-Feb-02 20:27 
GeneralRe: Released version 1.1 Pin
Atlantys22-May-02 21:54
Atlantys22-May-02 21:54 
GeneralWin2000 supports GetLayeredWindowAttributes Pin
Davide Calabro22-May-02 22:10
Davide Calabro22-May-02 22:10 
GeneralRe: Win2000 supports GetLayeredWindowAttributes Pin
Weiye Chen5-Aug-02 17:56
Weiye Chen5-Aug-02 17:56 
GeneralRe: Win2000 supports GetLayeredWindowAttributes Pin
Davide Calabro5-Aug-02 21:49
Davide Calabro5-Aug-02 21:49 
GeneralRe: Win2000 supports GetLayeredWindowAttributes Pin
Anonymous28-Oct-02 7:48
Anonymous28-Oct-02 7:48 
GeneralRe: Released version 1.1 Pin
Matthew Randall21-Dec-02 17:24
Matthew Randall21-Dec-02 17:24 

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.