Use the desktop window manager to create aero glass Effects

Source: Internet
Author: User
Tags old windows
[Switch] use the desktop window manager to create the aero glass Effect

CodeDownload location: Dwm2007_04.exe (166kb) (to use this code properly, you should use Vista with Aero effect)

DWM is a new interface used to manage how to merge various running and rendered windows to the Windows Vista desktop. WindowsPresentation Foundation (WPF) provides a more advanced layer that controls the presentation to the desktop layer. Windows display driver model (wddm) is used to process the actual low-level presentation of the display. This article only discusses how to use the DWM interface. For more information about this topic, read the msdnArticle"Windows Presentation Foundation Introduction" (msdn2.microsoft.com/aa663364.aspx ). Wddm is the only content of this article, because it can achieve new results through the DWM interface and fix some specific problems.

DWM technology Overview

The DWM interface is provided in all Windows Vista versions except Windows Vista Home Basic, which can be started through dwm.exe. All applications in the systemProgramYou can benefit from DWM without modifying or re-compiling. However, applications that use specific DWM functions can call the interfaces in dwmapi. dll (the common interfaces of DWM) and then pass these interfaces to dwm.exe. The interface declaration can be found in dwmapi. h, and the latest API information can be obtained online from windowssdk.msdn.microsoft.com.

Windows Vista is designed to use a graphical accelerator for each window, rather than just for 3D DirectXApplication. To achieve this, DWM needs to communicate with wddm, which is the final owner of the graphics processor and video memory. (DWM depends on milcore. dll, which is a component shared with the Windows Presentation Foundation for output and presentation to DirectX .) The rendering operation is handled by a separate thread in DWM, and users with DirectX devices cannot access it. In addition, most applications have threads for processing rendering operations and UIS (such as typical Win32The user message pop-up thread in the application), but it does not conflict with the DWM rendering thread. DWM obtains a window list, manages its bitmap in the tree structure, and combines it to the final desktop. In other words, each application presents its own bitmap, which is then combined by DWM.

The main window thread of the application presents its scenario. The DWM rendering thread accesses the scenario and updates the desktop through its DirectX interface. The transferred information is compressed to only previous changes (differences), and large data (images) are placed in the shared memory. This allows scenarios to be generated on one computer, and the final rendering operation can be completed on another computer. 3D programmers who have used OpenGL will be very familiar with this architecture. It allows servers to manage 3D scenarios and only send differences to client computers. You can present 3D scenarios in a distributed manner and enable them to run on any number of client computers with Full Hardware acceleration. This architecture enables DWM to provide level-1 Support for Remote Desktop solutions.

Although Windows Vista will run together with the old Windows XP compatible driver, the wddm video driver needs to be used for all DWM functions. Contrary to some assumptions, DWM does not require DirectX 10, but it does need more video/texture memory and video cards that support the Shader Model 2.0 or higher. The biggest change with wddm is that it introduces video memory manager (vidmm), which can swap the video memory distribution between the system memory and the video memory. This means that wddm can Virtualize video card resources, so it can do better in terms of video memory sharing and switching and context switching between different threads of different applications for graphics processors. In the past, it was almost impossible to run multiple 3D applications stably because the driver could not handle context switching. In addition, there is no formal plan available before the emergence of wddm, so a DirectX application is usually used up the resources of other applications. However, it is difficult to use wddm. Windows Vista has much stricter control over drivers and requires more robust and reliable drivers than earlier Windows versions.

