Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Transparency without Source Code

0.00/5 (No votes)
11 Aug 2000 3  
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