Use DWM to implement aero glass

Source: Internet
Author: User

Since Windows Vista, the aero glass effect has been applied to systems above Home Premium (Home Basic does not have this effect ). This effect is controlled by DWM (desktop window manager. For general programs, this effect will be applied in the window border by default. But if we want more control, for example, to make a part of the customer area present this effect, it is also very simple. We don't need to do any complicated algorithms in the program. We just need to call the API and hand it over to DWM.

I. Composition (window merging) and non-client rendering (non-customer area rendering)

Non-customer areas usually include the window title bar and window border. By default, non-customer zones are rendered as frosted glass, also known as compostion. There are several functions that can control the rendering mode of the system and the current window. There are also Windows messages used to accept changes in the rendering mode.

1. Check whether aero glass is enabled. Use
Function
Dwmiscompositionenabled
Check whether the system has enabled the aero glass special effect. It accepts a bool parameter and stores the current status. Function prototype:
Hresult dwmiscompositionenabled (bool *Pfenabled
);

2. enable/disable aero glass. Use FunctionsDwmenablecomposition
Enable or disable the system aero glass effect and pass inDwm_ec_enablecomposition
Enable and pass inDwm_ec_disablecomposition
Disable.

3. enable/disable non-customer rendering of the current window. FunctionDwmsetaskwattribute
Used to set window properties and PropertiesDwmwa_ncrendering_policy
Determines whether the current window is rendered in a non-customer area.Dwmncrp_enabled
Enable,Dwmncrp_disabled
Disable. When the system's Aero glass is disabled, the setting is invalid. Use the FunctionDwmget?wattri=
Checks the properties of the current window.

4. enable or disable the response system aero glass. Windows sends a message when aero glass is enabled or disabled.Wm_dwmcompositionchanged
,
Use
Function
Dwmiscompositionenabled

Detection Status.

5. enable or disable non-client rendering in the response window. When the non-client rendering of the current window is enabled or disabled, Windows will send a messageWm_dwmncrenderingchanged
,Wparam
Indicates the current status.

2. Transition and colorizationcolor)

Transition controls whether to display the minimization and restoration of windows in an animation mode. By using functionsDwmsetaskwattribute
, Set PropertiesDwmwa_transitions_forcedisabled
To enable or disable window animation. This setting is only valid for the current window.

When you modify the theme color through the control panel, Windows sends a messageWm_dwmcolorizationcolorchanged
, Through functions in the programDwmgetcolorizationcolor
Obtain the color of the current topic and whether it is transparent. By responding to color changes, the color style of the program can change with the theme style.

Iii. Enable aero glass effect in the customer Region

FunctionDwmenableblurbehindwindow
Enable the aero glass effect in the customer zone. The first parameter is the window handle, and the second parameter isDwm_blurbehind
Structure. WhereFenable
Set whether to enable the glass effect in the customer zone.Hrgnblur
Set the glass effect area. If this parameter is set to null, the entire customer area will display the glass effect. If it is set to a correct area, the area will display the glass effect, outside the region is completely transparent. To display the transparent effect, the original color of the customer area must be black.Wm_paint
Draw the customer area in the message. The following code uses GDI +. When aero glass is enabled, the entire window is painted black, and aero glass is dimmed when it is disabled:

