Windows window transparent knowledge

1. Transparent window requirements:

Child windows can not be the type of

To make the form has transparency, first have WS_EX_LAYERED extended attributes, the method may specify CreateWindowEx when SetWindowLong may be set dynamically. Layered windows can be provided the following code:
a LONG style = the GetWindowLong (m_hWnd, GWL_EXSTYLE);
style | = the WS_EX_LAYERED;
the SetWindowLong (m_hWnd, GWL_EXSTYLE, style);
 

2. SetLayeredWindowAttributes

(BOOL SetLayeredWindowAttributes

HWND hwnd, // specify the layered window handle

COLORREF crKey, // need to specify a transparent background color value, can be used the RGB () macro

BYTE bAlpha, // set the transparency, 0 represents a completely transparent, opaque 255

DWORD dwFlags // transparent manner

);

Wherein, dwFlags parameter may take the following values:

When LWA_ALPHA: crKey parameter is invalid, bAlpha parameters are valid;

LWA_COLORKEY: form all colors for local crKey will become transparent, bAlpha invalid parameters. Which is a constant.

LWA_ALPHA | LWA_COLORKEY: crKey place will become transparent, while the rest is determined based on transparency bAlpha parameters.

::SetLayeredWindowAttributes((HWND)透明窗口handle, RGB(100,100,100), 128, LWA_ALPHA | LWA_COLORKEY);

 

// Get a handle to the window where the mouse following
m_hCurrWnd = :: WindowFromPoint (point);

if (g_pSetLayeredWindowAttributes && m_hCurrWnd != m_hWnd)
{
::SetWindowLong(m_hCurrWnd, GWL_EXSTYLE,
GetWindowLong(m_hCurrWnd,  GWL_EXSTYLE) ^ WS_EX_LAYERED);
g_pSetLayeredWindowAttributes(m_hCurrWnd, 0, (BYTE)m_slider.GetPos(), LWA_ALPHA);

::RedrawWindow(m_hCurrWnd, NULL, NULL,
RDW_ERASE | RDW_INVALIDATE |
RDW_FRAME | RDW_ALLCHILDREN);
}

LayeredWindow offers two modes:

1. SetLayeredWindowAttributes to set transparency, complete uniform, transparent window, then the window still receive PAINT message, the same as other applications like an ordinary window.

2. Use UpdateLayeredWindow method, DC include bitmap submitted to the system, referred to the system of unified management, this time no longer receive paint messages, any changes to the window, can only be modified by UpdateLayeredWindow.

If you do not have transparent pixels for different levels, you can just use SetLayeredWindowAttributes mode, usage and work just like a normal window, there is a little different bitmap, buffers the window, so when the window above other windows were removed, this The system is going to draw their own, paint does not send the message. When using the benefits of this model, you do not substantially change the way you use window message after you receive paint, draw images will be redirected to another function inside the system, are combined to obtain transparency effects.

If you need to reach different transparency for pixel-level, or you want more direct control to draw a window, you must use UpdateLayeredWindow method, this method does not redirect your rendering result, not bitmap cache window, but completely to draw your own, so in memory, it is more efficient.

 

1.一旦你调用了SetLayeredWindowAttributes,UpdateLayeredWindow的调用就会失败,你需要重新设置WS_EX_LAYERED,这是两种模式切换的关键。
2.WS_EX_LAYERED属性是不可以设置给子窗口
3.在完全透明的地方,鼠标的点击将会被穿过,另如果对窗口设置了WS_EX_TRANSPARENT属性,鼠标消息也会穿过

DM uses the following ways to achieve UpdateLayeredWindow PAINT message receiving window:

If you want the button also made transparent, two ideas provided to the landlord under the reference:
One way is to directly use the picture drawn up by the AlphaBlend GDI or GDI + rendering transparent transparent channels do, but all the acts so that the buttons are to write themselves.
Another is to make this button also windowed form, to use a transparent SetLayeredWindowAttributes.

 

--------- Why Winforms control does not support semi-transparent background color

https://blog.csdn.net/MYsce/article/details/77900743

painting the entire Windows Forms framework is based on GDI +. We know that GDI + supports transparent color, which is supported color with alpha. So why the vast majority of control does not know the background color of translucent it? This is a Winforms limited by the Win32 API.

    All the controls in Winforms, only Form and ToolStripDropDown support Opacity property. When the value of the Opacity attribute 0, it is completely transparent; when the value of 1, was completely opaque; when a value between 0 and 1, it shows a translucent effect.

    So why can not other controls to achieve a translucent effect? When we go deep into the internal code Winform time to analyze the internal Opacity implemented, we need to find a translucent effect control supports WS_EX_LAYERED. From the MSDN http://msdn.microsoft.com/en-us/library/ms632680(VS.85).aspx we found WS_EX_LAYERED not be used for child windows (Child Window) on. Form can not be a child window of other windows, but only in ToolStripDropDown TopLevel is true when Opacity is valid, then the type of window is a pop-up rather than a child type. Therefore, these two types of spatial support Opacity property. While all other spaces are used in the creation of the WS_CHILD type, they belong Child window, it can not support Opacity property.

    If you want other space to support semi-transparent background color, in addition to our BackColor color contain alpha information, also need to ControlStyles.SupportsTransparentBackColor and ControlStyles.UserPaint set to true. For example, we want to get the background color to transparent Label, we can own Label inherit from a class, and calls the function Control.SetStyle class constructor in the first two Style set to true. at this time, if the alpha value 0 of the background color, the background becomes transparent Label the . It is worth mentioning that, due to the Control.UserPaint is true, we need to be responsible for themselves the whole process of Paint, Winforms controls are not automatically drawn. For example, by default, all Item in ListBox would not be drawn, we need to complete the operation in its Paint Paint event handler.

 

 

Guess you like

Origin blog.csdn.net/smartgps2008/article/details/90318801