我正在设计一个始终显示在屏幕上且不透明度约为 20% 的窗口。它被设计成一种状态窗口,所以它总是在顶部,但我希望人们能够通过该窗口单击到下面的任何其他应用程序。这是我现在键入时位于此 SO 帖子顶部的不透明窗口:
看到那个灰色条了吗?它会阻止我此刻在标签框中输入。
最佳答案
可以制作一个窗口,点击通过添加WS_EX_LAYERED和 WS_EX_TRANSPARENT样式到它的扩展样式。还要让它始终位于顶部设置其 TopMost为 true 并使其半透明使用合适的 Opacity值:
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);
}
}
示例结果
关于c# - Windows 窗体 : Pass clicks through a partially transparent always-on-top window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855720/