Case wm_paint: <br/>{< br/> paintstruct pS; <br/> HDC = beginpaint (hwnd, & PS ); <br/> // do not directly use the window handle to create graphics, which will cause flickering <br/> graphics graph (HDC ); <br/> // clear the customer region <br/> rect rcclient; <br/> getclientrect (hwnd, & rcclient); <br/> bool bcompenabled; <br/> dwmiscompositionenabled (& bcompenabled); <br/> solidbrush Br (bcompenabled? Color: Black: Color: darkgray); <br/> graph. fillrectangle (& Br, rect (rcclient. left, rcclient. top, <br/> rcclient. right, rcclient. bottom); <br/> endpaint (hwnd, & PS); <br/>}< br/> break;

The initialization and shutdown of GDI + are still required:

// Initialize GDI + <br/> ulong_ptr token; <br/> gdiplusstartupinput input; <br/> gdiplusstartup (& token, & input, null ); <br/> // ******************************* <br/> // close GDI + <br/> gdiplusshutdown (token );

The following code sets the entire customer zone to a glass effect:

Dwm_blurbehind BB = {0}; <br/> BB. dwflags = dwm_bb_enable | dwm_bb_blurregion; <br/> BB. fenable = true; <br/> BB. hrgnblur = NULL; <br/> dwmenableblurbehindwindow (hwnd, & bb );

The following code sets an elliptical area in the customer Zone center to a glass effect:

Rect; <br/> getwindowrect (hwnd, & rect); <br/> int width = 300, Height = 200; <br/> // center oval <br/> hrgn = createellipticrgn (rect. right-rect. left)/2-width/2, <br/> (rect. bottom-rect. top)/2-height/2, (rect. right-rect. left)/2 + width/2, <br/> (rect. bottom-rect. top)/2 + height/2); <br/> dwm_blurbehind BB = {0}; <br/> BB. dwflags = dwm_bb_enable | dwm_bb_blurregion; <br/> BB. fenable = true; <br/> BB. hrgnblur = hrgn; <br/> dwmenableblurbehindwindow (hwnd, & bb );

4. Expand the window border to the customer area

In the above method, there is still a line between the non-customer zone and the customer zone. How can we increase the scope of the glass effect and eliminate the limit? That is, the window border is expanded to the customer area, using the functionDwmextendframeworkclientarea
. The function accepts a window handle andMargins
Type parameter. Margins specifies the extended range in the upper, lower, and left directions. If the four values are-1, they are extended to the entire customer zone.

Margins margins = {50, 50, 50 }; <br/> dwmextendframeworkclientarea (hwnd, & margins );

Margins margins2 = {-1}; // it will be extended to the entire customer zone <br/> dwmexico tendframeworkclientarea (hwnd, & margins2 );

5. Drawing Images in a window

PNG images with Alpha channels can work well with Aero glass. It is very convenient to use GDI + to display PNG images. The following code loads a PNG image into the memory:

Bitmap BMP = bitmap: fromfile (L "ferrari.png", false );

In wm_paint message processing, after the entire customer area is painted black, use GDI + to draw the image to the window customer area:

// Draw a graph <br/> int width = BMP-> getwidth (); <br/> int Height = BMP-> getheight (); <br/> rect RC (30, 30, width, height); <br/> graph. drawimage (BMP, RC, 0, 0, width, height, unitpixel );

6. Text Rendering

When the window is transparent in a wide range, reading the text in the window becomes a problem. In Windows, the solution is to add a glowing effect to the text. The text in the title bar uses this method. We can useDrawthemetextex
Function. The prototype of this function is defined as follows:

Hresult drawthemetextex (htheme, <br/> HDC, <br/> int ipartid, <br/> int istateid, <br/> lpcwstr psztext, <br/> int icharcount, <br/> DWORD dwflags, <br/> lprect prect, <br/> const dttopts * poptions <br/> );

Htheme is a theme handle, which can be usedOpenthemedata
Obtain,
Openthemedata

The function accepts a window handle and the name of the topic class. Ipartid and istateid represent the part and state in the topic class, and all available topic classes, parts, and States can be viewed in the help document of the SDK. Psztext is the text to be drawn. Icharcount indicates the number of texts, and-1 indicates drawing all texts. Dwflags specifies the text format. Prect is the text drawing area. In poptions, you can set the light emitting, shadow, and other effects of the text. HDC is a device context handle. To achieve the luminous effect similar to the text in the title barBeginpaint
To useCreatecompatibledc
Create a handle in the memory, and create a bitmap. Use the memory handle to draw the text in place on the map. And then transfer the bitmap to the window. The following function encapsulates the process of drawing the luminous text:

// Draw luminous text <br/> void drawglowingtext (HDC, lpwstr sztext, rect & rcarea, <br/> DWORD dwtextflags = dt_left | dt_vcenter | dt_singleline, int iglowsize = 10) <br/>{< br/> // obtain the topic handle <br/> htheme hthm = openthemedata (getmediatopwindow (), L "textstyle "); <br/> // create Dib <br/> HDC hmemdc = createcompatibledc (HDC); <br/> bitmapinfo BMP info = {0}; <br/> BMP info. bmiheader. bisize = sizeof (BMP info. bmiheader); <br/> BMP info. bmiheader. bibitcount = 32; <br/> BMP info. bmiheader. bicompression = bi_rgb; <br/> BMP info. bmiheader. biplanes = 1; <br/> BMP info. bmiheader. biwidth = rcarea. right-rcarea. left; <br/> BMP info. bmiheader. biheight =-(rcarea. bottom-rcarea. top); <br/> hbitmap hbmp = createdibsection (hmemdc, & BMP info, dib_rgb_colors, 0, null, 0); <br/> If (hbmp = NULL) return; <br/> hgdiobj hbmpold = SelectObject (hmemdc, hbmp); <br/> // draw options <br/> dttopts = {0}; <br/> dttopts. dwsize = sizeof (dttopts); <br/> dttopts. dwflags = dtt_glowsize | dtt_composited; <br/> dttopts. iglowsize = iglowsize; // The luminous range <br/> // draw the text <br/> rect rc = {0, 0, rcarea. right-rcarea. left, rcarea. bottom-rcarea. top };< br/> hresult hR = drawthemetextex (hthm, hmemdc, text_label, 0, sztext,-1, dwtextflags, & rc, & dttopts ); <br/> If (failed (HR) return; <br/> bitblt (HDC, rcarea. left, rcarea. top, rcarea. right-rcarea. left, <br/> rcarea. bottom-rcarea. top, hmemdc, 0, 0, srccopy | captureblt); <br/> // clear <br/> SelectObject (hmemdc, hbmpold); <br/> deleteobject (hbmp ); <br/> deletedc (hmemdc); <br/> closethemedata (hthm); <br/>}

After drawing the image, add the following code to draw a piece of text:

// Draw text <br/> rect rctext = {10, 10,300, 40 }; <br/> drawglowingtext (HDC, l "a little bit of Chinese and some English", rctext );

Because the font emits light, leaving a space on the left of the text looks more comfortable. The effect is as follows:

VII. Association of thumbnails

Dwm api also has a function, that is, thumbnail Association. It allows us to display the thumbnail of a window to the customer area of our window. Unlike thumbnails, thumbnails are updated in real time. The following code displays the thumbnail of the QQ audio and video player in the client area of the window:

Hresult hR = s_ OK; <br/> hthumbnail thumbnail = NULL; <br/> hwnd hwndsrc = findwindow (_ T ("qqplayer window"), null ); <br/> hR = dwmregisterthumbnail (hwnd, hwndsrc, & thumbnail); <br/> If (succeeded (HR) <br/>{< br/> rect RC; <br/> getclientrect (hwnd, & rc); <br/> dwm_thumbnail_properties dskthumbprops; <br/> dskthumbprops. dwflags = dwm_tnp_rectdestination | dwm_tnp_visible | dwm_tnp_opacity; <br/> dskthumbprops. fvisible = true; <br/> dskthumbprops. opacity = 200; <br/> dskthumbprops. rcdestination = RC; <br/> hR = dwmupdatethumbnailproperties (thumbnail, & dskthumbprops); <br/>}

First, find the source window handle using the window title, and then useDwmregisterthumbnail
Register the thumbnail Association. After successful registrationDwmupdatethumbnailproperties
Update the thumbnail attributes, including whether the thumbnail is visible, transparency, and target painting area. The following results are displayed:

 

Source code download

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.