Click here to Skip to main content
Licence 
First Posted 11 Aug 2000
Views 215,928
Bookmarked 106 times

Transparency without Source Code

By | 11 Aug 2000 | Article
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.

    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

    About the Author

    sBernhard Hammer



    Austria Austria

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralNot Working in Windows2000 service Pack 4 PinmemberGaurang Shah 03323:53 15 Aug '07  
    Generalerror LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pinmemberjayender21:12 10 Aug '06  
    GeneralRe: error LNK2001: unresolved external symbol __imp__SetLayeredWindowAttributes@16 Pinmemberbob169726:13 7 Sep '06  
    Generalproblem in importing a dll in VC++ Pinsussvineet jain2:47 1 Apr '05  
    Generalerror LNK2001: unresolved external symbol __imp__ldap_init Pinmembermandanani22:50 24 Mar '05  
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init Pinsussvineet jain2:51 1 Apr '05  
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init PinmemberDoug Tomlinson2:28 2 Aug '05  
    GeneralRe: error LNK2001: unresolved external symbol __imp__ldap_init PinmemberDoug Tomlinson3:08 3 Aug '05  
    Generalwhy SetLayeredWindowAttributes can't used in 8bits(256) color quality Pinmemberwhizzkid@ms28.hinet.net4:07 11 Aug '04  
    GeneralAccessing layered window DC Pinmembersckaspick13:53 29 Jun '04  
    Generalcompile erro Pinmember_skidrow_vn_8:43 10 Feb '04  
    GeneralRe: compile erro PinmemberRRode6:28 10 Feb '05  
    Generalcompile error: unresolved external symbol __imp__... Pinsussx meng15:02 18 Nov '03  
    GeneralAlpha blend from a bitmap ! Pinmemberxela3:45 8 Oct '03  
    GeneralTransparency of individual components Pinmemberannation13:03 28 Aug '03  
    GeneralVC++ 7 Conversion Pinmembervisualflux10:29 13 Jun '03  
    GeneralTransparent inactive window Pinmemberlnkenjke8:11 26 May '03  
    GeneralDrawing on Transparent Window.. Pinmemberchamana20:12 3 Mar '03  
    GeneralRe: Drawing on Transparent Window.. PinmemberAlireza Mesgar1:45 29 Sep '04  
    GeneralThat's great ! PinmemberAlicia20033:23 3 Dec '02  
    Generalwindow transparency/color keying vs OpenGL Pinmemberolione2:54 29 Apr '02  
    GeneralRe: window transparency/color keying vs OpenGL PinmemberAnonymous12:23 9 Jun '02  
    QuestionHow do you change the window properties of a control bar so when it floats it transparent? PinmemberLucas Zanatta21:27 17 Mar '02  
    AnswerRe: How do you change the window properties of a control bar so when it floats it transparent? PinmemberAnonymous2:21 21 Apr '02  
    GeneralLuv the programm but.. PinmemberAlfred21:34 18 Dec '01  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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