gpt4 book ai didi

c# - SetWindowLong 启用/禁用单击表单

转载 作者:行者123 更新时间:2023-11-30 20:47:31 30 4
gpt4 key购买 nike

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);

void ClickThrough()
{
int wl = GetWindowLong(this.Handle, GWL.ExStyle);
wl = wl | 0x80000 | 0x20;
SetWindowLong(this.Handle, GWL.ExStyle, wl);
}

所以这成功地呈现了我的应用程序“点击”,所以它可以保持在最上面,但我仍然可以点击它后面的应用程序。用于它的流行代码段,但我的问题是,如何禁用它?

如何还原它以便我可以再次单击我的应用程序而无需重新启动它?

最佳答案

wl = wl | 0x80000 | 0x20;

在这里您使用按位或添加标志 WS_EX_LAYEREDWS_EX_TRANSPARENT。就其值(value)而言,像这样使用魔法常量是一种糟糕的形式。使用适当的名称声明常量:

public const uint WS_EX_LAYERED = 0x00080000;
public const uint WS_EX_TRANSPARENT = 0x00000020;

并在GetWindowLongSetWindowLong 中使用uint,以方便使用。

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

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

然后像这样设置扩展样式:

uint ex_style = GetWindowLong(this.Handle, GWL.ExStyle);
SetWindowLong(this.Handle, GWL.ExStyle, ex_style | WS_EX_LAYERED | WS_EX_TRANSPARENT);

像这样反转这个变化:

uint ex_style = GetWindowLong(this.Handle, GWL.ExStyle);
SetWindowLong(this.Handle, GWL.ExStyle, ex_style & !WS_EX_LAYERED & !WS_EX_TRANSPARENT);

您可以对样式使用枚举,但我觉得这有点不对劲,因为值与按位运算相结合。因此我更喜欢使用 uint

关于c# - SetWindowLong 启用/禁用单击表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743982/

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