Implementation of transparent window (opaque content such as text picture above window)
This article discusses the purpose of this article through SetLayeredWindowAttributes.
The implementation of SetLayeredWindowAttributes must set the window to the extended style of ws_ex_layered. However, only the Ws_popup window can set the extension style of the ws_ex_layered. This means that only transparent windows can be implemented in the popup window, which is not possible in the Ws_child style window.
I was trying for a long while to find No.
The prototype of SetLayeredWindowAttributes is as follows:
[CPP]View Plaincopy
- BOOL SetLayeredWindowAttributes ( hwnd hwnd,
- colorref Crkey,
- BYTE Balpha,
- DWORD dwFlags
- );
The first parameter is the handle of the window to be set, the second one is to set a transparent color, the third is to set the transparency, the range of Balpha is 0-255, if it is 0, then completely transparent, if it is 255, is completely opaque. The fourth parameter is whether the setting is transparent according to the transparent color or according to Balpha. Or two of them are set. If DwFlags is set to Lwa_colorkey, then Crkey will work, all the crkey color areas in the window will be transparent, and if DwFlags is set Lwa_alpha then Balpha will work. The entire window will be transparent according to the Balpha value. You can also set these two together to achieve both effects.
For example, the following code:
SetLayeredWindowAttributes (M_hwnd,rgb (255,0,255),
(BYTE),lwa_alpha| Lwa_colorkey);
All the magenta areas of the window will be fully transparent, then the remaining area is translucent.
Remember, it is not possible to set it separately, such as the following is not required to set up:
SetLayeredWindowAttributes (M_hwnd,
RGB (255,0,255), 0,lwa_colorkey);
SetLayeredWindowAttributes (m_hwnd,0, (BYTE) 220,lwa_alpha);
Let's implement a semi-transparent tip window:
1. Because our window is to implement a tip, this window is an irregular window, all need to map to achieve the border, and so on, all need magenta full, the other part of the semi-transparent effect. The following code can be used in oncreat or OnInitDialog:
SetLayeredWindowAttributes (M_hwnd,rgb (255,0,255),
(BYTE),lwa_alpha| Lwa_colorkey);
2. Then, we need the overall window, but the above text content can not be transparent, this problem can not be resolved, all can only in this window and then set a popup upwindow window, background brush a pure color, such as
RGB (0,255,0), and then oncreat or OnInitDialog with the SetLayeredWindowAttributes Settings window is fully transparent, the display of the content is drawn in this upwindow, so it seems to achieve the window translucent, But the above text is opaque effect.
The code is as follows:
SetLayeredWindowAttributes (M_hwnd,rgb (0,255,0), 0,lwa_colorkey);
Write the following code in the Upwindow OnPaint:
CRect RC;
GetClientRect (&RC);
dc. Fillsolidrect (Rc,rgb (0,255,0);
This will achieve the effect we need.
is what I achieved:
Original link: http://blog.csdn.net/tangaowen/article/details/6054123
http://blog.csdn.net/huasonl88/article/details/8697768
Implementation of transparent window (opaque content such as text picture above window)