gpt4 book ai didi

c# - 最上面的表格,点击 "through"可能吗?

转载 作者:可可西里 更新时间:2023-11-01 09:08:38 25 4
gpt4 key购买 nike

感谢您之前的回答,使我能够完成在鼠标坐标中显示大红叉的基本工具,以便更加明显。红色十字是透明形式的具有透明背景的图像。问题是您无法点击,因为它的最顶端和表单中心实际上位于鼠标 xy。有什么方法可以使它可用,以便十字仍显示在光标上但“可点击”通过吗?

最佳答案

您可以使用 SetWindowLong设置 WS_EX_TRANSPARENT 窗口样式:

If the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to the other windows underneath the layered window.

CodeProject 有 this详细介绍该技术的文章。虽然它是在 VB.NET 中,但应该很容易转换为 C#。

我过去使用过以下代码:

public enum GWL
{
ExStyle = -20
}

public enum WS_EX
{
Transparent = 0x20,
Layered = 0x80000
}

public enum LWA
{
ColorKey = 0x1,
Alpha = 0x2
}

[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hWnd, GWL nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hWnd, GWL nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte alpha, LWA dwFlags);

protected override void OnShown(EventArgs e)
{
base.OnShown(e);
int wl = GetWindowLong(this.Handle, GWL.ExStyle);
wl = wl | 0x80000 | 0x20;
SetWindowLong(this.Handle, GWL.ExStyle, wl);
SetLayeredWindowAttributes(this.Handle, 0, 128, LWA.Alpha);
}

但它也是从别处复制的。此处的重要行位于 OnShown 方法中。虽然我不得不承认这条线

wl = wl | 0x80000 | 0x20;

有点神秘,设置 WS_EX_LAYERED 和 WS_EX_TRANSPARENT 扩展样式。

你也可以这样设置

wl = wl | WS_EX.Layered | WS_EX.Transparent;

关于c# - 最上面的表格,点击 "through"可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1524035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com