反向表单点击?

时间:2016-08-17 21:31:35

标签: c# forms winforms winapi

所以你可以制作一个表格Click-Through-Able ...

进口:

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

代码:

int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);

现在,如何在运行一次代码后反转效果?

我试过了:

int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x10000 | 0x10);

但那没用。

提前致谢!

2 个答案:

答案 0 :(得分:4)

作为另一种选择,您可以通过以下方式删除这些样式:

var style = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, style & ~(0x80000 | 0x20));

注意

使用这些常量可以更容易理解代码:

const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;

答案 1 :(得分:3)

要将样式恢复为初始状态,您需要将样式设置为第一个代码段中initialStyle的值。

你不能只是简单地在风格上添加更多标志并期望它恢复正常。

public class Example
{
    private int _initialStyle = 0;

    public void ApplyStyle()
    {
        _initialStyle = GetWindowLong(...);
        SetWindowLong(..., _initialStyle | /* styles */);
    }

    public void RestoreStyle()
    {
        SetWindowLong(..., _initialStyle);
    }
}