U盘PE| w764位旗舰版下载 | U盘装win7系统 | U盘启动 |win7pe | win10下载 |加入收藏土豆PE官网U盘PE,U盘装win7系统,win7pe,U盘启动,U盘装系统,w764位旗舰版下载站!
当前位置:主页 > 帮助中心 > 帮助中心 >

win32透明窗口

来源:http://www.tudoupe.com时间:2022-06-11

描绘半透明透明的透明窗子并不是件小事。该算法如下:

  1. Create aWS_POPUPwindow with theWS_EX_LAYEREDstyle.
  2. Create a 32 bit DIB Section using theCreateDIBSection()function and select it to any compatible DC. It will be an off-screen plain to render window contents to.
  3. Render the window contents, preserving the alpha channel.
  4. Call theUpdateLayeredWindow()function to draw the window to the screen.

二、

https://stackoverflow.com/questions/3970066/creating-a-transparent-window-in-c-win32 winapi - Creating a transparent window in C++ Win32 - Stack Overflow

Creating a transparent window in C++ Win32

I'm creating what should be a very simple Win32 C++ app whose sole purpose it to ONLY display a semi-transparent PNG. The window shouldn't have any chrome, and all the opacity should be controlled in the PNG itself.

My problem is that the window doesn't repaint when the content under the window changes, so the transparent areas of the PNG are "stuck" with what was under the window when the application was initially started.

Here's the line where I setup the new window:

For the call to RegisterClassEx, I have this set for the background:

Here is my handler for WM_PAINT message:

One thing to note is that the application is always docked to the left of the screen and doesn't move. But, what's underneath the application may change as the user opens, closes or moves windows under it.

When the application first starts, it looks perfect. The transparent (and simi-transparent) parts of the PNG show through perfectly. BUT, when the background underneath the application changes, the background DOESN'T update, it just stays the same from when the application first started. In fact, WM_PAINT (or WM_ERASEBKGND does not get called when the background changes).

I've been playing with this for quite a while and have gotten close to getting 100% right, but not quite there. For instance, I've tried setting the background to (HBRUSH) NULL_BRUSH and I've tried handling WM_ERASEBKGND.

What can be done to get the window to repaint when the contents under it changes?

SetBKMode and SetBKColor are the APIs I have used to make the transparent parent control.

2 Answers

I was able to do exactly what I wanted by using the code from Part 1 and Part 2 of this series:

Displaying a Splash Screen with C++

  • Part 1:Creating a HBITMAParchive
  • Part 2:Displaying the windowarchive

Those blog posts are talking about displaying a splash screen in Win32 C++, but it was almost identical to what I needed to do. I believe the part that I was missing was that instead of just painting the PNG to the window using GDI+, I needed to use the UpdateLayeredWindow function with the proper BLENDFUNCTIONparameter. I'll paste the SetSplashImage method below, which can be found in Part 2 in the link above:

Share

Follow

edited Aug 6, 2020 at 15:51

Ian Boyd

234k238238 gold badges835835 silver badges11601160 bronze badges

answered Oct 19, 2010 at 19:03

adoss

1,08111 gold badge99 silver badges99 bronze badges

  • best answer, also support automatic PNG alpha blend when used with WIC (Windows Imaging Component) via COM interface.

    – dns

    Jul 5, 2015 at 1:51
  • Ended up wasting a lot of time with AlphaBlend. This solution is proabably one of the easiest ways to achieve Per-Pixel Alpha Blending, specially when you are updating the window.

    – TheBlueNotebook

    Jan 11, 2016 at 12:30

Add a comment

24

Use the SetLayeredWindowAttributes archive function, this allows you to set a mask color that will become transparent, thus allowing the background to show through.

You will also need to configure your window with the layered flag, e.g. :

After that it's fairly simple:

When your PNG contains semi-transparent pixels that you want to blend with the background, this becomes more complicated. You could try looking at the approach in this CodeProject article:

Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above

Share

Follow

edited Aug 6, 2020 at 15:50

Ian Boyd

234k238238 gold badges835835 silver badges11601160 bronze badges

answered Oct 19, 2010 at 15:53

Simon Steele

11.5k44 gold badges4343 silver badges6767 bronze badges

  • 1

    I've tried this an it ALMOST works. The issue is that there are some areas of the PNG which are simi-transparent, and the color key method only makes the pixels which are 100% opacity of the color key color value transparent. If all else fails, my fallback will be to remove all the semi-transparent regions, but I'd really rather avoid doing that.

    – adoss

    Oct 19, 2010 at 16:24
  • You can try more complicated hacks like the one in the article I've linked, but essentially as soon as you're looking to blend a pre-existing image with the desktop contents things start getting complicated.

    – Simon Steele

    Oct 19, 2010 at 16:45
  • Check out the answer that I posted. I like that code a whole lot better than what I was seeing in this CodeProject article. Not only that, but I believe what I found is the proper solution instead of a hack. Thanks for the help!

    – adoss

    Oct 19, 2010 at 19:05
  • Ah nice, I hadn't seen the BLENDFUNCTION stuff before - you're welcome!

    – Simon Steele

    Oct 20, 2010 at 8:54
  • 1

    why don't you set the forth parameter of SetLayeredWindowAttributes to LWA_ALPHA

    – Valen

    Apr 18, 2021 at 2:21

C++(Cpp)更新层窗口实例-热例

Copyright © 2012-2014 Www.tudoupe.Com. 土豆启动 版权所有 意见建议:tdsky@tudoupe.com

土豆系统,土豆PE,win7系统下载,win7 64位旗舰版下载,u盘启动,u盘装系统,win10下载,win10正式版下载,win10 RTM正式版下载,win8下载,电脑蓝屏,IE11修复,网络受限,4K对齐,双系统,隐藏分区,系统安装不了,U盘装系统,笔记本装系统,台式机装系统,diskgenius运用,GHSOT装系统,U盘修复,U盘技巧,U盘速度,U盘不能格式化,U盘复制发生错误,U盘加密,U盘选购,开机黑屏,蓝屏,进不了系统,上不了网,打不开程序,点击无反应,系统设置,PE个性化,PE添加网络,PE维护系统

点击这里给我发消息