Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / WTL
Article

How to make a transparent and click-through window

Rate me:
Please Sign up or sign in to vote.
4.42/5 (20 votes)
25 Nov 2005CPOL2 min read 141K   3.3K   56   16
A WTL class that will make any window transparent and allow mouse clicks to pass through the window in Windows 2000 and XP.

Introduction

In the December 2005 column of the MSDN magazine Paul DiLascia explained how to create a so called "layered" window which is transparent and passes mouse events through itself. It turned out an easy thing to do and I wrote a WTL class that I called (surprisingly) CLayered.

One remark though - code presented here will work only on Windows 2000 and higher.

To create a visible-through (layered) window, we need to set the extended style WS_EX_LAYERED, and to make the window clicked-through, we need the WS_EX_TRANSPARENT extended style. This is done with the SetWindowLong() API function or the ModifyStyleEx() function of the CWindow class. Then, we can use the SetLayeredWindowAttributes() API function to set the opacity and transparency of a layered window. To reverse the transparency, remove the WS_EX_LAYERED and WS_EX_TRANSPARENT styles.

But let me warn you that setting the WS_EX_TRANSPARENT attribute affects the entire window: the user can't close the window using the 'x' button, select it with the mouse, or select any controls on the window. The application can still close the window programmatically. Microsoft recommends using the WS_EX_TRANSPARENT style for modal windows that have a short life.

Using the code

  1. Derive your class from CLayered, like so:
    class CMainDlg : public CDialogImpl<CMainDlg>, 
                    public CUpdateUI<CMainDlg>,
                    public CMessageFilter, 
                    public CIdleHandler,
                    CLayered<CMainDlg>
  2. Then somewhere in your code, modify your window style:
    // To make a visible-through window
    BOOL SetLayered()

    or

    // To make a clicked-through window
    BOOL SetClickThru()
  3. And set the transparency of the window. If necessary, call:
    BOOL MakeTransparent(int nOpacity)

    where nOpacity describes the opacity of the layered window. 0 means the window is completely transparent, 255 means the window is opaque.

Software requirements

  • Minimum operating system required - Windows 2000.
  • Demo project was created with Visual Studio 2005, but it does not contain any VS2005 specific features so it would not be a problem to re-create it on a different version of the Visual Studio - just create an empty WTL project and add all the source files.
  • Demo project was tested on Windows XP Pro SP2.

History

  • November 20, 2005 - initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Bug[My vote of 1] Using VS2010, its not working Pin
cdebel18-Jul-11 8:10
cdebel18-Jul-11 8:10 
GeneralRe: [My vote of 1] Using VS2010, its not working Pin
Igor Vigdorchik18-Jul-11 12:50
Igor Vigdorchik18-Jul-11 12:50 
GeneralMy vote of 1 Pin
Thomas W25-Jun-09 1:55
Thomas W25-Jun-09 1:55 
Generalproblems with WS_EX_LAYERED Pin
doctorrie15-Feb-07 16:47
doctorrie15-Feb-07 16:47 
GeneralRe: problems with WS_EX_LAYERED Pin
Igor Vigdorchik15-Feb-07 17:04
Igor Vigdorchik15-Feb-07 17:04 
Generalfound fix - thank you for clues Pin
doctorrie15-Feb-07 19:21
doctorrie15-Feb-07 19:21 
QuestionBroken link?! Pin
Hansa4Ever2-Dec-05 4:58
Hansa4Ever2-Dec-05 4:58 
AnswerRe: Broken link?! Pin
Igor Vigdorchik2-Dec-05 16:33
Igor Vigdorchik2-Dec-05 16:33 
Works for me.
AnswerRe: Broken link?! Pin
Hansa4Ever2-Dec-05 22:25
Hansa4Ever2-Dec-05 22:25 
QuestionWhy? Pin
Neville Franks25-Nov-05 16:18
Neville Franks25-Nov-05 16:18 
AnswerRe: Why? Pin
Igor Vigdorchik25-Nov-05 17:41
Igor Vigdorchik25-Nov-05 17:41 
GeneralRe: Why? Pin
Averk29-Nov-05 20:19
Averk29-Nov-05 20:19 
GeneralRe: Why? Pin
Igor Vigdorchik30-Nov-05 7:35
Igor Vigdorchik30-Nov-05 7:35 
GeneralRe: Why? Pin
Pablo Aliskevicius30-Nov-05 19:05
Pablo Aliskevicius30-Nov-05 19:05 
GeneralRe: Why? Pin
Igor Vigdorchik1-Dec-05 13:11
Igor Vigdorchik1-Dec-05 13:11 
AnswerRe: Why? Pin
Adm.Wiggin1-Dec-05 13:23
Adm.Wiggin1-Dec-05 13:23 

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.