包含不透明文本和按钮的透明窗口

发布于 2024-08-02 18:49:49 字数 348 浏览 4 评论 0原文

我正在创建一个非侵入式弹出窗口,以在处理耗时的操作时通知用户。目前,我通过调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-08-09 18:49:49

为了在分层窗口中执行“正确的”alpha,您需要通过调用 UpdateLayeredWindow 为窗口管理器提供 PARGB 位图。

据我所知,实现此目的的最简洁方法如下:

  1. 使用 PixelFormat32bppPARGB 像素格式创建 GDI+ Bitmap 对象。
  2. 创建一个 Graphics 对象以在此 Bitmap 对象中进行绘制。
  3. 使用 GDI+ 将所有绘图绘制到该对象中。
  4. 销毁在步骤 2 中创建的 Graphics 对象。
  5. Bitmap 对象调用 GetHBITMAP 方法以获取 Windows HBITMAP
  6. 销毁 Bitmap 对象。
  7. 使用 CreateCompatibleDC 创建一个内存 DC,并在其中选择第 5 步中的 HBITMAP
  8. 使用内存 DC 作为源调用 UpdateLayeredWindow。
  9. 选择之前的位图并删除内存DC。
  10. 销毁第 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:

  1. Create a GDI+ Bitmap object with the PixelFormat32bppPARGB pixel format.
  2. Create a Graphics object to draw in this Bitmap object.
  3. Do all your drawing into this object using GDI+.
  4. Destroy the Graphics object created in step 2.
  5. Call the GetHBITMAP method on the Bitmap object to get a Windows HBITMAP.
  6. Destroy the Bitmap object.
  7. Create a memory DC using CreateCompatibleDC and select the HBITMAP from step 5 into it.
  8. Call UpdateLayeredWindow using the memory DC as a source.
  9. Select previous bitmap and delete the memory DC.
  10. Destroy the 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 the SetTextRenderingHint on the Graphics object to set the antialiasing type to the same type that is configured by the user, for a nicer look.

半夏半凉 2024-08-09 18:49:49

我怀疑您需要两个顶级窗口,而不是一个 - 一个具有 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.

胡大本事 2024-08-09 18:49:49

我不能肯定地说,您需要尝试一下,但由于一切都是一个窗口,您可以尝试为按钮设置分层属性以使其不透明。

至于文本,您也许可以将其放在自己的框架中,并设置背景和前景色,并修改其分层属性以使背景颜色透明......

但由于这些是子窗口而不是顶级窗口窗口,我真的不知道它会起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文