User32.dll setlayeredwindowattributes Function

Source: Internet
Author: User

Setlayeredwindowattributes function:
Hwnd is the handle of a transparent form,
Crkey is the color value,
Balpha is transparency and the value range is [0,255].
Dwflags is transparent and can take two values:
When the value is lwa_alpha, The crkey parameter is invalid and the balpha parameter is valid;
When the value is lwa_colorkey, The balpha parameter is valid and all the areas in the form where the color is crkey will become transparent.
Lwa_alpha = 0x2 lwa_colorkey = 0x1
To make the form transparent, you must first have the ws_ex_layered extension attribute (the old SDK does not define this attribute, so you can directly specify it as 0x80000). ws_ex_layered = 0x80000

The Windows 2000 operating system has greatly improved both in terms of quality and on the user interface. The user's operations on the system have become simpler, more convenient, and more comfortable.

. If you pay attention to it during use, it is not difficult to find some new features on the interface. For example, Windows 2000 provides support for the shadow Alpha hybrid cursor and menus.

Supports the fade-in and fade-out effects of the prompt box, and the transparent effects such as Alpha hybrid film when dragging the shell. All these special effects seem different, but they are all updated on Windows 2000.

Implemented by calling a hierarchical window API function. Since it is an API function provided by Windows 2000, it means that this function can be implemented in your own applications.

Number of special effects supported.

Sometimes some minor improvements are enough to enhance the Display Effect of the user interface during programming. For example, a cursor with a mix of Alpha results is obviously better than a normal one.

It feels much better, especially in a Large Screen Display or multi-display system. Furthermore, the screen size of the monitor is limited. If you want to view multiple

The window content is obviously not convenient. Although the Large Screen Display or multi-display system can alleviate the problem to some extent, it is far better to set these application forms to transparent or translucent.

Complete and convenient. Although the two applications shown are overwritten, the program form on the foreground obviously does not affect the display of the background Word document content. Given that the hierarchical Window Function

In order to fully explore its potential and apply it to programming.

 
Layered window

Before introducing the hierarchical window function, you must first introduce the ws_ex_layered extended window style. This window style is also added in Windows 2000. If this attribute is used

The body will have visual effects such as composite shapes, animations, and Alpha blends.

The window appears as a rectangle on the screen by being cropped by another window. To implement a circular form, it is not enough to simply draw a circular form.

Click to detect the object according to the original rectangle, and the window located under the form will still be cropped by the rectangle of the form. You may consider obtaining the visualization under the rectangle of the form before the garden form is displayed.

Region snapshot, and draw it to the current form after the form is displayed later. However, because other windows can draw the area covered by the form, the foreground program cannot know that the painting will

This solution does not work properly in multi-process and multi-task environments because it does not obtain new snapshots in the following visible regions in a timely manner. In this case

In Windows 95/98 and Windows NT 4.0, the correct method is to use the setjavaswrgn () API function to identify the desired form shape, but this processing is frequently changing the form shape or

There are still defects when dragging on the screen: the front-end form requires that the window located under the screen be painted throughout the area, which will produce too much messages and computing workload. In addition, setjavaswrgn () can only be used.

The form is completely transparent, and the translucent effect cannot be achieved. Perhaps this is why the hierarchical window is proposed.

Layered windows actually implement two completely different concepts: layered and redirected. To remove any layer, the ws_ex_layered flag must be set.

You can also call setwindowlong () with the gwl_exstyle flag after the creation to set the settings. Next, you can use the updatelayeredwindows () function to update

Layered window. In specific use, you need to plot the visible area in the bitmap, and provide it with key colors, Alpha parameters, and so on to the updatelayeredwindows () function. Yes

Note that when using the updatelayeredwindows () function, the application does not need to respond to wm_paint or other draw messages.

In addition, you can also use the traditional Win32 rendering mechanism to implement this, which requires calling another API function setlayeredwindowattributes () to complete the key color or alpha

Set the mixed parameter value. Once this function is called, the system starts to redirect all the plots for the layered window and automatically apply the specified special effects.