Note that DirectX 10 is an API for Windows Vista only. Applications designed for earlier versions of DirectX will run on the earlier version of DirectX API implementation (expected to be called DirectX 9 L. This will be the last version supported by the driver before wddm. DirectX 9 L applications are expected to run on Windows XP with DirectX 9 L installed and Windows Vista. DirectX 10 does not contain legacy interfaces.

Benefits of combining desktops

All of these new subsystems allow you to present windows independently and perform the combined steps before presenting them to the desktop. This can be well utilized in Windows Vista and the following update applications. In this regard, I will show you two ways to use aero glass effects and thumbnails. The glass effect is only available when the aero glass solution is run and the combination function is enabled. Aero basic does not provide this effect.

Since each window is created in its own video memory area, DWM needs to display the final combination of the window to the desktop. This means that DWM can access the images on the desktop and mix them with your window rendering to create a combination of the two. This is evident when mixing with existing desktop images to create Frost glass effects in any window area. Since each window is displayed on the outer surface of the middle screen, this means that DWM is the only program involved in updating the glass effect. You do not need to invalidate any basic window when moving a window that uses the glass effect. DWM processes the update of translucent visual images to new coordinates. You can instruct DWM to add some client areas for the window to display the glass effect, so that you can create a glass area for your own use.

This combination of off-screen desktop makes desktop response faster. Since each window is now presented separately from the desktop, it eliminates the common problems you encounter in slow updates of applications (such as Web browsers. In earlier versions of Windows,Figure 2The displayed content is no stranger. This tear occurs when you move the window back and forth at the top of another application, because the update speed of the following window is too slow. After the desktop combination, you will not see this phenomenon again.


Figure 2 Tear caused by slow rendering (click the image to get a smaller view)
Figure 2 Tear caused by slow rendering (click the image to get a larger view)

Applications released along with Windows Vista use this function to present the glass effect to the client area. One of the best examples is Windows Media.Player. This program extends the framework to the bottom of the client region. It draws custom controls in this region. The minimum version looks likeFigure 3Similar.


Figure 3 Glass effect in Windows Media Player (click the image to get a smaller view)
Figure 3 Windows Media Player glass effect (click the image to get a large view)

When I move the window back and forth on my desktop, the image displayed through the control is composed of the desktop part under the player. If you move the window to an animation, you can see the animation through the window above! This is the combination of desktop functions.

If the combination effect is disabled, only the "AURORA" effect is obtained in the window, and the window will show an opaque default window color, as shown in figureFigure 4.


Figure 4 Disable Windows Media Player for glass effect (click the image to get a smaller view)
Figure 4 Disable Windows Media Player for glass effect (click the image to get a larger view)

In essence, the aero basic and aero glass interfaces are the watershed between the previous and new standards of dwm ui. The aero basic interface provides the same API for the program to maintain backward compatibility. However, running the basic interface means that you are using the old window manager and DWM is not active. The old interface means that the UI layout will be as expected by the program written before Windows Vista. DWM controls the aero glass interface and limits access to it. If the application enters a non-client area (glass frame), DWM detects it and switches to the aero basic framework.

In Windows Vista, other new features provided by combining desktops include the new windows flip (Alt + TAB) and flip 3D (Alt + Windows key) task switches. Flip 3D is particularly interesting because its functionality depends on some code in DWM, which gets each top-level window in the scene graph and presents it to a series of distorted windows, you can scroll through these windows with a keyboard or mouse (seeFigure 1).

DWM controls the interaction between windows and the desktop composite engine. To integrate your program into the DWM function, you need to understand how DWM works and how it interacts with it. Besides some other functions, DWM has four main functional areas in its public API:

    • Basic desktop combination settings
    • Display glass effect in client window
    • Thumbnail Rendering
    • Adjust the rendering effect to interact with multimedia programs

I will describe the first three parts in this article. The last part is provided for DirectX and video playback applications. Because DWM runs asynchronously, if you do not strictly control it, sampling distortion may occur.

Some common composite Functions

If you want to use the desktop combination function in the program, you need to query and set various DWM parameters. For example, if some applications switch to full-screen display, then DWM closes the combination and displays it in an opaque desktop background color, your application should identify this situation and disable the combination-specific features. Below are some basic functions that integrate your program with DWM:

DwmenablecompositionEnable or disable the DWM combination. DWM will keep this setting in the current process or until it is reset. Changing the settings will cause a wm_dwmcompositionchanged notification. Most applications do not need to call this function, but you may need to monitor windows final messages.

DwmiscompositionenabledObtain the status of the DWM combination enabled on the desktop.

DwmsetaskwattributeSet the value of the specified DWM attribute for the window to control how to handle the DWM transition, whether non-client rendering is allowed, and how the flip 3D will process the window. For example, If you disable non-client rendering for a window, the requirement to extend the framework later or blur the content behind the window will fail.

Dwmget?wattri=Retrieves the current value of the specified dwm?wattri=for the specified window.

DwmgetcolorizationcolorRetrieves the current color of the DWM glass combination. This value is based on the current color scheme. Changing this setting will cause a wm_wmcolorizationcolorchanged notification.

DwmdefwindowprocWhen wm_nchittest is used for calling, and when wm_nccalcsize and similar messages need to be processed due to the expansion of the client framework, DWM hit test is located in a non-client region.

Presenting glass in your program is quite simple. DWM provides two functions for this purpose:

DwmextendframeworkclientareaA simple function that extends the edge of a non-client framework to a window.

DwmenableblurbehindwindowA more complex function that provides more control over the display of glass effects.

Since all the combined windows are presented to an out-of-screen window through DWM and then combined and then presented to the desktop, it is not difficult to get these images and provide real-time thumbnail representation of the application. DWM provides you with four functions to control how thumbnails are rendered:

DwmquerythumbnailsourcesizeReturns the original size of the DWM thumbnail.

DwmregisterthumbnailCreate a thumbnail relationship between the target window and the source window.

DwmunregisterthumbnailDelete the DWM thumbnail relationship created by dwmregisterthumbnail.

DwmupdatethumbnailpropertiesUpdates the attributes of a specified thumbnail.

DWM provides five functions to fine-tune the DWM rendering method, but these functions are beyond the scope of this article.

Prepare aero glass

To program the DWM interface, run Windows Vista that displays aero glass. Although the most convenient method is to use C ++ code to call these new functions, if possible, I like to use C # To write user interface code. All the code in this article is written in C #, but this means that you must undergo a severe test. To use the functions discussed in this article, you must either use the links in C ++ and the correct library, or use C # To compile P/invoke packaging for functions and structures. The download section of this article contains a library that provides packaging of functions and structures required by DWM, so you can call it from the C # program. Basically, this is just a set of instructions on the dwmapi. dll download interface. To use the DWM function used in this article for glass effects and thumbnails, you need to create the C # declaration of the DWM function and data structure. The Declaration I created for this article is similar to Figure 5.

If you are using C #, you need to create a declaration similar to this type in your code. Then, if you are running Windows Vista, you can call DWM. Of course, you should not just assume that your application is running on Windows Vista. For the sake of insurance, you need to confirm that environment. osversion. version. Major is at least 6.0. Alternatively, you can catch exceptions caused by an attempt to call a non-existent function through P/invoke.

If you want to use the glass effect, the computer you use must meet three requirements. First, run premium, business, or ultimate for Windows Vista. Secondly, the hardware should be able to run the aero interface (for details, see microsoft.com/windowsvista/getready/capable.mspx ). Finally, you need to select the Windows aero color scheme in Windows Vista. Exercise caution when using this operation, because excessive use of this effect may cause a burden on the computer's GPU.


Figure 6 Window color and appearance options (click the image to get a smaller view)
Figure 6 Window color and appearance options (click the image to get a larger view)

Enable the aero solution by opening the "personalized" control panel and clicking the "window color and appearance" option. On this screen (seeFigure 6), Make sure that the "enable transparency" option is selected, and then click the "open traditional style appearance attribute" link. Click windows aero under "Color Scheme" in the "appearance Settings" dialog box (seeFigure 7). After you click "OK", the aero interface and glass effect are displayed. If you want to, you can also customize the window color and opacity level.


Figure 7 Windows aero glass effect (click the image to get a smaller view)
Figure 7 Windows aero glass effect (click the image to get a large view)

Programming DWM

By calling dwmiscompositionenabled, you can check whether the aero solution is enabled in your program. However, note that not only can users change the current aero scheme at any time, but other applications can also enable or disable this scheme programmatically. Therefore, the result of checking this function once may be unreliable.

The dwmenablecomposition function enables or disables the aero solution. For example, if your application may encounter compatibility problems, you may need to disable the combination when the application is running (if you are writing a full-screen DirectX dedicated application, will automatically disable the combination ). This setting is only maintained during the process of setting it. When the process ends, the combination flag is reset to its initial value. In general, unless due to application compatibility, the application should not use this setting, but should be made by the system or user.

When the status of the desktop combination changes, the wm_dwmcompositionchanged message is broadcasted. You cannot determine whether a combination is enabled or disabled through parameters. Therefore, if you are interested, you can call dwmiscompositionenabled by yourself. The code for executing the check is very simple. The difficulty is how to determine the appearance of the window when the combination is disabled.

 
// Check to see if composition is enabledif (dwmiscompositionenabled () {// enable glass rendering} else {// fallback rendering}

Finally, even if the aero solution is enabled, the user may have changed the glass color and made the combination effect opaque. I have compiled a small application that can be used to create a window with full glass effects, and then I changed the glass properties in the control panel (seeFigure 8). The default settings of the color and transparency of the first image display window. Then I closed the opacity and left an opacity window. I changed to the red window color and the default transparency, from which you can still identify some underlying window images. In an opaque window that is large enough, you will observe the highlighted area placed in the glass display, which is the twill Aurora effect visible on the window.


Figure 8a Change color and transparency
Figure 8b
Figure 8C

You can call the dwmgetcolorizationcolor function to view the combination color and opacity. If this function is successful, it sets a GDI + argb color value and a Boolean value, indicating whether the color is opaque. Just like changing the aero scheme in the control panel, a message is broadcast when the combination color changes. In this case, wm_dwmcolorizationcolorchanged will be sent, but in this case, the parameter will tell you what the new color and opacity are.

Protected override void wndproc (ref message MSG) {Switch (MSG. MSG) {Case wm_dwmcolorizationcolorchanged: // The color format of currcolor is 0x20.rggbb. uint currcolor = (uint) MSG. wparam. toint32 (); bool opacityblend = (MSG. lparam. toint32 ()! = 0);... break ;}}

When you change the DWM rendering for a non-client region, the wm_dwmncrenderingchanged message is sent. If DWM is enabled for non-client rendering, wparam is true. When the maximum or non-maximum DWM combination window receives the wm_dwmwindowmaximizedchange message, you will also be notified. If the window has been maximized, wparam is true.

We have introduced two functions. You can use these two functions to provide glass effects for your program. The first one is dwmextendframeworkclientarea. Windows using the aero scheme have glass effect at the boundary of the title bar and window edge (essentially all non-client areas of the window ). This function allows you to extend the display to each side of the non-client area of the client area and present it with glass effect. In other words, you can seamlessly extend the top, left, right, and bottom sides of the glass window frame to your window.

The second function is dwmenableblurbehindwindow, which allows you to display glass effects in any shape on the edge and specify more parameters to better control the rendering effect. However, I think most users who use glass effects only extend the glass effect from the edge to the client area. No matter which function is used, you need to closely track the combination status to see if glass effects should be enabled during rendering. This means that four wm_dwm * messages need to be tracked, or dwmiscompositionenabled is called to understand whether to disable the glass effect for rendering or when it is turned on.

First, let's take a look at the simple call. This function is designed for frameless windows (such as the taskbar, sidebar, tablet Pen Input window, and "start" menu); no behavior on the frame window is defined.

The dwmexico tendframeworkclientarea function uses the window handle and margins structure. A window handle is a window in which the framework extends from the edge to the client area. You need to set the margins structure containing the number of shards to extend the framework to the client. The C # Implementation 5 of the margins structure is shown in.

At the beginning, you may be confused because no other Win32 function works similarly, but basically you can control each edge independently from other functions. Select the edge you want to expand and specify the depth of the effect to be presented in the client region (seeFigure 9). To expand multiple edges, they can overlap. If you want to dynamically track the window size for this effect, you must call the dwmextendframeworkintoclientarea function each time you change the window size. One special case is to set one or more margins to-1, which will extend the glass effect to the entire window. To reset the margin, you only need to set all the margin values to 0, and call dwmextendframeworkintoclientarea again.


Figure 9 Glass margin in the client area (click the image to get a smaller view)
Figure 9 Glass margin in the client area (click the image to get a larger view)

What if you do not want to extend the glass effect from the frame to the client area? The dwmenableblurbehindwindow function gives you better control over how to add glass effects to Windows. Similarly, this function uses the window handle of the window you want to add the glass effect to, but it also uses the dwm_blurrbehind structure, so that you can set various parameters on how to use the blur effect on the window. The most important parameter is the region parameter, which is a GDI term used to describe any shape area typically composed of a series of straight lines and curves.

The dwm_blurrbehind structure (5) contains parameters that control how to display the blur effect.

If you want to enable the glass effect in the client area, you can set the fenable flag to true. To disable the flag, set the flag to false. The hrgnblur parameter is the handle of the area you created to display the glass effect. Just like setting the margin value to-1 in the DWM-extendframeworkclientarea, setting the hrgnblur parameter to null in the fuzzy structure will notify DWM to apply the glass effect to the entire window.

You may misunderstand the last parameter ftransitiononmaximized. Because the glass effect is disabled in the maximized window, you will think this flag is related to this. This flag actually controls whether to transition to the maximized color when there is a maximized window on the desktop. Unfortunately, if you set this parameter to true, then when the window is displayed, it is not a transparent area, but only an aurora effect.

You can use the dwflags parameter to tell the interface which parameters you want to set. When you want to set a parameter, you need to open the corresponding bit in the dwflags parameter. This is consistent throughout the DWM interface.

Do not forget to present the glass color to this area. When you create a glass effect in a specified area, it is better to use the same black paint brush as that in dwmextendframeworkclientarea.

Obviously, DWM provides you with two interfaces. A complicated interface allows you to build areas of any shape, A simple interface only allows you to extend the glass effect of the window frame to your client area, so that you can draw additional controls and similar content as part of a title bar. In both cases, it is worrying that when you re-resize the window and update the area showing the glass effect (if this area is not the entire client area, what happens to the shape of a region. TheSource codeAllows you to set an extended client framework or region and switch the combination flag.

Draw on the glass Area

Some tips are required to use the glass effect as the background in the window. If you present anything that is not transparent to you (such as the GDI function), your project is displayed in the glass area, but sometimes with an exception. If you want to actually mix the presentation into the glass interface, you need to use the features that can use the Alpha color pipeline, such as GDI +, Windows Presentation Foundation, or Windows XP theme API.

A special problem is that the bitwise Mode 0x00000000 is used to present the GDI project in black, and the completely transparent black will also happen when the Alpha pipeline is used. This means that if you draw with a black GDI paint brush or pen, the color will be transparent, not black. This problem is especially evident when you try to use the default text color to control text labels located in the glass area. Because the default text color is usually black, DWM considers it transparent, so the text will be incorrectly written into the glass area.Figure 10This example is displayed. The first line is written using GDI +, and the second line is a text label control using the default color. It can be seen that the content is almost unrecognizable, because it is actually an incorrectly rendered file, and the text is displayed in gray rather than black.


Figure 10 Transparent dialog box

It is gratifying that this problem can be solved in many ways. One of the methods is to use the control described by the owner. Another method is to present a bitmap with an Alpha pipeline. But the simplest way to control text is to make. NET Framework 2.0 use GDI + for you. You can easily do this by setting the usecompatibletextrendering attribute on your control. By default, this attribute is set to false. In this way, controls written for previous versions of the. NET Framework are displayed in the same way. However, if you set it to true, the text will be correctly rendered. You can use application. setusecompatibletextrenderingdefault to set this attribute globally. If you are using Visual Studio2005, the template code will include a call to set the compatible text rendering to false in the main routine before creating the form. You can edit it and set it to true (as shown below). When writing in the window, all controls will look correct.

 
Static void main () {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (true); application. Run (New glassform ());}

You can go to Miguel. lacouture found details about this content and the use of the textrenderer class in the build world-ready apps using complex scripts in Windows Forms controls article published in The msdn magazine in March 2006.

The glass effect should be enabled before the display window starts. The combination engine will view the Alpha value of your window and apply the blur effect to the transparent area. This may cause problems when using some GDI functions because these functions do not retain the Alpha value. You can use GDI + when needed, but be cautious when using it, Because GDI + calls are presented in software rather than on hardware, therefore, when the Window refresh frequency is high, using GDI + call may consume a large amount of system resources.

You can use the same method to obtain the glass effect in the DirectX application. All you need to do is to control the Alpha value of the rendering target and use one of the two DWM functions that enable the glass effect. Wherever it indicates that DWM uses the glass effect, it uses the Alpha value of the rendering target. The rendering target should be opaque no matter where it is; otherwise, undefined behavior will be obtained.

Thumbnail

A thumbnail is a real-time display window displayed by DWM in an opened application. Thumbnails are used by flip and ALT + TAB task switches. Essentially, you can request a thumbnail of an application window and make it appear in the application. The thumbnail API provides you with real-time representation of the application window.

Thumbnails are easy to use, because Windows has done a lot of hard work for you. The difficulty lies in obtaining the hwnd of the application. After obtaining the required hwnd, you only need to register a thumbnail and associate the hwnd with the hwnd to be rendered and the position in the window. At this time, the operating system will be responsible for future updates. Whenever the source window changes, the changes are reflected in the target window.

To use a thumbnail, you must first use the dwmregisterthumbnail function to register the thumbnail. You provide two window handles: The Source hwnd (that is, the window to which the thumbnail view is needed) and the target hwnd (the window to which the thumbnail is to be rendered ). After the thumbnail is used, you need to call dwmunregisterthumbnail to let DWM know that the link has ended. After a thumbnail is created, the dwmregisterthumbnail function returns a thumbnail handle, which is used as a parameter by all other thumbnail functions. After registering a thumbnail, you must call dwmupdatethumbnailproperties to update the thumbnail. Figure 11 shows the sample code of the form that presents the Real-Time thumbnail of another window.

In addition to registering and canceling the thumbnails, two other functions are used with the thumbnails. Dwmquerythumbnailsourcesize returns the original size of the specified thumbnail. Dwmupdatethumbnailproperties allows you to update the attributes of a given DWM thumbnail. It adopts the dwm_thumbnail_properties structure, and its C # implementation is shown in 5.

When you do not want to use the entire source window, you can use the dwm_thumbnail_properties structure to specify the number of attributes, such as the target rectangle (rcdestination member) in the target window) and the rectangular area of the source window to be used (rcsource member ).

If you do not want to make the thumbnail completely opaque, you can also use the opacity member to specify the opacity of the thumbnail, where 0 is transparent and 255 is opaque. If you want the thumbnail to be invisible, you can set the fvisible flag to false. If you only want to use the client area of the thumbnail window, instead of the entire source window (including non-client areas, such as the Framework and title bar), you only need to set the fsourceclientareaonly Boolean value to true. You can use the dwflags parameter to tell the interface which parameters to set. When setting a parameter, You need to enable the corresponding bit in the dwflags parameter.

Finally, there is no limit on the size of the target window, for example, the thumbnail interface is usually used to expand or contract the source window. There are limits on the aspect ratio. Always maintain the aspect ratio of the source window. If the size of the source window is changed, the thumbnail size is also changed to keep itself within the specified boundary.

A button is provided in the source code included in this article. Using this button, you can create a micro-real-time thumbnail of the main application window, as shown in figureFigure 12.

You can easily find that the thumbnail is displayed in real time. If you change the main application window, you will see a thumbnail update. You will find that you can easily use thumbnails and the findwindow and getwindow Win32 functions to create your own task switches with a little bit of wisdom.


Figure 12a Create real-time thumbnails Figure 12B

From http://msdn.microsoft.com/msdnmag/issues/07/04/Aero/default.aspx? Loc = zh]

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.