With the improvement of graphic display technologies, the API of operating systems also improved, providing programmers with more and more opportunities for developing non-standard interface elements. So one of the innovations of the Windows 2000 operating system was layered windows, parts of which could be translucent. Soon, a description of the API for working with layered windows became available on MSDN. However, on programmers' forums, questions about how to make a window semi-transparent are still being asked.
Necessary
- - compiler;
- - windows sdk or framework for windows programming.
Instructions
Step 1
Get a handle to the window you want to make semi-transparent. Find or create a window. To create it, use either the CreateWindow, CreateWindowEx API functions, or the wrapper methods around these functions belonging to the classes of the used framework. The prototype for the CreateWindow function looks like this:
HWND CreateWindow (LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent,
HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
As you can see, the function returns a handle to the created window as the result of execution. If any wrapper class is used, use its methods on the object corresponding to the created window to get the handle.
Step 2
Finding a window can be done using API calls FindWindow, FindWindowEx, EnumWindows, EnumChildWindows, EnumThreadWindows, and their combinations. You can get a handle to a window in a specific area using the WindowFromPoint and ChildWindowFromPoint functions.
Step 3
Set the window to the extended style WS_EX_LAYERED. Use the SetWindowLong API or the corresponding methods of wrapper objects. The SetWindowLong function completely replaces the modifiable window parameter information, so use it in combination with the GetWindowLong function to retrieve the previous value for a set of style flags. For example, the style can be changed like this:
:: SetWindowLong (hWnd, GWL_EXSTYLE,:: GetWindowLong (hWnd, GWL_EXSTYLE));
Here hWnd is the window handle found as a result of performing the actions described in the previous step.
Step 4
Make the window semi-transparent. Use the SetLayeredWindowAttributes API or methods of the wrapper classes. The SetLayeredWindowAttributes function prototype looks like this:
BOOL SetLayeredWindowAttributes (HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
Step 5
The hwnd parameter to the function must be a valid window handle obtained in the first step. The crKey parameter is a color key used to define semi-transparent areas. The bAlpha parameter specifies the translucency value. With the bAlpha parameter value equal to 0, the "semi-transparent" areas will be completely transparent. If the bAlpha parameter is 255, they will be completely opaque. The dwFlags parameter determines the mode of further display of the window contents. When the LWA_COLORKEY flag is included in the dwFlags value, the semi-transparent areas of the window will be determined based on the color key. When the LWA_ALPHA flag is enabled, the bAlpha parameter will be used to determine the translucency value.
Step 6
To make the entire window semi-transparent, call SetLayeredWindowAttributes with the preferred bAlpha parameter value, the LWA_ALPHA flag, but no LWA_COLORKEY flag. Use the found window handle as the first parameter to the function. For example, to make a window half transparent, use the call:
:: SetLayeredWindowAttributes (hWnd, RGB (0, 0, 0), 128, LWA_ALPHA);