Implementation of form translucent Special Effects

The preceding section describes the hierarchical window in detail. Here we will introduce the setlayeredwindowattributes () function and its usage, and implement it on this basis.

Form translucent effect. The following figure shows the setlayeredwindowattributes () prototype:

Bool setlayeredwindowattributes (
Hwnd, // handle to the layered window transparent form
Colorref crkey, // specifies the Color Key color value
Byte balpha, // value for the blend function transparency, value range: [0,255]
DWORD dwflags // The transparent mode of action. Two values can be taken:
When the value is lwa_alpha, The crkey parameter is invalid and the balpha parameter is valid;
When the value is lwa_colorkey, The balpha parameter is valid, and all the areas in the form where the color is crkey will become transparent.
Lwa_alpha = 0x2
Lwa_colorkey = 0x1
);
Dwflags has two settings: lwa_alpha (value: 2) and lwa_colorkey (value: 1). If lwa_alpha is set, specify the transparency of the form through the balpha parameter;

If the lwa_colorkey flag is set, specify the key color as the crkey. the area covered by the color will be removed from the form, and the removed area will not be clicked for detection. Other colors

It is displayed normally. If the Alpha hybrid value is set to 0, click detection is not performed in the window area.

As mentioned above, the setlayeredwindowattributes () function is a new API function in Windows 2000, and its module is user32.dll. Use getmodulehandle

() Load the user32.dll module and call getprocaddress () to obtain the setlayeredwindowattributes () pointer in user32.dll.

The setlayeredwindowattributes () function sets the form to translucent:

// Global variable
Typedef bool (winapi * lpfn) (hwnd, colorref Cr, byte balpha, DWORD dwflags );
Lpfn g_psetlayeredwindowattributes;
......
// Obtain the pointer of setlayeredwindowattributes in user32.dll.
Hmodule huser32 = getmodulehandle (_ T ("user32.dll "));
G_psetlayeredwindowattributes = (lpfn) getprocaddress (huser32, "setlayeredwindowattributes ");
If (g_psetlayeredwindowattributes = NULL)
: Postquitmessage (0 );
This Code usually runs during program initialization, loads the user32.dll module to the memory, and obtains the setlayeredwindowattributes () function pointer. Exit the program

Previously, you must use the freelibrary () function to release the previously loaded modules:

// Uninstall the module
If (m_huser32! = NULL)
Freelibrary (m_huser32 );
When you set the form to translucent, you must first obtain the window handle of the form to specify which window to operate. If you set a window in this application

Can be dynamically obtained by passing the window handle or using getsafehwnd. If you want to set a program window outside the program, the general approach is (

) Function to obtain the window handle of the specified window title. After obtaining the window handle, you cannot directly call setlayeredwindowattributes () to set it.

After getwindowlong () gets the current window style settings, add the ws_ex_layered extended style on the basis of the settings, and set it through the setwindowlong () function:

// Translucent
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 );
To use this function, you must add a pre-defined statement before the preceding statement:

# Define ws_ex_layered 0x00080000
Special form Effects

Using setlayeredwindowattributes () to create a special-shaped form is very simple. The specific process is very similar to the implementation of a translucent form. The difference is that the flag is set

Lwa_colorkey and specify the key colors to be transparent:

// Specify the key color
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_erase | rdw_invalidate | rdw_frame | rdw_allchildren );
Summary

In this article, the setlayeredwindowattributes () API function is added to Windows 2000 to easily implement the semi-transparent settings of the form and the creation of a special form. Division

The setlayeredwindowattributes () function is provided. Windows 2000 also provides many similar special effects functions, such as the animation window () used for Dynamic Sliding display of forms ().

Because these functions are the API functions provided by the system, they are not restricted by programming languages. That is to say, they can not only be implemented in VC ++. In C ++ builder, Delphi, etc.

It can also be implemented in a similar way in the development environment. The program code described in this article is compiled by Microsoft Visual C ++ 2000 under Windows 6.0 professional

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.