Nathan
Nathan

Reputation: 131

Reverse Form Click-Through?

So you can make a form Click-Through-Able...

Imports:

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

Code:

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

Now how would I go about reversing the effect after running the code once?

I tried this:

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

But that did not work.

Thanks in advance!

Upvotes: 1

Views: 301

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

As another option, you can remove those styles this way:

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

Note

The code would be more understandable using these constants:

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

Upvotes: 5

Xiaoy312
Xiaoy312

Reputation: 14477

In order to restore the style back to its initial state, you need to set the style to the value of initialStyle from the first snippet.

You can't just simply keep appending more flags onto the style and expecting it to return to normal.


public class Example
{
    private int _initialStyle = 0;

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

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

Upvotes: 3

Related Questions