包含不透明文本和按钮的透明窗口
我正在创建一个非侵入式弹出窗口,以在处理耗时的操作时通知用户。目前,我通过调用 SetLayeredWindowAttributes
设置其透明度,这给了我一个合理的结果:
替代文本 http://img6.imageshack.us/img6/3144/transparentn.jpg
但是我希望文本和关闭按钮显示为不透明 (它看起来不太适合白色文本),同时保持背景透明 - 有没有办法做到这一点?
I'm creating a non-intrusive popup window to notify the user when processing a time-consuming operation. At the moment I'm setting its transparency by calling SetLayeredWindowAttributes
which gives me a reasonable result:
alt text http://img6.imageshack.us/img6/3144/transparentn.jpg
However I'd like the text and close button to appear opaque (it doesn't quite look right with white text) while keeping the background transparent - is there a way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了在分层窗口中执行“正确的”alpha,您需要通过调用
UpdateLayeredWindow
为窗口管理器提供 PARGB 位图。据我所知,实现此目的的最简洁方法如下:
PixelFormat32bppPARGB
像素格式创建 GDI+Bitmap
对象。Graphics
对象以在此Bitmap
对象中进行绘制。Graphics
对象。Bitmap
对象调用GetHBITMAP
方法以获取 WindowsHBITMAP
。CreateCompatibleDC
创建一个内存 DC,并在其中选择第 5 步中的HBITMAP
。此方法应该允许您控制所绘制的所有内容的 Alpha 通道:背景透明,文本和按钮不透明。
另外,由于您要输出文本,我建议您调用
SystemParametersInfo
来获取默认的抗锯齿设置 (SPI_GETFONTSMOOTHING
),然后调用SetTextRenderingHint
code> 在 Graphics 对象上将抗锯齿类型设置为与用户配置的类型相同,以获得更好的外观。In order to do "proper" alpha in a layered window you need to supply the window manager with a PARGB bitmap by a call to
UpdateLayeredWindow
.The cleanest way to achieve this that I know of is the following:
Bitmap
object with thePixelFormat32bppPARGB
pixel format.Graphics
object to draw in thisBitmap
object.Graphics
object created in step 2.GetHBITMAP
method on theBitmap
object to get a WindowsHBITMAP
.Bitmap
object.CreateCompatibleDC
and select theHBITMAP
from step 5 into it.UpdateLayeredWindow
using the memory DC as a source.HBITMAP
created in step 5.This method should allow you to control the alpha channel of everything that is drawn: transparent for the background, opaque for the text and button.
Also, since you are going to be outputting text, I recommend that you call
SystemParametersInfo
to get the default antialiasing setting (SPI_GETFONTSMOOTHING
), and then theSetTextRenderingHint
on the Graphics object to set the antialiasing type to the same type that is configured by the user, for a nicer look.我怀疑您需要两个顶级窗口,而不是一个 - 一个具有 alpha 混合,第二个显示在第一个窗口上方,带有不透明文本和按钮,但具有透明背景。要通过单个窗口完成此操作,您需要使用 UpdateLayeredWindow API 调用,但使用此方法将导致您的按钮在与之交互(悬停突出显示、焦点等)时不会重绘。
如果此应用程序仅适用于 Vista,则可能会出现新的 API 调用你可以使用,但我不相信它在 XP 或更早版本中可用。
I suspect you'll need two top level windows rather than one - one that has the alpha blend and a second that is display above the first with the opaque text and button but with a transparent background. To accomplish this with a single window you'll need to use the UpdateLayeredWindow API call, but using this will cause your buttons to not redraw when they are interacted with (hover highlights, focus etc.)
It is possible that if this application is for Vista only there is a new API call that you can use, but I do not believe it is available in XP or earlier.
我不能肯定地说,您需要尝试一下,但由于一切都是一个窗口,您可以尝试为按钮设置分层属性以使其不透明。
至于文本,您也许可以将其放在自己的框架中,并设置背景和前景色,并修改其分层属性以使背景颜色透明......
但由于这些是子窗口而不是顶级窗口窗口,我真的不知道它会起作用。
I can't say for sure, you'll need to try it, but since everything is a window, you could try setting the layered attributes for your button to make it opaque.
As for the text, you may be able to put that in its own frame with a set background and foreground color, and modify its layered attributes to make the background color transparent...
But since these are child windows and not the top-level window, I really don't know that it'll work.