【问题标题】:Windows Forms: Pass clicks through a partially transparent always-on-top windowWindows 窗体:通过部分透明的始终位于顶部的窗口传递点击
【发布时间】:2021-05-25 13:30:50
【问题描述】:

我正在设计一个始终显示在屏幕上且大约 20% 不透明的窗口。它被设计为一种状态窗口,因此它始终位于顶部,但我希望人们能够通过该窗口单击下面的任何其他应用程序。这是我现在键入时位于此 SO 帖子顶部的不透明窗口:

看到那个灰色条了吗?这会阻止我现在在标签框中输入。

【问题讨论】:

  • winforms 不可能。
  • 你有任何证据支持这个答案吗?我觉得这很难相信......
  • 相信它。我的“证据”是十多年使用获胜表格的经验。
  • @rory.ap 我认为发布的答案是 OP 正在寻找的。我错过了问题的一部分吗?它在 Windows 7 上运行。
  • 我的立场是正确的。太酷了!

标签: c# .net winforms transparent


【解决方案1】:

您可以通过将WS_EX_LAYEREDWS_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);
    }
}

样本结果

【讨论】:

  • 您可以直接覆盖 CreateParams 并使用 P/Invoke 设置 ExStyle。
  • 我尝试了这个解决方案,但是当我点击窗口时没有任何反应
  • @BruceStackOverFlow 点击将传递到后台窗口。
  • 我使用 ubuntu 和 mono 似乎这不起作用...stackoverflow.com/questions/61000080/…
  • @BruceStackOverFlow 它是 Windows 上的 WinForms (.NET) 而不是 Linux 上的单声道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多