设置透明背景 Win32 [英] Setting transparent background Win32

查看:32
本文介绍了设置透明背景 Win32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的很简单,但似乎没有很多关于它的信息.基本上我有一个普通的非透明父窗口,我想在该父窗口内放置一个子窗口(具有自己的非透明控件).所以我唯一要做的就是将子窗口的背景画笔设置为透明,但它仍然绘制了白色背景.尝试使用 WS_EX_LAYERED 样式和 SetLayeredWindowAttributes,但它使子窗口不可见.

What I'm trying to do is very simple but there doesn’t seem to be a lot of information on it. Basically I have a normal non-transparent parent window and I want to place a child window (that has its own non-transparent controls) inside that parent window. So the only thing I have to do is to set the background brush of the child window transparent, but it still draws a white background. Tried using WS_EX_LAYERED style and SetLayeredWindowAttributes but it makes the child window invisible.

推荐答案

在Windows上实现透明"子控件有​​两种基本方式:

There are two basic ways to achieve "transparent" child controls on Windows:

  • 在父窗口中处理 WM_CTLCOLORxxx 消息.这是使现有控件支持透明度的一种便捷方式.每个控件都将发送适合控件类型的 WM_CTLCOLORxxx 消息.如果您创建一个表示对话框背景皮肤的画笔,并从每条消息中返回它,则最终效果就好像每个控件都涂有透明的外部区域.但是,如果您想要 alpha 效果,或者控件在物理上相互重叠,这将失败.

  • Handle WM_CTLCOLORxxx messages in the parent window. This is a convenient way to make existing controls support transparency. Each control will send a WM_CTLCOLORxxx message appropriate to the type of the control. If you create a brush that represents the background skin of the dialog, and return that from each message, the net effect will be as if each control was painted with a transparent outer area. This fails however if you want alpha effects, or for controls to physically overlap each other.

在父窗口上使用 WS_EX_COMPOSITED.如果没有这种风格,窗口管理器的子窗口的绘制顺序是不确定的,实际上是从上到下.当尝试 alpha 混合重叠控件时,结果将是......令人不快.WS_EX_COMPOSITED 将确保子窗口从下到上绘制.确保您不要使用 WS_CLIPCHILDREN 或 WS_CLIPSIBLINGS 样式,因为这将完全防止重叠区域被绘制.

Use WS_EX_COMPOSITED on the parent window. Without this style, the window managers paint order of child windows is undefined, in practice, its top to bottom. When trying to alpha blend overlapping controls the result will be... unpleasant. WS_EX_COMPOSITED will ensure the child windows are painted bottom to top. Make sure that you DON'T use either the WS_CLIPCHILDREN or WS_CLIPSIBLINGS styles as that will prevent the overlapping areas from being painted at all.

你仍然需要通过 WM_CTLCOLORxxx 消息做一些聪明的事情,因为标准控件仍然会尝试用对话框背景灰色填充它们的整个矩形.

You still need to do something clever via WM_CTLCOLORxxx messages as the standard controls are still going to try and fill their entire rect with dialog-background-grey.

这篇关于设置透明背景 Win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