Use duilib to develop a translucent special-shaped form Program (with source code and demo) and duilibdemo

Source: Internet
Author: User

Use duilib to develop a translucent special-shaped form Program (with source code and demo) and duilibdemo

Reprinted, please describe the original source. Thank you ~~ : Http://blog.csdn.net/zhuhongshu/article/details/43532791

The functions of translucent special-shaped forms already have basic functions in the earlier versions of duilib maintained. However, due to many defects, I have always suggested less use, even I wrote my own cool dog project and used a semi-transparent shaped form in a few small places. However, after discussing with other friends in the group today, I found many problems and solutions. So I immediately fixed the current library and wrote a demo of a translucent special-shaped form to test the effect. The translucent form is implemented using the UpdateLayeredWindow function, not a double-layer form.


In this blog, I will mainly explain the following points:


1. Problems with the use of translucent special-shaped forms in earlier versions

2. General Solution in the new version

3. No solution yet

4. Notes for using a translucent special-shaped form


If you don't talk much about it, first show the demo:



Existing Problems and Solutions:


Question 1: font penetration

This is the biggest problem with the previous semi-transparent form. Due to the defects of GDI, the transparent channel information is missing during text rendering, resulting in text penetration. Previously, the heat group owner's alpha repair method was used, however, this problem cannot be completely solved. For this reason, I modified the text rendering function. When the form is in transparent mode or you enable the gdi + text rendering mode, you can use the gdi + to render the text. To enable the translucent mode, set the bktrans attribute of the Window label of the xml layout to true. To enable the gdi + text rendering mode, set the gdiplustext attribute of the Window label of the xml layout to true. Bktrans and gdiplustext attributes are independent. (If you feel that the default GDI text rendering effect is not good enough, you can use the gdiplustext attribute to get a better rendering effect ).


The rendering efficiency of GDI + is famous for its low level. However, if I tested the text rendering, I could not see the impact on the rendering efficiency.


Question 2: font penetration 2

It's still the text with the font penetrating. Even if we change the text rendering to gdi + and fix it with alpha, we still find that the font penetrating problem still exists, which is also a problem that I have been wondering about before. Today, I did a lot of tests and found that it was related to the background color of the control. If the background color or background color of the control is not set to translucent, the rendered text will not penetrate, but the background color will penetrate if it is set to opaque. The background color filling function is responsible for the DrawColor function of the CRenderEnghine class. Observe the source code and find out. If the transparency of the background color is set to FF (that is, opacity), the GDI function is used to fill the background color. If transparency is set, then, use the AlphaBlend function to fill the background with a bitmap. Therefore, the problem lies in the transparent channel (indicating that the alpha fix cannot completely solve the problem ).


Therefore, as long as duilib fills the background color with transparent information, it will not cause penetration. Then, read the code and find that duilib has many default alpha background color filling codes. I personally feel that it is better to directly use gdi + to fill the background color. The Code is as follows:


    if( color <= 0x00FFFFFF ) return;Gdiplus::Graphics graphics( hDC );Gdiplus::SolidBrush brush(Gdiplus::Color((LOBYTE((color)>>24)), GetBValue(color), GetGValue(color), GetRValue(color)));graphics.FillRectangle(&brush, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);


This code is relatively simple and will not affect the efficiency, right?


Question 3: incomplete form refresh

The translucent special-shaped program compiled in the original library sometimes has incomplete forms refresh. For example, you can drag a translucent program to the outside of the screen and double-click the title bar to maximize it, then, we minimize the number and drag the program into the screen. Then we can see that the original screen is not drawn.


The cause of this problem is that the UpdateLayeredWindow function is required to use a special form. After using this function, the UpdateLayeredWindow function takes over the form painting, in many cases, the WM_PAINT message will not be triggered. After tracking the code, it is found that the information is incomplete when the program restores and calls the GetUpdateRect function to obtain information about the updated region. Therefore, I intercepted the WM_SYSCOMMAND message in the CManagerUI class, and found that the Program sets m_bIsRestore as true after the maximum restoration, and uses m_bIsRestore as the flag. When the interface is refreshed in WM_PAINT, if m_bIsRestore is true, the whole interface is refreshed, not just the region obtained by GetUpdateRtect.


No solution yet


After solving the problems described above, use the alpha-Repaired code. You can use a semi-transparent special-shaped form. However, there are still some unsolved problems:

1. The effect of the Edit Control in semi-transparent mode is poor. The Edit Control of duilib calls the edit Control of Win32 internally. To use it in semi-transparent mode, you have to use the edit Control of The WS_POPUP style form Win32. However, in this case, the form will have some appearance problems. That is, when you click the Edit Control of duilib, it will flash. Therefore, we recommend that you use the RichEdit control instead of Edit, and the RichEdit control supports translucent or fully transparent background.


2. Although the RichEdit control can be used normally, it may cause text penetration because it does not process transparent channels during text rendering. Text penetration only occurs when the RichEdit control has no background color or background image.


Notes for using a translucent special-shaped form


Currently, You need to note the use of the RichEdit control. If RichEdit is directly exposed to a fully transparent place, text rendering will be problematic. If RichEdit sets the background color or background color, or its parent control sets the background color background image, there is no problem. Fortunately, there should be nothing to expose RichEdit directly to a fully transparent location. The text box above is made with RichEdit.


If penetration is detected when RichEdit is used, we recommend that you change the textcolor value or bkcolor value to fix the problem.



Summary:


As there are many modifications and the design files and code are also messy, I cannot provide a single source file. You can use svn to compare the code of the new and old versions. I updated both the source code and demo to my library. duilib and uilib are supported: Click to open the link.



Redrain 2015.2.5


Related Article

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.