Click here to Skip to main content
15,868,141 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 272.6K   5.9K   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

     
    GeneralNot Working in Windows2000 service Pack 4 Pin
    GauranG Shah15-Aug-07 23:53
    GauranG Shah15-Aug-07 23:53 
    Generalerror LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pin
    jayender10-Aug-06 21:12
    jayender10-Aug-06 21:12 
    GeneralRe: error LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pin
    bob169727-Sep-06 6:13
    bob169727-Sep-06 6:13 
    Generalproblem in importing a dll in VC++ Pin
    vineet jain1-Apr-05 2:47
    vineet jain1-Apr-05 2:47 
    Generalerror LNK2001: unresolved external symbol __imp__ldap_init Pin
    mandanani24-Mar-05 22:50
    mandanani24-Mar-05 22:50 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    vineet jain1-Apr-05 2:51
    vineet jain1-Apr-05 2:51 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    Doug Tomlinson2-Aug-05 2:28
    Doug Tomlinson2-Aug-05 2:28 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    Doug Tomlinson3-Aug-05 3:08
    Doug Tomlinson3-Aug-05 3:08 
    Generalwhy SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
    Eureka Jim11-Aug-04 4:07
    Eureka Jim11-Aug-04 4:07 
    GeneralAccessing layered window DC Pin
    sckaspick29-Jun-04 13:53
    sckaspick29-Jun-04 13:53 
    Generalcompile erro Pin
    _skidrow_vn_10-Feb-04 8:43
    _skidrow_vn_10-Feb-04 8:43 
    GeneralRe: compile erro Pin
    Member 172228710-Feb-05 6:28
    Member 172228710-Feb-05 6:28 
    Generalcompile error: unresolved external symbol __imp__... Pin
    x meng18-Nov-03 15:02
    sussx meng18-Nov-03 15:02 
    I have this compile error under VC++ 6.0: "unresolved external symbol __imp__SetLayeredWindowAttributes@16"

    Any advice?

    x meng
    xmeng@nc.rr.com
    GeneralAlpha blend from a bitmap ! Pin
    xela8-Oct-03 3:45
    xela8-Oct-03 3:45 
    GeneralTransparency of individual components Pin
    Member 55445328-Aug-03 13:03
    Member 55445328-Aug-03 13:03 
    GeneralVC++ 7 Conversion Pin
    visualflux13-Jun-03 10:29
    visualflux13-Jun-03 10:29 
    GeneralTransparent inactive window Pin
    lnkenjke26-May-03 8:11
    lnkenjke26-May-03 8:11 
    GeneralDrawing on Transparent Window.. Pin
    chamana3-Mar-03 20:12
    chamana3-Mar-03 20:12 
    GeneralRe: Drawing on Transparent Window.. Pin
    Alireza Mesgar29-Sep-04 1:45
    Alireza Mesgar29-Sep-04 1:45 
    GeneralThat's great ! Pin
    Alicia20033-Dec-02 3:23
    Alicia20033-Dec-02 3:23 
    Generalwindow transparency/color keying vs OpenGL Pin
    29-Apr-02 2:54
    suss29-Apr-02 2:54 
    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 

    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.