Problems with transparent/translucent windows/Images

Source: Internet
Author: User
Tags drawtext
There have been many transparent and translucent windows in the last two weeks. Before that, I wroteCodeWith the help of the encapsulated skin library, but now it's all "handwritten"-the Stone Age adventure, encountered some difficulties, and I would like to summarize it here.
1. Ignore the Alpha value of the GDI function.
Only the alphablend API can be used to draw Alpha channels. alphablend can be used to draw 32-bit bitmaps. Because only this API can recognize the alpha channel. If a memdc uses drawtext to draw text, the Alpha value of these text areas is 0, then, use the alphablend API to copy the memdc bitmap to the actual DC, which will cause an exception in the text area, either transparent or pure white. Solution: 1. When copying data from memdc to the actual DC, use bitblt for simple copy instead of alphablend for hybrid copy; 2. 1 may not be able to achieve the desired translucent effect, so we can only traverse each pixel of the text area before the hybrid copy and change its alpha value to 255. [Refer to preview.

2. alphablend API and transparency
Alpha Channel value. 0 indicates full transparency, and 255 indicates full opacity.
The sourceconstantalpha member in the blendfunction struct in the alphablend function indicates the Alpha value of the entire image. 0 indicates that the source image is completely transparent, and 255 indicates that the alpha value of each pixel of the source image is used.
The image generated by the alphablend function is used to mix the source, target, and sourceconstantalpha. A complex set of Computing: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183393 (V = vs.85). aspx
Computing involves the following situations:
1. The ac_src_alpha attribute is not set. Only sourceconstantalpha is mixed;
2. Set the ac_src_alpha attribute and set sourceconstantalpha to 0xff. At this time, only the Alpha values of each pixel in the source image are mixed;
3. Set the ac_src_alpha attribute and set sourceconstantalpha to non-0xff. At this time, both the sourceconstantalpha value and the Alpha value of each pixel in the source image are used for mixing.
According to the msdn formula, when the value of sourceconstantalpha is 0, the source DC image is not displayed at all on the Target DC, and the RGB value of the source DC is lost. When sourceconstantalpha is 255, the Alpha value of the source DC image is used.
When alphablend is used, data is copied from one DC to another. Therefore, to paste an image to the Target DC, you must first select the image to memdc, and then use the alphablend function to copy the image from memdc to the actual DC.
Using this API, you can achieve smooth transition during image switching. The previous image disappears slowly and the next image is slowly displayed. 3. possible causes of alphablend failure
Parameter Error cause 1: If the bitmap bmbitspixel is 24, this function cannot be used.
Therefore, for images with unknown sources, the safe method is
Bitmap bitmapinfo;
: GetObject (hbitmap, sizeof (Bitmap), & bitmapinfo );
If (bitmapinfo. bmbitspixel = 24) // a 24-Bit Bitmap directly uses bitblt/stretchblt
{...........}

Cause of parameter error 2: The parameter may be used incorrectly. The bitmap of the source DC does not match the value indicating the height and width of the source bitmap in the parameter.

The rendering effect is strange: I thought that bitblt could not be used after alphablend was used. In fact, it is acceptable and will not cause alpha channel loss. However, there may be a problem: Use alphablend to draw an image on memdc, use drawtext to draw a text on memdc, and then use alphablend to copy the content of memdc to the actual DC, this will cause the text to become white. Therefore, alphablend cannot be used in the last step, and bitblt must be used.
 
4. Drawing text with GDI +
The drawstring of GDI + can draw text. If you use GDI + to draw text and add the ws_ex_layer attribute, the text drawn by GDI + must be copied to the target area using updatelayeredwindow. You cannot use GDI + for painting directly in a window with the ws_ex_layer attribute. You must first draw the painting and then copy it using the updatelayeredwindow function.

5. Use GDI +
The header file must be added to vs2008: # include <objbase. h> GDI + initialization: Ulong_ptr m_gditoken;   Gdiplusstartupinput m_gdiinput; Gdiplusstartup (& M_gditoken ,& M_gdiinput , Null ); GDI + destruction Gdiplusshutdown ( M_gditoken );

6. The ws_ex_layered window cannot be displayed.
Only the ws_ex_layered attribute is available, and the setlayeredwindowattribute or updatelayeredwindow function is not called. This will make the window unavailable.

7. How to draw partial transparent windows
1) cut out some transparent graphs;
2) create a window with the ws_ex_layered attribute;
3) Select the image to memdc and use the alphablend function to copy the image to another memdc;
4) because the drawtext function of GDI clears the Alpha value of pixels in the region by 0, it is transparent. Therefore, we recommend that you use the drawstring function of GDI +;
5) Finally, use updatelayeredwindow to copy memdc to the actual DC.

8. Differences between setlayeredwindowattributes and updatelayeredwindow
Both versions earlier than Windows 7 require a top-level window with the ws_ex_layered attribute (a window without the ws_child attribute). Versions later than Windows 8 support non-top-level windows.
The setlayeredwindowattributes function can be implemented: 1. Transparent to an RGB value; 2. transparent to the entire window.
The updatelayeredwindow function implements partial translucent. In addition, this function is a GDI + function, which must be initialized during use.

9. updatelayeredwindow does not support partial update. 
The updatelayeredwindow function provides the setwindowpos function to set the window position and size. This function does not support partial update of the painting area.

10. The value after makeintresource cannot be assigned to wstring.
This is not a string. If a value is assigned, the wm_paint message is triggered (rather inexplicable) and an error occurs.

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.