附件:透明窗体
这几天在做个东东,需要将窗体嵌入桌面及窗口透明
开始我以为以我现在的所知应该没什么大问题,结果也的确如此,没什么大问题,只是细节上出了几个小问题...
1. 关于嵌入桌面
原理很简单,找到 Progman 并将窗口 SetParent 过去即可
(当然也可以嵌入到 Progman 的子窗口以保证窗口不覆盖桌面的图标,不过我的要求是我的窗口必须覆盖桌面图标,呵呵)
但是问题来了,我发现我的窗口在桌面上F5或者Win+D的时候会隐藏,尝试拦截 WM_SYSCOMMAND/SC_MINIMIZED消息无效
但是其他嵌入桌面的程序则没有此问题
查看了一下Window Style,发现我的窗口的 Ex Style 多了一个 WS_EX_APPWINDOW 属性(因为是主窗口)
但是我的主窗口此属性去不掉,因此 New 了一个 Form 动态创建后 OK
代码(就一句...)

2. 关于透明
第一个想到的是 Layered Window
需要注意的是窗口样式 ExStyle 必须包含 WS_EX_LAYERED(0x80000),而且在 CreateParams 里指定无效...
然后使用 SetLayeredWindowAttributes 设置透明度,关于这个函数网上有很多问题,这里不在赘述
执行时我发现我的窗口并没有被透明,SetLayeredWindowAttributes 返回 False
于是我估计还是跟窗体的 Style 有关系(因为我的窗口 BorderStyle=bsNone)
果然,在 CreateParams 里给窗体加上 WS_POPUP 后透明成功~鼓掌~~
不过这样窗口虽然透明了可是窗体里控件也跟着透明了...
跟着就想到 WS_EX_TRANSPARENT 样式,加上后窗体透明,但是控件不透明了
不过这个样式下当桌面被刷新的时候窗口得不到及时通知,因此显示会有点问题
正在尝试 MsgHook...(待续)
先贴个代码吧

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// D7以上直接使用 Form 的 AlphaBlend/AlphaBlendValue 或者 TransparentColor/TransparentColorVale 即可实现透明
// 其实最终还是用 SetLayeredWindowAttributes 实现的

2

3

4

5

看清这世界的美丽与残酷
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步