Win2000 programming implements form transparent effects

xiaoxiao2021-03-06  88

Win2000 programming implements form transparent effects

Abstract: This article mainly introduces the new API function setLayeredWindowAttributes () under Windows 2000, and the general method of implementing form transparent effects through this function, and combining the code gives a specific programming implementation process.

Keywords: form effects; hierarchical windows

introduction

Windows 2000 operating system has a lot of improvement and improvement in quality or in the user interface, users are more simple, convenient, and feel more comfortable to the operation of the system. 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.

Layered window

Before introducing the hierarchical window function, first introduce the WS_EX_LAYERED extension window style. 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.

Implementation of form translucent effects

The layered window has been described in detail in front. 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 Window

ColorRef CRKEY, / / ​​SPECIFIES THE Color Key

Byte Balpha, // Value for the Blend Function

DWORD DWFLAGS // Action

);

The dwflags has two settings of LWFLAGS (value 2) and LWA_COLORKEY (value 1), if LWA_alpha is set, specify a 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, and the removed area will no longer be clicked, and other colors are normal. 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 () can set the form to a half transparent by the setLayeredWindowAttributes () function:

// Global variable

Typedef Bool (WinAPI * LPFN) (HWND HWND, ColorRef Cr, Byte Balpha, DWORD DWFLAGS);

LPFN G_PsetLayeredWindowAttribute;

......

// Get the pointer in the function setlayeredWindowAttribute in user32.dll

HModule huser32 = getModuleHandle (_T ("User32.dll")); g_psetlayeredwindowattributes = (lpfn) getProcaddress (HUSER32, "SetlayeredWindowAttributes);

IF (g_psetlayeredWindowAttributes == null)

:: PostquitMessage (0);

This code typically runs when the program is initialized and loads the USER32.DLL module to memory and gets the setlayeredWindowAttributes () function pointer. You need to release the previously loaded module before the program exits:

// Uninstall module

IF (M_HUSER32! = NULL)

Freelibrary (m_huser32);

When setting the form to a 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, you cannot call SetLayeredWindowAttributes (), you need to add a WS_EX_LAYERED extension style on GetWindowlong () after getting the current window style, and set it through the setWindowlong () function:

// translucent

HWND HWND = GetSafehWnd ();

Long lwindowlong = getwindowlong (hwnd, gwl_exstyle) | ws_ex_layred;

:: setwindowlong (hwnd, gwl_exstyle, lwindowl);

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 interaction form

It is very simple to use setlayeredWindowAttributes () to create a shaped form. The specific process is extremely similar to the translucent form, which is only set to LWA_COLORKEY and specify key colors that require transparent display:

/ / Specify key colors

HWND HWND = GetSafehWnd ();

Long lwindowlong = getwindowlong (hwnd, gwl_exstyle) | ws_ex_layred;

:: setwindowlong (hwnd, gwl_exstyle, lwindowl);

G_psetlayeredWindowAttributes (HWND, RGB (255, 255, 255), 0, 1);

:: RedRawwindow (Hwnd, Null, Null, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

summary

转载请注明原文地址:https://www.9cbs.com/read-106139.html

New Post(0)