Click here to Skip to main content
12,695,624 members (27,017 online)
Click here to Skip to main content
Add your own
alternative version

Stats

242.5K views
5.5K downloads
112 bookmarked
Posted

Transparency without Source Code

, 11 Aug 2000
Rate this:
Please Sign up or sign in to vote.
Adding transparency to any window, even if you don't have its source.
<!-- Link to source file download -->
  • Download source files - 13 Kb
  • <!-- Link to demo file download -->
  • Download executable (MFC DLLs required) - 5 Kb
  • <!-- Add the rest of your HTML here -->

    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.

    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

    Share

    About the Author

    No Biography provided

    You may also be interested in...

    Pro
    Pro

    Comments and Discussions

     
    GeneralNot Working in Windows2000 service Pack 4 Pin
    Gaurang Shah 03316-Aug-07 0:53
    memberGaurang Shah 03316-Aug-07 0:53 
    Generalerror LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pin
    jayender10-Aug-06 22:12
    memberjayender10-Aug-06 22:12 
    GeneralRe: error LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pin
    bob169727-Sep-06 7:13
    memberbob169727-Sep-06 7:13 
    Generalproblem in importing a dll in VC++ Pin
    vineet jain1-Apr-05 3:47
    sussvineet jain1-Apr-05 3:47 
    Generalerror LNK2001: unresolved external symbol __imp__ldap_init Pin
    mandanani24-Mar-05 23:50
    membermandanani24-Mar-05 23:50 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    vineet jain1-Apr-05 3:51
    sussvineet jain1-Apr-05 3:51 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    Doug Tomlinson2-Aug-05 3:28
    memberDoug Tomlinson2-Aug-05 3:28 
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pin
    Doug Tomlinson3-Aug-05 4:08
    memberDoug Tomlinson3-Aug-05 4:08 
    Generalwhy SetLayeredWindowAttributes can't used in 8bits(256) color quality Pin
    whizzkid@ms28.hinet.net11-Aug-04 5:07
    memberwhizzkid@ms28.hinet.net11-Aug-04 5:07 
    GeneralAccessing layered window DC Pin
    sckaspick29-Jun-04 14:53
    membersckaspick29-Jun-04 14:53 
    Generalcompile erro Pin
    _skidrow_vn_10-Feb-04 9:43
    member_skidrow_vn_10-Feb-04 9:43 
    GeneralRe: compile erro Pin
    RRode10-Feb-05 7:28
    memberRRode10-Feb-05 7:28 
    Generalcompile error: unresolved external symbol __imp__... Pin
    x meng18-Nov-03 16:02
    sussx meng18-Nov-03 16:02 
    GeneralAlpha blend from a bitmap ! Pin
    xela8-Oct-03 4:45
    memberxela8-Oct-03 4:45 
    GeneralTransparency of individual components Pin
    annation28-Aug-03 14:03
    memberannation28-Aug-03 14:03 
    GeneralVC++ 7 Conversion Pin
    visualflux13-Jun-03 11:29
    membervisualflux13-Jun-03 11:29 
    GeneralTransparent inactive window Pin
    lnkenjke26-May-03 9:11
    memberlnkenjke26-May-03 9:11 
    GeneralDrawing on Transparent Window.. Pin
    chamana3-Mar-03 21:12
    memberchamana3-Mar-03 21:12 
    GeneralRe: Drawing on Transparent Window.. Pin
    Alireza Mesgar29-Sep-04 2:45
    memberAlireza Mesgar29-Sep-04 2:45 
    GeneralThat's great ! Pin
    Alicia20033-Dec-02 4:23
    memberAlicia20033-Dec-02 4:23 
    Generalwindow transparency/color keying vs OpenGL Pin
    olione29-Apr-02 3:54
    memberolione29-Apr-02 3:54 
    GeneralRe: window transparency/color keying vs OpenGL Pin
    Anonymous9-Jun-02 13:23
    memberAnonymous9-Jun-02 13:23 
    QuestionHow do you change the window properties of a control bar so when it floats it transparent? Pin
    Lucas Zanatta17-Mar-02 22:27
    memberLucas Zanatta17-Mar-02 22:27 
    AnswerRe: How do you change the window properties of a control bar so when it floats it transparent? Pin
    Anonymous21-Apr-02 3:21
    memberAnonymous21-Apr-02 3:21 
    GeneralLuv the programm but.. Pin
    Alfred18-Dec-01 22:34
    memberAlfred18-Dec-01 22: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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170118.1 | Last Updated 12 Aug 2000
    Article Copyright 2000 by sBernhard Hammer
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid