Transparent and click-through window
In December 2005 column of MSDN magazine Paul DiLascia explained how to create a so called "layered" window which is transparent and passes mouse events through itself. It turned out an easy thing to do and I wrote a WTL class that I called (surprisingly) CLayered.
One remark though - code presented here will work only on Windows 2000 and higher.
To create a visible-through (layered) window, we need to set the extended style
WS_EX_LAYERED, and to make the window clicked-through, we need
WS_EX_TRANSPARENT extended style. This is done with
SetWindowLong() API function or
ModifyStyleEx() function of the CWindow class.
Then, we can use the
SetLayeredWindowAttributes() API function to set the opacity and transparency of a layered window.
To reverse the transparency, remove
But let me warn you that setting the
WS_EX_TRANSPARENT attribute affects the entire window: the user can't close the window using the 'x' button, select it with the mouse, or select any controls on the window. The application can still close the window programmatically.
Microsoft recommends using the
WS_EX_TRANSPARENT style for modal windows that have a short life.
How to use it
Derive your class from
CLayered like so:
class CMainDlg : public CDialogImpl<CMainDlg>,
Then somewhere in your code modify window style:
// To make a visible-through window
// To make a clicked-through window
And to set the transparency of the window, if necessary, call
// nOpacity describes the opacity of the layered window.
// 0 means the window is completely transparent, 255 means the window is opaque.
BOOL MakeTransparent(int nOpacity)