在C#开发桌面应用程序的过程中,制作悬浮工具、桌面挂件这类功能时,经常需要让窗口部分区域透明,同时让透明区域的鼠标点击能够穿透到下方的窗口,而非透明区域保持正常的点击响应。要实现这个效果,需要借助Windows系统的窗口样式设置和分层窗口相关属性来完成。
核心实现原理
Windows窗口的鼠标事件处理依赖于窗口的样式和扩展样式,要实现点击穿透,核心是要设置窗口的扩展样式为WS_EX_TRANSPARENT,同时结合分层窗口的属性,让系统判定透明区域不接收鼠标输入。另外还需要处理窗口的透明度、背景色等属性,确保透明区域的判定符合预期。
需要使用的Windows API
实现该功能需要调用以下几个核心的Windows API函数,我们需要先将这些函数引入到C#项目中:
- GetWindowLong:用于获取指定窗口的样式信息
- SetWindowLong:用于设置指定窗口的样式信息
- SetLayeredWindowAttributes:用于设置分层窗口的透明度、透明色等属性
首先定义API函数的引入代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public partial class TransparentClickThroughForm : Form
{
// 窗口样式常量
private const int GWL_EXSTYLE = -20;
private const int WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
// 分层窗口属性常量
private const int LWA_COLORKEY = 0x1;
private const int LWA_ALPHA = 0x2;
// 引入API函数
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
}
完整实现步骤
1. 设置窗口基础属性
首先需要将窗口的FormBorderStyle设置为无边框,同时设置窗口的透明色,这里我们将品红色(0xFF00FF)作为透明色,所有绘制为品红色的区域都会被视为透明区域:
public TransparentClickThroughForm()
{
InitializeComponent();
// 设置窗口无边框
this.FormBorderStyle = FormBorderStyle.None;
// 设置窗口背景色为透明色(品红色)
this.BackColor = System.Drawing.Color.Magenta;
// 设置窗口透明色,品红色区域会被判定为透明
this.TransparencyKey = System.Drawing.Color.Magenta;
}
2. 设置窗口扩展样式实现点击穿透
在窗口加载完成之后,我们需要获取当前窗口的扩展样式,添加分层窗口样式和透明点击穿透样式,然后重新设置窗口的扩展样式:
private void TransparentClickThroughForm_Load(object sender, EventArgs e)
{
// 获取当前窗口的扩展样式
int exStyle = GetWindowLong(this.Handle, GWL_EXSTYLE);
// 添加分层窗口样式和透明点击穿透样式
exStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
// 设置新的窗口扩展样式
SetWindowLong(this.Handle, GWL_EXSTYLE, exStyle);
// 设置分层窗口属性,指定透明色为品红色,透明色区域完全透明
SetLayeredWindowAttributes(this.Handle, 0xFF00FF, 0, LWA_COLORKEY);
}
3. 处理非透明区域的绘制
现在我们需要绘制非透明的交互区域,比如在窗口中心绘制一个蓝色的圆形,这个圆形区域会正常响应鼠标点击,而其余品红色区域会穿透点击:
private void TransparentClickThroughForm_Paint(object sender, PaintEventArgs e)
{
// 创建一个蓝色画刷
using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue))
{
// 在窗口中心绘制一个直径100的圆形,该区域为非透明交互区域
int centerX = this.Width / 2;
int centerY = this.Height / 2;
int radius = 50;
e.Graphics.FillEllipse(brush, centerX - radius, centerY - radius, radius * 2, radius * 2);
}
}
4. 绑定事件验证效果
我们可以给窗口绑定点击事件,验证非透明区域能够正常响应,透明区域点击会穿透到下层窗口:
private void TransparentClickThroughForm_Click(object sender, EventArgs e)
{
// 点击非透明区域时弹出提示
MessageBox.Show("点击了非透明区域");
}
private void TransparentClickThroughForm_MouseDown(object sender, MouseEventArgs e)
{
// 非透明区域按住鼠标可以拖动窗口
if (e.Button == MouseButtons.Left)
{
// 调用API实现窗口拖动
ReleaseCapture();
SendMessage(this.Handle, 0xA1, 0x2, 0);
}
}
// 拖动窗口需要的API
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
注意事项
- 如果设置
WS_EX_TRANSPARENT样式后所有区域都无法点击,需要检查是否同时设置了正确的透明色和分层窗口属性 - 若需要整个窗口都穿透点击,不需要绘制任何非透明区域即可,所有区域都会穿透到下层窗口
- 该实现方式仅适用于WinForms项目,WPF项目实现点击穿透需要使用不同的API和属性设置方式
- 透明色的选择要避免和窗口内需要显示的颜色冲突,否则对应颜色的区域也会被判定为透明
效果验证
运行程序后,窗口除了蓝色的圆形区域外都是透明的,点击蓝色圆形区域会弹出提示框,点击其余透明区域,鼠标事件会直接作用到下层的窗口,比如下层的浏览器、桌面图标等,实现了透明区域响应点击穿透的效果。
C#鼠标穿透透明区域点击WS_EX_TRANSPARENTSetWindowLong分层窗口修改时间:2026-06-17 17:40:00