CLayeredWindowHelperST v1.0 - A wrapper class for transparent windows

Sample Image

SoftechSoftware homepage
SoftechSoftware Email

Environment: VC++ 6.0, XP, Win2k, NT 4.0, Win9x/ME

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


Comments

  • Great stuff, but what about the win95/98/NT transparency? I know it can be done somehow...

    Posted by Legacy on 05/17/2003 12:00am

    Originally posted by: e-sushi

    Great Stuff!

    Too bad it doesn't show how to make 95/98 windows transparent, like in the good old shell-replacement coding days. (lstransparent.dll etc)

    It would be cool to integrate a more sophisticated thing into our project: http://shellwm.sourceforge.net

    /* In case you're interested on how to subclass one (or even all) window(s), go have a look over there. We handle toolwindows in most cases... but it would take too long to tell you here. Just look at the source, it's still in beta, but close to stable! */


    Reply
  • Problem in Captuing Screen ( with Layerwindow ) using Microsoft Encoder

    Posted by Legacy on 04/30/2003 12:00am

    Originally posted by: Svc

    Hi ,

    I found a strange problem.. Generally we can create a .WMV ( Window Media Format) file using Microsoft Encoder ( having MS Encoder 9.0) . But When I have a LayerWindow on the Screen and Capturing the Screen ( I dunnuo whts the problem with Encoding) and doing suppose drawing ( Althogh layerwidow is not transparent) . Its displaying Blank when playing the WMV file.

    Reply
  • What about Drawing on this Layered Window.???

    Posted by Legacy on 02/13/2003 12:00am

    Originally posted by: Tinkle

    Hi Davide.,

    It's a great piece code for Layered Window . ! Thanks for your effort .

    By the way., Is it possible to draw on a Layered Window.?

    Thanks again..

    Warm Regards,
    TINKLE.

    Reply
  • How can I set transparency for one image?

    Posted by Legacy on 07/09/2002 12:00am

    Originally posted by: Jianguo Wang

    As titled, do you know any codes to set transparency for one image and then add another one on top of it? Thanks very much!!!

    Reply
  • Released version 1.1

    Posted by Legacy on 02/15/2002 12:00am

    Originally posted by: Davide Calabro'

    It can be found on http://www.softechsoftware.it

    Reply
  • Removing the transparent effect ?

    Posted by Legacy on 02/13/2002 12:00am

    Originally posted by: Steeve H.

    I would like to know how to remove the transparent effect in run-time (not changing the transparency).

    Thx

    Reply
  • Changing window style of another appliction

    Posted by Legacy on 02/12/2002 12:00am

    Originally posted by: Sanjay

    Hi,
    Is it possible to change the windowstyle , of a window of another running application using only SendMessage ?
    Thanks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds