C# 设置Windows程序窗口为穿透状态

本文介绍了一种在Windows应用程序中实现窗体鼠标穿透的方法,通过使用.NET平台下的P/Invoke技术调用Windows API,包括SetWindowLong、GetWindowLong及SetLayeredWindowAttributes等函数,使窗体支持鼠标穿透功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

 1         private const uint WS_EX_LAYERED = 0x80000;
 2         private const int WS_EX_TRANSPARENT = 0x20;
 3         private const int GWL_STYLE = (-16);
 4         private const int GWL_EXSTYLE = (-20);
 5         private const int LWA_ALPHA = 0;
 6 
 7         [DllImport("user32", EntryPoint = "SetWindowLong")]
 8         private static extern uint SetWindowLong(
 9         IntPtr hwnd,
10         int nIndex,
11         uint dwNewLong
12         );
13 
14         [DllImport("user32", EntryPoint = "GetWindowLong")]
15         private static extern uint GetWindowLong(
16         IntPtr hwnd,
17         int nIndex
18         );
19 
20         [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
21         private static extern int SetLayeredWindowAttributes(
22         IntPtr hwnd,
23         int crKey,
24         int bAlpha,
25         int dwFlags
26         );
27 
28         /// <summary> 
29         /// 设置窗体具有鼠标穿透效果 
30         /// </summary> 
31         public void SetPenetrate()
32         {
33             this.TopMost = true;
34             GetWindowLong(this.Handle, GWL_EXSTYLE);
35             SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
36             SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
37         }

 

转载于:https://www.cnblogs.com/Red-ButterFly/p/7726534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值