分类: C/C++
2008-08-25 17:51:07
CLayeredWindowHelperST
is a wrapper class of all required APIs to add support for transparent (layered)
windows to your applications. This is a scalable class. If running under Windows 2000/XP your windows
will have the selected transparent effect, while if running under Windows 9x/ME/NT will run error-less.
In your project include the following files:
CLayeredWindowHelperST
. This class encapsulates all the required APIs to supportCLayeredWindowHelperST G_Layered;Modify the style of your window, then set the transparency effect. For dialog-based applications, in your
OnInitDialog
: // Call the base-class method CDialog::OnInitDialog(); // Modify the style G_Layered.AddLayeredStyle(m_hWnd); // Set the trasparency effect to 70% G_Layered.SetTransparentPercentage(m_hWnd, 70); -or- G_Layered.SetLayeredWindowAttributes(m_hWnd, 0, 210, LWA_ALPHA);
AddLayeredStyle
Adds the WS_EX_LAYERED
style to the specified window.
This style is required to have a transparent effect.
// Parameters: // [IN] Handle to the window and, indirectly, the class to which the window belongs. // Windows 95/98/Me: The AddLayeredStyle function may fail if the window // specified by the hWnd parameter does not belong to the same process // as the calling thread. // // Return value: // Non zero // Function executed successfully. // Zero // Function failed. To get extended error information, call ::GetLastError(). // LONG AddLayeredStyle(HWND hWnd)SetLayeredWindowAttributes
// Parameters: // [IN] hWnd // Handle to the layered window. // [IN] crKey // A COLORREF value that specifies the transparency color key to be used when // composing the layered window. All pixels painted by the window in this color will be transparent. // To generate a COLORREF, use the RGB() macro. // [IN] bAlpha // Alpha value used to describe the opacity of the layered window. // When bAlpha is 0, the window is completely transparent. // When bAlpha is 255, the window is opaque. // [IN] dwFlags // Specifies an action to take. This parameter can be one or more of the following values: // LWA_COLORKEY Use crKey as the transparency color. // LWA_ALPHA Use bAlpha to determine the opacity of the layered window. // // Return value: // TRUE // Function executed successfully. // FALSE // Function failed. To get extended error information, call ::GetLastError(). // BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)SetTransparentPercentage
// Parameters: // [IN] hWnd // Handle to the layered window. // [IN] byPercentage // Percentage (from 0 to 100) // // Return value: // Non zero // Function executed successfully. // Zero // Function failed. To get extended error information, call ::GetLastError(). // BOOL SetTransparentPercentage(HWND hWnd, BYTE byPercentage)GetVersionI
// Return value: // Class version. Divide by 10 to get actual version. // static short GetVersionI()GetVersionC
// Return value: // Pointer to a null-terminated string containing the class version. // static LPCTSTR GetVersionC()
To compile CLayeredWindowHelperST
you need the Platform SDK (August 2001 or newer). This is not
mandatory because at compile-time, if not found, it will be emulated. At run-time you don't need any
additional SDK or libraries installed.
THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.