This article mainly introduces the new API function setLayeredWindowAttributes () and the general method of implementing form transparent effects through this function: This article mainly introduces new API functions setlayeredWindowAttributes () and through this function A general method of transparent effects, and combines the code to give a specific programming implementation process. Introduction Windows 2000 operating system has a lot of improvement and improvement in quality or in the user interface, the user is more simple, convenient, and feel more comfortable. If the user pays attention to it, it is not difficult to find some new features on the interface. For example, Windows 2000 provides support for mixed cursor with shadow alpha, providing support for menus, prompt box fade in fade out, and dragging in the shell with alpha mixed effect pictures and other transparent effects. All of these special effects seem to be different, but actually achieved by calling for a hierarchical window API function for Windows 2000. Since it is the API function provided by the Windows 2000 system, it means that it is also possible to achieve various effects that supports the support in its own application. In the program design, sometimes some minor improvements are sufficient to enhance the display effect of the user interface. For example, using a cursor with an Alpha mixed effect is obviously better than a common cursor, especially under large screen displays or multi-display systems, this cursor is easier to find. Further, the screen size of the display is limited. If you want to see the contents of multiple windows, it is clear that it is not convenient, although the large-screen display or multi-display system can mitigate to a certain extent but far better to use these applications. The body is set to be transparent or semi-transparent. The two applications shown below have overwritten, but the program form in the front desk obviously does not affect the display of the background Word document. In view of the practical role of layered window functions in programming, this article will provide specific introduction to fully explore its potential and apply it to program design.
The hierarchical window first introduces the WS_EX_LAYERED extension window style before introducing the hierarchical window function. The window style is also new in Windows 2000. If this property is used, the form will have visual effects in composite shape, animation, alpha mixing. The window appears in a rectangle on the screen by cutting cropped by other windows. In order to achieve a circular form, it is not enough to draw a circular form, so that the system also clicks the form in the original rectangle, and the window under the form will still be rectangled by the form. Cropped. Perhaps consider the snapshot of the visible area under the form of the form before displaying the garden form, and draws it to the current form after the form is displayed later. However, since other windows can draw the area under this form, the front desk cannot be learned when the drawing will happen without timely acquisition of new snapshots, this solution does not Process, normal work in multi-tasking environments. For this case, the correct approach to Windows 95/98 and Windows NT 4.0 is to indicate the desired form shape through the setWindowRGN () API function, but this processing is frequently changed the form shape or dragging on the screen. There is still defective: the front form forms will require the form below to redraw over the entire area, which will have excessive messages and calculation. Moreover, SETWINDOWRGN () can only realize the full transparency of the form, and the translucent effect cannot be achieved. Perhaps this is why the hierarchical window is proposed. The hierarchical window truly achieves two distracted concepts: hierarchical and redirects. In order to be able to remove any layer, the WS_EX_LAYERED flag bit must be set, which can be set when the form is created, and it can be set by calling setWindowlong () by the GWL_EXSTYLE flag after being created. Next, you can update the hierarchical window via the UpdateLayeredWindows () function. When used, you need to draw a visual area in the bitmap and provide it with key colors, alpha mixing parameters, etc., together with the UpdateLayeredWindows () function. It should be noted that the application does not need to respond to WM_PAINT or other draw messages when using the updatelayeredWindows () function. In addition, it is also possible to implement the traditional Win32 drawing mechanism, which requires calling another API function setLayeredWindowAttributes () to complete the setting of the key color or Alpha mixed parameter value. Once the function is called, the system will begin to redirect all drawings and automatically apply the specified effects for the hierarchical window. The implementation of the formal translucent effects has made a more detailed introduction to the hierarchical window. Here will be specifically introduced here for the setlayeredWindowAttributes () function and its usage method, and the form is translucent on the basis. First give the function of the setlayeredWindowAttributes ():
BOOL SetLayeredWindowAttributes (HWND hwnd, // handle to the layered windowCOLORREF crKey, // specifies the color keyBYTE bAlpha, // value for the blend functionDWORD dwFlags // action); wherein there dwFlags LWA_ALPHA (value of 2), and LWA_COLORKEY (value 1) Two settings, if lwa_alpha is set, specify the transparency of the form via the Balpha parameter; if the LWA_COLORKEY flag is set, specify the key color to CRKEY, the area covered by the color will be removed from the form, after removal The area will no longer be clicked, and other colors are displayed normally. If the Alpha mixed value is set to 0, the window area is also not tap. As mentioned earlier, the SetlayeredWindowAttributes () function is an API function added by Windows 2000, which is the module to be user32.dll. After loading the USER32.DLL module with getModuleHandle () and calls getProcAddress () to get the function setLayeredWindowAttributes () After the pointer in user32.dll, you can set the form to a half translation through the setLayeredWindowAttributes () function: // Global variable TypedEf Bool (WINAPI * lpfn) (HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags); lpfn g_pSetLayeredWindowAttributes; ...... // get the function pointer in User32.dll SetLayeredWindowAttributes HMODULE hUser32 = GetModuleHandle (_T ( "USER32.DLL")) ; g_pSetLayeredWindowAttributes = (lpfn) GetProcAddress (hUser32, "SetLayeredWindowAttributes"); if (g_pSetLayeredWindowAttributes == NULL) :: PostQuitMessage (0); this code is generally run at initialization and when the program module is loaded into memory and USER32.DLL obtain Get the setlayeredWindowAttributes () function pointer. You need to release the previously loaded module before the program exits:
// Uninstall the module IF (M_HUSER32! = NULL) Freelibrary (m_huser32); When setting the form to translucent, first get the window handle of the form to indicate which window to be operated. If you are setting a window in this application, you can be dynamically obtained by passing the window handle or with getSafehWnd (). If you want to set a program window outside of this program, the general approach is to get the window handle of the specified window title by calling the FindWindow () function. After getting the window handle, setLayeredWindowAttributes () cannot be called directly, you need to add a WS_EX_LAYERED extension in GetWindowlong () after getting the current window style setting, and set it through the setWindowlong () function: // transparent HWND hWnd = GetSafeHwnd (); LONG lWindowLong = GetWindowLong (hWnd, GWL_EXSTYLE) | WS_EX_LAYERED; :: SetWindowLong (hWnd, GWL_EXSTYLE, lWindowLong); g_pSetLayeredWindowAttributes (hWnd, 0, (BYTE) m_sldAlpha.GetPos (), 2) ;: : RedRawwindow (HWND, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); In order to be able to use this function, you must also add a predefined statement before the above statement:
#define WS_EX_LAYERED 0X00080000 Implementation of the Special Form Special Effect Use setLayeredWindowAttributes () Creating a shaped form is very simple, the specific process is extremely similar to the translucent form, and the different is only set to LWA_COLORKEY and specify that the transparent display is required. Key colors:
// Specify the key color HWND hWnd = GetSafeHwnd (); LONG lWindowLong = GetWindowLong (hWnd, GWL_EXSTYLE) | WS_EX_LAYERED; :: SetWindowLong (hWnd, GWL_EXSTYLE, lWindowLong); g_pSetLayeredWindowAttributes (hWnd, RGB (255, 255, 255), 0, 1);:: redrawwindow (hwnd, null, null, rdw_ered, rdw_invalidate | rdw_frame | rdw_allchildren); Summary This article This article is very simple to implement the form of the form by using the new API function setLayeredWindowAttributes () for Windows 2000. The creation of the form. In addition to the setlayeredWindowAttributes () function, Windows 2000 also provides many other similar special effects functions such as AnimateWindow () for form dynamic sliding display. Since these functions are the API function provided by the system, it will not be restricted by the programming language, that is, the above effects can be achieved without only VC . In other development environments such as C Builder, Delphi, it is also possible to be implemented in a similar approach. The program code described herein is compiled by Microsoft Visual C 6.0 under Windows 2000 PROFESSIONAL.