Windows窗体:通过部分透明的always-on-top窗口传递点击次数

Sam*_*ver 11 .net c# transparent winforms

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

例

看到灰色吧?这会阻止我在此刻输入标签框.

Rez*_*aei 21

您可以通过向其扩展样式添加WS_EX_LAYEREDWS_EX_TRANSPARENT样式来创建窗口,单击.也以使其总在最前面设置其TopMosttrue并使其半透明的合适的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

样本结果

在此输入图像描述

  • @SamWeaver - 证明你可以随时学习新东西:) (5认同)
  • 您可以仅覆盖CreateParams并使用P / Invoke设置ExStyle。 (2认同)
  • @Marco谢谢你!你很善良:) (2认同)