Windows 表单:通过部分透明的始终在顶部传递点击 window
Windows Forms: Pass clicks through a partially transparent always-on-top window
我正在设计一个 window,它始终显示在屏幕上,不透明度约为 20%。它被设计成一种状态 window,因此它始终位于顶部,但我希望人们能够通过单击 window 进入下面的任何其他应用程序。这是不透明的 window 位于此 SO post 之上,我现在正在输入:
看到那个灰色条了吗?它会阻止我此刻在标签框中输入。
您可以通过添加 WS_EX_LAYERED
and WS_EX_TRANSPARENT
styles to its extended styles. Also to make it always on top set its TopMost
to true
and to make it semi-transparent use suitable Opacity
值来制作 window,点击率:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Opacity = 0.5;
this.TopMost = true;
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var style = GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle,GWL_EXSTYLE , style | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
}
示例结果
我正在设计一个 window,它始终显示在屏幕上,不透明度约为 20%。它被设计成一种状态 window,因此它始终位于顶部,但我希望人们能够通过单击 window 进入下面的任何其他应用程序。这是不透明的 window 位于此 SO post 之上,我现在正在输入:
看到那个灰色条了吗?它会阻止我此刻在标签框中输入。
您可以通过添加 WS_EX_LAYERED
and WS_EX_TRANSPARENT
styles to its extended styles. Also to make it always on top set its TopMost
to true
and to make it semi-transparent use suitable Opacity
值来制作 window,点击率:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Opacity = 0.5;
this.TopMost = true;
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var style = GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle,GWL_EXSTYLE , style | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
}
示例结果