Click here to Skip to main content
15,860,844 members
Articles / Desktop Programming / MFC
Article

Making any application transparent in Windows 2000/XP

Rate me:
Please Sign up or sign in to vote.
4.92/5 (52 votes)
7 Jul 20033 min read 207.4K   6.6K   105   49
This article discusses how the layering feature in Windows 2000 and Windows XP can be used to make any application transparent even when its source is not available

Image 1

Introduction

There are many articles that demonstrate how you can use the new layering features of Windows 2000 or Windows XP to create applications that are translucent. This article adds to that and shows how you can use the same features to turn any application transparent even if you do not have sources for it.

Using this "WinTrans" application you will be able to select any currently running application and turn it transparent by dragging and dropping the wand (icon at the top-left corner) on the title bar of that application. You can also control the transparency using the slider. "WinTrans" has an interface very much like SPY and also demonstrates the usage of Win32 APIs to locate a window under the mouse pointer and extract details like the class associated with it, the caption of the window, etc.

"WinTrans" comes in handy if you want to work on a maximized window and at the same time keep a watch on some other application running in the background.

Background

In Windows 2000 and Windows XP a new function named SetLayeredWindowAttributes has been added to User32.dll. To use this function an application needs to set the WS_EX_LAYERED (0x00080000) bit in the window style either while creating it or later using the SetWindowLong function. Once this bit is set any application can call this function passing a handle to a window and make either the whole window or a particular color on the window transparent. This function takes the following arguments.

  • HWND hWnd: Handle to the window
  • COLORREF col: Color to be made transparent
  • BYTE bAlpha: If this value is 0 the window becomes completely transparent and if it is 255 it becomes opaque
  • DWORD dwFlags: If this flag is 1 then only the color col is made transparent. If it is 2 then the whole window becomes transparent as dictated by the bAlpha value

Using the code

We begin by defining the following member variables of the main dialog class in WinTransDlg.h

bool m_bTracking;   //  will be true when the mouse is 
                    //  being tracked
HWND m_hCurrWnd;    //  Handle to the window over which 
                    //  the mouse was last present
HCURSOR m_hCursor;  //  The wand cursor

We also define a function pointer that points to the SetLayeredWindowAttributes function. This function is defined in the User32.dll.

//  Global definition
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, 
              BYTE bAlpha, DWORD dwFlags);
lpfn g_pSetLayeredWindowAttributes;

In the OnInitDialog event handler for the dialog we get the address of the SetLayeredWindowAttributes function and store it in g_pSetLayeredWindowAttributes. We also load the wand cursor and store the handle in m_hCursor

BOOL CWinTransDlg::OnInitDialog()
{
    ....
    //  get the function pointer for SetLayeredWindowAttributes 
    //  in User32.dll
    HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
    g_pSetLayeredWindowAttributes = (lpfn)GetProcAddress(hUser32,
               "SetLayeredWindowAttributes");
    if (g_pSetLayeredWindowAttributes == NULL)
        AfxMessageBox (
            "Layering is not supported in this version of Windows",
             MB_ICONEXCLAMATION);

    //  Load the wand cursor
    HINSTANCE hInstResource = AfxFindResourceHandle(
         MAKEINTRESOURCE(IDC_WAND), RT_GROUP_CURSOR);
    m_hCursor = ::LoadCursor( hInstResource, MAKEINTRESOURCE(IDC_WAND) );
    ...
}

We then define handler for the WM_LBUTTONDOWN, WM_LBUTTONUP and WM_MOUSEMOVE events. For the handler for WM_LBUTTONDOWN we do the following

void CWinTransDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    ...
    SetCapture();      // make mouse move events to 
                       // be directed to this window
    m_hCurrWnd = NULL; // Currently no window is to be made transparent
    m_bTracking = true;     // set the tracking flag
    ::SetCursor(m_hCursor); // turn the mouse pointer into the wand cursor
    ...
}

For mouse move event handler the following code is used

void CWinTransDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    ...
    if (m_bTracking)
    {
        ...
        //  convert mouse coordinates to screen
        ClientToScreen(&point);
        ...
        //  get the window at the mouse coords
        m_hCurrWnd = ::WindowFromPoint(point);
        ...
        // Show details of the window like class, caption, etc.
        ...
    }
    ...
}

As long as the left mouse button is clicked anywhere inside the main dialog and is not released the mouse pointer will change into the wand and the details of the window beneath the pointer will be displayed on the WinTrans dialog

When the button is released the event handler for WM_LBUTTONUP is called.

void CWinTransDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
    ...
    //  stop tracking the mouse
    ReleaseCapture();
    m_bTracking = false;

    //  If the window under the mouse is not of this 
    //  application we toggle its
    //  layer style flag and apply the alpha as set by the slider control
    if (g_pSetLayeredWindowAttributes && m_hCurrWnd != m_hWnd)
    {
        ::SetWindowLong(m_hCurrWnd, GWL_EXSTYLE,
                        GetWindowLong(m_hCurrWnd, 
                        GWL_EXSTYLE) ^ WS_EX_LAYERED);
        g_pSetLayeredWindowAttributes(m_hCurrWnd, 0,
                        (BYTE)m_slider.GetPos(), LWA_ALPHA);

        ::RedrawWindow(m_hCurrWnd, NULL, NULL,
                       RDW_ERASE | RDW_INVALIDATE | 
                       RDW_FRAME | RDW_ALLCHILDREN);
    }
    ...
}

Points of Interest

This application works correctly only when the wand is dropped on the title bar of another application or on the body of a dialog based application. If for example the wand is dropped on the body of notepad it will not work

To remove the transparent effect you can simply drag-n-drop the wand again on that application. Since in OnLButtonUp we toggle the WS_EX_LAYERED bit the transparency effect will also be toggled

TransWand does not work on the command window

History

  • v1.0 This is the initial version.
  • v1.1 Added Undo All button and check box to clear the transparency effect on all windows when WinTrans is closed

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
United States United States
I just love coding. I started programming in 1995 with BASIC and then moved through Cobol, Pascal, Prolog, C, C++, VB, VC++ and now C#/.NET.

I received a Bachelor of Technology degree in Computer Science from University of Calcutta in 2001.

I worked for some time in Texas Instruments, Adobe Systems and now in Microsoft India Development Center in the Visual Studio Team Systems.

I am from the City of Joy, Kolkata in India, but now live and code Hyderabad.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Joe T. King10-Jun-13 2:37
Joe T. King10-Jun-13 2:37 
GeneralMy vote of 5 Pin
runafterfree19-Jul-12 16:47
runafterfree19-Jul-12 16:47 
GeneralWindows 7 transparency Pin
Scott Sucher29-Jul-10 14:06
Scott Sucher29-Jul-10 14:06 
GeneralWindows 7 total transparency of open windows Pin
Scott Sucher29-Jul-10 14:05
Scott Sucher29-Jul-10 14:05 
Generalworks like a charm Pin
cap3327-Jul-10 21:37
cap3327-Jul-10 21:37 
GeneralNice, windows border Pin
addictive7-Nov-08 7:15
addictive7-Nov-08 7:15 
GeneralNice demo Pin
Anil K P9-Dec-07 22:55
Anil K P9-Dec-07 22:55 
Generaldifficult problem for me Pin
uy quoc5-Dec-06 23:20
uy quoc5-Dec-06 23:20 
GeneralHelp me! Pin
uy quoc3-Dec-06 22:34
uy quoc3-Dec-06 22:34 
GeneralRe: Help me! Pin
Ratish Philip4-Dec-06 16:11
Ratish Philip4-Dec-06 16:11 
GeneralRe: Help me! Pin
uy quoc5-Dec-06 14:14
uy quoc5-Dec-06 14:14 
QuestionIs there any possible way to change Edit Box Or Forms Caption? Pin
sis138228-Jul-06 21:48
sis138228-Jul-06 21:48 
Generalbehind transparency Pin
Robsori12-Jun-06 22:56
Robsori12-Jun-06 22:56 
GeneralRotating a dialog 90 degrees Pin
AlexEvans11-Jan-06 10:04
AlexEvans11-Jan-06 10:04 
GeneralRe: Rotating a dialog 90 degrees Pin
abhinaba12-Jan-06 5:43
abhinaba12-Jan-06 5:43 
GeneralRe: Rotating a dialog 90 degrees Pin
AlexEvans12-Jan-06 14:30
AlexEvans12-Jan-06 14:30 
GeneralOne suggestion Pin
PravinSingh22-Aug-05 21:24
PravinSingh22-Aug-05 21:24 
GeneralRe: One suggestion Pin
abhinaba23-Aug-05 1:06
abhinaba23-Aug-05 1:06 
Generalhiding the cursor Pin
leojose3-Jun-05 20:39
leojose3-Jun-05 20:39 
GeneralThere is one serious error in code Pin
Member 9796006-Sep-04 5:45
Member 9796006-Sep-04 5:45 
GeneralRe: There is one serious error in code Pin
abhinaba6-Sep-04 18:56
abhinaba6-Sep-04 18:56 
GeneralRe: There is one serious error in code Pin
Member 9796007-Sep-04 8:55
Member 9796007-Sep-04 8:55 
Generalwhy SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
Eureka Jim11-Aug-04 4:27
Eureka Jim11-Aug-04 4:27 
GeneralPocket PC Pin
pratik67821-May-04 4:14
pratik67821-May-04 4:14 
GeneralRe: Pocket PC Pin
abhinaba21-May-04 19:05
abhinaba21-May-04 19:05 

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.