Special topic: duilib Win32 Transparency effect

Source: Internet
Author: User
Tags transparent color

Win32 Transparency Effects Related basics

layered Windows Layered window . This is the concept that Windows2000 began to introduce, redefining the window's hit testing method, which used to cut the window in rectangle way, and put the window with a ws_ex_layered style and hit it according to the shape and pixel value of the window. testing, so that our irregular window becomes a truly independent window, not a traditional one that is contained by a rectangular window that is not visible.

Layered window Redraw mode, transparency effect generation

Three ways to create an irregular window
1. Set the area of the window via the zone-related API SetWindowRgn;;
2. Through the setlayeredwindowattributes to specify a special transparent color, so that part of the background map fully transparent to achieve the "irregular" window;
3. Use Updatelayeredwindow to specify special color transparency or to set the window full through the image's alpha value.

    • Updatelayeredwindow (): Directly updates the position, size, shape, content and semi-transparency of a layered window. The advantage is that once and for all, there is no need to respond to various redraw events in the window function.
    • Use the SetLayeredWindowAttributes () function to set information about the transparency of the window, and then in the traditional way, respond to various redraw events in the window function.

Note: controls on a layered window become transparent with the window
Solution: Prepare two windows, window A and Window B, window A as a display window, that is, a special window, and window B as a logical window, and then let the two windows overlap in a piece, it can be said that Windows B created a window, Then through the Updatelayeredwindow to the window A to realize the abnormity, because window A on the window B, then will be bound to cover the window B control, and then we will be the window A through the setwindowrgn to cut, through the hole out of the control position to achieve the display of the control.

Irregular area Redraw

setwindowrgn () function : The area that is used to set a window. Only places that are contained within this area will be redrawn, and other regional systems not included in the zone will not be displayed.

Our irregular shapes come from this. This function and its friends are very powerful, can not only define the independent basic shape of the area, but also by the operation (COMBINERGN: Two regions can be intersected, copied, subtracted, XOR, or operation) to combine the existing areas to create a new region.

Example of an irregular window effect
    • Simple effect

    • Complex effects

The realization of Duilib library to transparent effect

Duilib, form transparency setting method
1. Set the Window Label property bktrans= "True" alpha= "" "The value of alpha is 0-255. This setting is the transparency of the entire form, and all controls become transparent.
2. If you want to set the opacity of the background transparency control purely, you can make a translucent background picture, set the window label's bktrans= "true", and do not set the Alpha property, when the background is transparent, the other controls are opaque.
3. Set the transparency of a control individually, you can use the Fade property of the picture, or the Mask property. Fade to set the transparency of the picture, taking a value of 0-255. Mask to set the transparent color.

DUILIB.CRENDERENGINE.LOADIMAGE Specifies the transparency of a color mask on a control: when the bitmap of a picture resource is loaded, zero the pixels of the color mask, and the positions of the pixels are transparent when drawn
 if( *(DWORD*)(&pDest[i*4]) == mask ) {            pDest[i*4] = (BYTE)0;            pDest[i*4 + 1] = (BYTE)0;            pDest[i*4 + 2] = (BYTE)0;             pDest[i*4 + 3] = (BYTE)0;            bAlphaChannel = true;        }
The picture resource file is loaded only once and is cached in memory after it is successfully loaded.
// data : 加载的图片资源if( !data ) return NULL;    if( type != NULL ) data->sResType = type;    data->dwMask = mask;if( !m_mImageHash.Insert(bitmap, data) ) {// 缓存提速::DeleteObject(data->hBitmap);delete data;data = NULL;}
DuiLib.CRenderEngine.DrawImage rendering Engine: transparent effect rendering process
static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");.... ....if( lpAlphaBlend && (alphaChannel || uFade < 255) ) {// uFade:设置背景图片透明度;  alphaChannel:启用指定颜色(Mask)透明        BLENDFUNCTION bf = { AC_SRC_OVER, 0, uFade, AC_SRC_ALPHA };     if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;    .... ....// Alpha混合贴图lpAlphaBlend(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,                             hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top,                             lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
DuiLib.CControlUI.DoPaint Control Redraw Process
void CControlUI::DoPaint(HDC hDC, const RECT& rcPaint){    if( !::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem) ) return;    // 绘制循序:背景颜色->背景图->状态图->文本->边框    if( m_cxyBorderRound.cx > 0 || m_cxyBorderRound.cy > 0 ) {        CRenderClip roundClip;        // CRenderClip内部以SetWindowRgn()函数对绘制区域进行控制        CRenderClip::GenerateRoundClip(hDC, m_rcPaint,  m_rcItem, m_cxyBorderRound.cx, m_cxyBorderRound.cy, roundClip);        PaintBkColor(hDC);        PaintBkImage(hDC);// 调用渲染引擎绘制背景图片        PaintStatusImage(hDC);        PaintText(hDC);        PaintBorder(hDC);    }    else {        PaintBkColor(hDC);        PaintBkImage(hDC);        PaintStatusImage(hDC);        PaintText(hDC);        PaintBorder(hDC);    }}
DuiLib.CPaintManagerUI.MessageHandler after each control is drawn, the whole is pasted into the main window for display
BLENDFUNCTION blendPixelFunction = {AC_SRC_OVER, 0, m_nOpacity, AC_SRC_ALPHA};BOOL bRet = ::UpdateLayeredWindow(m_hWndPaint, NULL, &pt, &szWindow, m_hDcOffscreen, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA);
The target effect of masking effect in the cover process of lesson preparation artifact

A for the user Mouse selection section, no mask;
B is not selected, full shadow Matte (example graph not perfect, matte not covering entire picture)

b for window, set bktrans= "true" to achieve transparency;
By SetWindowRgn cropping, the B does not draw a area, thus the user can select the effect
There is a problem: a zone can not respond to mouse messages, user drag operation can not respond!

Improvement Programme
    1. A for the user Mouse selection section, no mask;

    2. B is not selected, all shadow Matte (example diagram is not complete, the entire picture is not covered)

    3. C is a logical window, covering the entire picture (example diagram is not perfect, not covering the entire picture), responsible for user action response

C for window, set bktrans= "true", the background map requires an infinitely close to full transparency to eliminate the impact to the a zone.
B is a control that creates a matte effect from a background map
B by SetWindowRgn clipping, so that B does not draw a region, so as to achieve user selection effect

Final effect

Special topic: duilib Win32 Transparency effect

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.