Click here to Skip to main content
16,013,465 members
Articles / Desktop Programming / MFC
Article

Transparency without Source Code

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
11 Aug 2000 277.3K   6K   118   54
Adding transparency to any window, even if you don't have its source.
  • Download source files - 13 Kb
  • Download executable (MFC DLLs required) - 5 Kb
  • When I checked the newly available APIs for Windows 2000, I ran into layered windows, and after playing around with it for a while, I finally wrote this cute little program. It allows you to add transparency to any window, just by clicking on it with your mouse.

    Image 1

    The core functionality is done in only 4 lines of code (6 if you include variable definitions). The rest is just wizard-generated MFC code.

    HWND hWnd;
    POINT pt;
    ::GetCursorPos(&pt);
    hWnd=::WindowFromPoint(pt);
    SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)^WS_EX_LAYERED);
    SetLayeredWindowAttributes(hWnd,RGB(0,0,0),m_slider.GetPos(),LWA_ALPHA);

    First it finds the window under the current cursor position by using GetCursorPos() and WindowFromPoint, then it toggles its WS_EX_LAYERED (new in W2k) style using SetWindowLong, and finally, it sets its transparency to a value (between 0 and 255) defined by a slider control. The new SetLayeredWindowAttributes function is available only on Windows 2000, and is well-documented in the current MSDN library. You can also use it for color-keying, i.e. to make pixels of a specific color completely transparent, while leaving other pixels unchanged. The two effects can also be combined.

    SetLayeredWindowAttributes is defined as follows:

    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
    );

    SetLayeredWindowAttributes can also be used to fade in/out other windows, or to create irregularly formed windows (this was also possible using window regions, but that was much slower).

    I personally use this program to make my Taskbar, ICQ and Winamp windows transparent, since these are always on top, and I prefer being able to see what happens behind them.

    Have Fun!

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

    Comments and Discussions

     
    GeneralRe: window transparency/color keying vs OpenGL Pin
    9-Jun-02 12:23
    suss9-Jun-02 12:23 
    QuestionHow do you change the window properties of a control bar so when it floats it transparent? Pin
    Lucas Zanatta17-Mar-02 21:27
    Lucas Zanatta17-Mar-02 21:27 
    AnswerRe: How do you change the window properties of a control bar so when it floats it transparent? Pin
    21-Apr-02 2:21
    suss21-Apr-02 2:21 
    GeneralLuv the programm but.. Pin
    18-Dec-01 21:34
    suss18-Dec-01 21:34 
    GeneralRe: Luv the programm but.. Pin
    1-May-02 4:25
    suss1-May-02 4:25 
    QuestionHow can I activate this feature on my 2000 Pin
    Michel Yossef David12-Dec-01 3:41
    Michel Yossef David12-Dec-01 3:41 
    QuestionWhat to do in ActiveX Control? Pin
    Daed30-Jul-01 23:04
    Daed30-Jul-01 23:04 
    GeneralTransparency vs. Windows Media Player Pin
    21-May-01 2:36
    suss21-May-01 2:36 
    This is a great sample. Thanks!

    I have need to build translucent windows like this in my application. The sample works great in all the places I need except the most important for my program. It does not work properly with Windows Media Player. I'm running version 7.00.00.1956, but can run another version if that would help. Of course, I'm running on Win2000.

    What I need is a window that is about 50% translucent, floating above the Windows Media Player (WMP), so the running movie shows through. When WMP is running in its browser-like mode, it shows through the overlaying translucent window just fine. Once I start playing a video in WMP, the rectangle of the video goes dark in the overlaying window. I've tried this playing an MPEG and an AVI, with similar results in both cases.

    For my application, I don't need to make the WMP window itself translucent. But, as an additional data point, if you use Bernhard Hammer's sample application to set the WMP translucent, WMP itself turns the rectangle of the playing video dark. And, once you quit WMP, you get some odd remnants left over on the screen.

    So, does anyone know how to make a translucent window that overlays WMP and properly shows WMP's playing video through the overlaying window? Is there something special I need to do in my use of WMP? I'm using WMP as an Active X control in my MFC Visual C++ application.
    GeneralRe: Transparency vs. Windows Media Player Pin
    21-May-01 11:22
    suss21-May-01 11:22 
    GeneralRe: Transparency vs. Windows Media Player Pin
    22-May-01 4:33
    suss22-May-01 4:33 
    GeneralRe: Transparency vs. Windows Media Player Pin
    25-May-01 21:56
    suss25-May-01 21:56 
    GeneralConsole windows Pin
    Thomas Freudenberg10-Apr-01 23:44
    Thomas Freudenberg10-Apr-01 23:44 
    GeneralRe: Console windows Pin
    1-Oct-01 13:11
    suss1-Oct-01 13:11 
    GeneralRe: Console windows Pin
    Anonymous18-Sep-03 5:07
    Anonymous18-Sep-03 5:07 
    QuestionWhere do we get libraries from? Pin
    16-Dec-00 23:27
    suss16-Dec-00 23:27 
    AnswerRe: Where do we get libraries from? Pin
    14-Jan-01 7:34
    suss14-Jan-01 7:34 
    GeneralJust an idea Pin
    NormDroid12-Dec-00 0:13
    professionalNormDroid12-Dec-00 0:13 
    GeneralRe: Just an idea Pin
    14-Dec-00 1:29
    suss14-Dec-00 1:29 
    GeneralRe: Just an idea Pin
    NormDroid14-Dec-00 3:25
    professionalNormDroid14-Dec-00 3:25 
    GeneralSame feature for Visual Basic Pin
    1-Dec-00 7:42
    suss1-Dec-00 7:42 
    GeneralSource version != Executeable version Pin
    29-Nov-00 14:25
    suss29-Nov-00 14:25 
    GeneralA error LNK 2001 Pin
    Thomas Jacobsen4-Nov-00 12:40
    Thomas Jacobsen4-Nov-00 12:40 
    GeneralRe: A error LNK 2001 Pin
    5-Nov-00 10:39
    suss5-Nov-00 10:39 
    GeneralRe: A error LNK 2001 Pin
    Thomas Jacobsen6-Nov-00 0:34
    Thomas Jacobsen6-Nov-00 0:34 
    QuestionSDK? Pin
    Steven Carleton20-Aug-00 12:46
    Steven Carleton20-Aug-00 12:46 

    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.